[A] Let's simplify the getPlayer2Move rules first.
- If round is divisible by 3, then return 3
if a number is divisible by another number that means the remainder of the division is 0. We can write the code as follows:
if (round%3 == 0)
result = 3;
- If round is not divisible by 3 but is divisible by 2, then return 2.
Same as the previous one, we are going to use the remainder operation. We can use an else-if condition here.
else if (round%2 == 0)
result = 2;
- If round is not divisible by 3 and is not divisible by 2, then return 1.
An else would work fine here.
else
result = 1;
The full code of part A is attached below.
[B]
playGame method:
Let's have a quick look at the game rules first:
- If both players spend the same number of coins, player 2 gains 1 coin.
This can be translated to if player1Spending == player2Spending, then player2 gets a coin.
- If the players do not spend the same number of coins and the positive difference between the number of coins spent by the two players is 1, player 2 is awarded 1 coin.
If the absolute value of (player1Spending - player2Spending == 1) then player2 gets awarded.
- If the players do not spend the same number of coins and the positive difference between the number of coins spent by the two players is 2, player 1 is awarded 2 coins.
We can either use an else statement or a separate conditional statement here, but I'll go for the else statement.
We can use a while loop to detect when the game ends, either a player's coins is less than 3 OR when the maxRounds have been played.