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!"); } }
Program:
File name: RockPaperScissorsGame.java
//Include necessary header files
import java.util.ArrayList;
import java.util.Random;
//Define the HandShape class
class HandShape
{
/*Declare the arratlist to store the handshapes*/
ArrayList<String> Shapes=new ArrayList<String>();
//Define the constructor
HandShape()
{
//Call the method "add()"
Shapes.add("Rock");
Shapes.add("Paper");
Shapes.add("Scissors");
}
//Define the function "getShape()"
public String getShape()
{
//Object creation
Random r = new Random();
/*Randomly select a hand shape from list*/
String shape= Shapes.get(r.nextInt(Shapes.size()));
//Call the method "remove()"
Shapes.remove(shape);
//Return the shape
return shape;
}
}
//Define the player class
class Player
{
//Variable declaration
String name;
String str;
//Define the constructor
Player(String name)
{
this.name=name;
}
//Define the method "setName()"
public void setName(String name)
{
this.name=name;
}
//Define the method "getName()"
public String getName()
{
//Return the player name
return this.name;
}
//Define the method "showHand()"
public int show_Hand(HandShape hs1)
{
//Call the method "getShape()"
str=hs1.getShape();
//If the shape is equal to Rock
if(str=="Rock")
{
//Then, return the index of the handshape
return 0;
}
//If the shape is equal to Paper
else if(str=="Paper")
//Then, return the index of the handshape
return 1;
//Otherwise
else
//Return the index of the handshape
return 2;
}
//Define the function "find_Winner()"
public String find_Winner(Player pla2)
{
//Check the condition
if((this.str=="Scissors" && pla2.str=="Rock")||(this.str=="Paper" && pla2.str=="Scissors")||(this.str=="Rock" && pla2.str=="Paper"))
//Return the player2 name
return pla2.name;
//Otherwise
else
//Return the other plyer name
return this.name;
}
}
//Define the class RockPaperScissorsGame
public class RockPaperScissorsGame
{
//Define the "main()" method
public static void main(String[] args)
{
//Print the statement
System.out.println("$java RockPaperScissorsGame");
//Declare the variables
String[] handShapeName= {"Rock","Paper","Scissors"};
//Create object for "HandShape" class
HandShape hs=new HandShape();
//Create object for "player" class
Player p1=new Player("Alice");
Player p2=new Player("Bob");
//Print the result
System.out.println(p1.getName()+" shows "+handShapeName[p1.show_Hand(hs)]);
//Print the result
System.out.println(p2.getName()+" shows "+handShapeName[p2.show_Hand(hs)]);
//Print the winner
System.out.println(p1.find_Winner(p2)+" wins!");
}
}
Output: