Player 3
In your example, you state that we are not interested in the 2 from 2.8, but you never explain why. Why?
This is a remainder challenge. We are only interested in the remainder and hence we want to investigate the 0.8 of the 2.8 and we are not interested in the 2.
Does this help?
Kind regards, Lisa
For 63:
63 % 4 = 15.75
Take the 15 away, and you’re left with 0.75
4 * 0.75 = 3, meaning Player 4 goes first.
In C++:
int main()
{
int goFirst{};
goFirst = 63 % 4;
if (goFirst == 0) {std::cout << “Player 1 goes first \n”;}
else if (goFirst == 1) {std::cout << “Player 2 goes first \n”;}
else if (goFirst == 2) {std::cout << “Player 3 goes first \n”;}
else {std::cout << “Player 4 goes first \n”;}
}
For the previous poster’s number: 1789
1789 / 4 = 447.25
4 * 0.25 = 1
Player 2 goes first.
Next number = 99
Thank you.
Python code below:
import random
#random_number = random.randint(1, 100)
random_number = 99
next_number = random.randint(1, 1000)
number_of_players = int(input("How many players are there?"))
print(
f"\nRandom number is {random_number}\nNumber of players is {number_of_players}\n"
)
p1 = "Player 1"
p2 = "Player 2"
p3 = "Player 3"
p4 = "Player 4"
goes_first = str
if random_number % number_of_players == 0:
goes_first = p1
elif random_number % number_of_players == 1:
goes_first = p2
elif random_number % number_of_players == 2:
goes_first = p3
else:
goes_first = p4
print(f"{goes_first} goes first")
print(f"The next number is {next_number}")
Output:
How many players are there?4
Random number is 99
Number of players is 4
Player 4 goes first
The next number is 894
589 % 4 = 0.25
0.25 * 4 = 1
589 % 4 = 1
Next Number = 76
C
#include <stdio.h>
int main(int argc, char** argv) {
printf("Player %d goes first\n", 7926 % 4 + 1);
return 0;
}
Output:
Player 3 goes first
Next number 7510
JS
Result : player3
Next number : 6321
Code written in C++
#include <iostream>
#include <cstdlib>
#include <ctime>
// Function to calculate player name based on remainder
void printPlayerName(int num) {
int remainder = num % 4;
switch (remainder) {
case 0:
std::cout << "Player 1";
break;
case 1:
std::cout << "Player 2";
break;
case 2:
std::cout << "Player 3";
break;
case 3:
std::cout << "Player 4";
break;
default:
std::cout << "Invalid number";
break;
}
std::cout << std::endl;
}
int main() {
// Declare three integer values
int num1 = 63;
int num2 = 6321;
int randNum = rand() % 10000; // Generate a random number between 0 and 10,000
// Calculate remainder and output player for num1
printPlayerName(num1);
// Calculate remainder and output player for num2
printPlayerName(num2);
// Output random number
std::cout << "Random number: " << randNum << std::endl;
return 0;
}
Result for default value: Player 4
Result for previous value in this thread: Player 2
Next Number: 9383
63 % 4 = 3
Since our player numbering starts at 0 (meaning 0 remainder), the player at remainder 3 is the 4th player! So Player 4 goes first.
in C++:
#include <iostream>
using namespace std;
int main() {
int remainder = 63 % 4;
cout << remainder;
}
The calculator makes this trickier for me to translate the answer to the logic.
63 / 4 = 15.75. We are left with .75 of a number, so 3/4 around our circle lands us at 3. This is our 4th Player.
New Number: 82
Working on learning Powershell scripting so I’m crossing streams so to speak…
Ugly but I wanted to try ‘switch’, and it works!
82 % 4 = 2
Player 3 will be going first!
New number: 93
63 / 4 = 15.75
15.75 - 15 = 0.75
0.75 * 4 = 3 (Therefore player 4 goes first)
In code: 63 % 4 = 3 (don’t know enough about code to actually do it)
93 % 4 = 1 (Therefore player 2 goes first)
New Number: 55
1%4 = 0.25
0.25*4 = 1
Player 2 turn.
Next Number: 148
I took a bit of a different approach to solving this. I ran a while loop that would generate a new player from a list of players. This is in javascript:
// player array
const players = [{ name: "newt", turn: 0 }, { name: "ripley", turn: 0 }, { name: "hicks", turn: 0 }, { name: "apone", turn: 1}];
// set global turn order array
let turnOrder = [];
// get random number helper fn
function getRandomInteger(max) {
return Math.round(Math.random() * max);
}
// check if a number is in an array
function validatePlayer(num, arr) {
if (!arr) {
console.error("no array has been passed");
return;
}
if (!arr.includes(num)) {
return true;
}
}
// update global turn order array fn
function setTurnOrder() {
// while loop to test if the arrays line up
while (turnOrder.length < players.length) {
let nextPlayer = getRandomInteger(players.length - 1);
// check if that player has already been assigned
if (validatePlayer(nextPlayer, turnOrder)) {
// if they haven't, then add them to the array
players[nextPlayer].turn = turnOrder.length
turnOrder.push(nextPlayer);
}
}
}
setTurnOrder()
console.log(players, turnOrder)```