In: Computer Science
The answer should be in JAVA.
You will design and implement two classes to support a client
program, RockPaperScissorsGame.java, to simulate
Rock-Paper-Scissors game.
Read and understand the client program to find out the requirements
for the HandShape and Player classes.
The rules of the Rock-Paper-Scissors game are:
Scissors✌️ beats Paper✋ that beats Rock✊ that
beats Scissors✌️
Additionally, to simplify the game logic (and complexify a little
bit the HandSahpe class) , two players cannot show the same hand
shape.
The followings are some sample runs:
$java RockPaperScissorsGame
Alice shows Paper
Bob shows Scissors
Bob wins!
$java RockPaperScissorsGame
Alice shows Paper
Bob shows Rock
Alice wins!
$java RockPaperScissorsGame
Alice shows Scissors
Bob shows Rock
Bob wins!
RockPaperScissorsGame.java
public class RockPaperScissorsGame { public static void main(String[] args) { String[] handShapeName = {"Rock", "Paper", "Scissors"}; HandShape handShape = new HandShape(); Player player1 = new Player("Alice"); Player player2 = new Player("Bob"); System.out.println(player1.getName() + " shows " + handShapeName[player1.showHand(handShape)]); System.out.println(player2.getName() + " shows " + handShapeName[player2.showHand(handShape)]); System.out.println(player1.findWinner(player2) + " wins!"); } }
public class RockPaperScissorsGame {
public static void main(String[] args) {
String[] handShapeName = {"Rock", "Paper", "Scissors"};
HandShape handShape = new HandShape();
Player player1 = new Player("Alice");
Player player2 = new Player("Bob");
System.out.println(player1.getName() + " shows " +
handShapeName[player1.showHand(handShape)]);
System.out.println(player2.getName() + " shows " +
handShapeName[player2.showHand(handShape)]);
System.out.println(player1.findWinner(player2) + " wins!");
}
public class RockPaperScissor {
private Set<Situation> winSituations;
public RockPaperScissor() {
this.winSituations = new HashSet<>();
this.winSituations.add(new Situation("Rock", "Scissor"));
this.winSituations.add(new Situation("Scissor", "Paper"));
this.winSituations.add(new Situation("Paper", "Rock"));
}
public Result evaluateWinner(Situation situation) {
if (this.winSituations.contains(situation)) {
return Result.PLAYER1_WINS;
} else (this.winSituations.contains(situation.invert())) {
return Result.PLAYER2_WINS;
}
}
public static void main(String[] args) {
RockPaperScissor RockPaperScissor = new RockPaperScissor();
System.out.println(RockPaperScissor.evaluateWinner(new
Situation("Rock", "Rock")));
System.out.println(RockPaperScissor.evaluateWinner(new
Situation("Scissor", "Paper")));
System.out.println(RockPaperScissor.evaluateWinner(new
Situation("Rock", "Paper")));
}
}