Java Reference
In-Depth Information
This method decides whether a game is a tie or which player is the winner. It outputs
the players' selections and the winner of the game. This method has two parameters:
player 1's choice and player 2's choice. It returns the number (1 or 2) of the winning
player.
The definition of this method is:
Method
game
Result
public static int gameResult(RockPaperScissors play1,
RockPaperScissors play2)
{
int winner = 0;
RockPaperScissors winnerObject;
if (play1 == play2)
{
winner = 0;
System.out.println("Both players selected "
+ play1
+ ". This game is a tie.");
}
else
{
winnerObject = winningObject(play1, play2);
//Output each player's choice
System.out.println("Player 1 selected " + play1
+ " and player 2 selected "
+ play2 + ".");
//Decide the winner
if (play1 == winnerObject)
winner = 1;
else if (play2 == winnerObject)
winner = 2;
//Output winning object's message
System.out.println(winnerObject.getMessage());
//Output the winner
System.out.println("Player " + winner
+ " wins this play.");
}
return winner;
}
 
Search WWH ::




Custom Search