Example from video by gerrie converted to c++

int totalTimeInSeconds = 29; // add 1 latter for 30 sec
int timeOfTurn = 30;
int numberTurn = totalTimeInSeconds / timeOfTurn; //  0 <-(29sec) 
numberTurn += 1; // 1 |
int secondsInTurn = totalTimeInSeconds % 30; // 29sec <--(29 % 30) // add 1 later
if (numberTurn % 2 == 0) // even turns are for player2 and odd for player1
{
    std::cout << "It is player2 turn." << std::endl;
}
else // (1 % 2 == 1) and going in here for first turn
{
    std::cout << "It is player1 turn." << std::endl;
}
std::cout << "He is in "<< numberTurn << ". turn" << " having done " << secondsInTurn + 1
    <<" seconds of the total time " << totalTimeInSeconds + 1 << std::endl;

How does it look? Is it correct? Its a litle tricky to get numbers right and its starts right at first line adding 1 to 29 to get total of 30 seconds that passed.

2 Likes

Does it work properly? The code looks right.

Yep for total of 30(29+1) seconds pased. The player1 has just finished his turn. Now its player2 turn for a total time of 31 seconds:

Excelent !! Addiing some more players would be nice. Like 4 of them interchaging: 1 2 3 4 1 2 3 4

Thanks and Cheers

int main()
{
// Idea: extend the program to 4 players: every 30 second then next player is for a turn
int totalTimeInSeconds = 90; // add 1 latter for actual total time in seconds
int timeOfTurn = 30;
int numberTurn = totalTimeInSeconds / timeOfTurn; // ( 0 = 29sec / 30 ) // find out which turn it is and then
numberTurn += 1; // (1 = 0 + 1) // count them
int secondsInTurn = totalTimeInSeconds % 30; // ( 29 = 29 % 30 ) // how many seconds have passed in turn
secondsInTurn += 1;
if (numberTurn % 4 == 0)
{
std::cout << β€œIt is player4 turn.” << std::endl;
}
else if(numberTurn % 4 == 1)
{
std::cout << β€œIt is player1 turn.” << std::endl;
}
else if(numberTurn % 4 == 2)
{
std::cout << β€œIt is player2 turn.” << std::endl;
}
else if(numberTurn % 4 == 3)
{
std::cout << β€œIt is player3 turn.” << std::endl;
}

std::cout << "He is in " << numberTurn << ". turn" << " having done " << secondsInTurn
    << " seconds of the total time " << totalTimeInSeconds + 1 << std::endl;

}

Thats it 4 players interchanging in time. :smiley:

Great work Pavle!

Thank you very much. :smiley:

Privacy & Terms