Answer:
(1)
public int getPlayer2Move(int round) {
int result = 0;
//If the round number is divisible by 3
if(round % 3 == 0) {
result = 3;
}
//If round isn't divisible by 3 but is divisible by 2
else if(round % 3!= 0 && round % 2 == 0) {
result = 2;
}
//If it's neither divisible by 3 nor 2
else {
result = 1;
}
return result;
}
(2)
public void playGame() {
//Initializing coins for player 1
int player1Coins = startingCoins;
//Initializing coins for player 2
int player2Coins = startingCoins;
for (int round = 1; round <= maxRounds; round++) {
//If either player has less than 3 coins
if(player1Coins < 3 || player2Coins < 3) {
break;
}
//Coins spent by player 1
int player1Spends = getPlayer1Move();
//Coins spent by player 2
int player2Spends = getPlayer2Move(round);
//Calculating remaining coins for player 1
player1Coins -= player1Spends;
//Calculating remaining coins for player 2
player2Coins -= player2Spends;
//If both players spend the same amount
if(player1Spends == player2Spends) {
player2Coins += 1;
continue;
}
//Calculating the absolute difference in coins spent
int difference = Math.abs(player1Spends - player2Spends);
//If the difference is 1
if(difference == 1) {
player2Coins += 1;
continue;
}
//If the difference is 2
if(difference == 2) {
player1Coins += 2;
continue;
}
}
//At the end of the game
//If both players have equal coins
if(player1Coins == player2Coins) {
System.out.println("tie game");
}
//If player 1 has more coins than player 2
else if(player1Coins > player2Coins) {
System.out.println("player 1 wins");
}
//If player 2 has more coins than player 1
else if(player1Coins < player2Coins) {
System.out.println("player 2 wins");
}
}