write a program that allows your computer program play the rock-paper-scissors-lizard-spock game against itself. Refer to http://en.wikipedia.org/wiki/Rock-paper-scissors-lizard-Spock for more information.
In this problem, the program simulates two players who play against
each other. The play continues until one of the computer-generated
players wins 4 times. Solve the problem using a class called
"RockPaperScissorLizardSpock". Feel free to use as many other
classes as needed to make proper use of OOP design.
The program should use multiple threads to simulate "simultaneous
play", just as in the real game itself. Use multi-threading /
concurrency techniques to simulate the actual game's simultaneous play
method where both players throw their hand at the same time for each
iteration of the game and then determine who wins this play.
In this game, in every iteration, two random integers are generated in
the range of [1 to 5] - 1 refers to Rock. 2 refers to Paper. 3 refers to
Scissors. 4 refers to Lizard. 5 refers to Spock.
For example, if the computer randomly generates integers 2 and 5 in
the first iteration, 2 is for the first player and 5 is for the second
player. 2 refers to Paper and 5 refers to Spock. Based on Rule 8 in the
following 10 rules, Paper (2) disproves Spock (5), so player 1
wins.
Likewise, continue the iterations until one player wins 4 times. All
other outcomes such as the "Spock vs. Spock" would be
considered a draw. Also, ensure that the play ends after a certain number
of iterations. Use custom Exceptions with try/catch and not accept any
system error message.
rule 1: Scissor cut paper
rule 2: Paper covers rock
rule 3: Rock crushes lizard
rule 4: Lizard poisons Spock
rule 5: Spock smashes (or melts) scissor
rule 6: Scissor decapitate lizard
rule 7: Lizard eats paper
rule 8: Paper disproves Spock
rule 9: Spock vaporizes rock
rule 10: Rock breaks scissors
Answer
import java.util.Scanner;
import java.lang.Math;
public class
RockPaperScissorLizardSpock extends Thread {
// attributes
static int Player1wins = 0;
static int Player2wins = 0;
// run method used by thread
public void run() {
// Number of threads
int threads_number = 1000;
for(int
i=1 ; i < threads_number ; i++)
{
try
{
Thread.sleep(1000);
}catch(InterruptedException
e){
System.out.println(e);
}
play();
}
}
// main method to run the program
public static void main(String[]
args){
// instance object from
RockPaperScissorLizardSpock
RockPaperScissorLizardSpock r1 =new
RockPaperScissorLizardSpock();
// run threads
r1.run();
}//end main
// random method to generate random
number between 1 - 5
public static int rand(){
return (int) (Math.random() * 5) + 1;
}
// end
// get name of Rock ,Paper,
Scissor,Lizard,Spock according to random number
public static String getName(int n) {
switch(n) {
case 1:
return
"rock";
case 2:
return
"spock";
case 3:
return
"paper";
case 4:
return
"lizard";
case 5:
return
"scissors";
default:
return
"Invalid";
}
}
// play game method
public static void play(){
// First
player
// Second
player
// get a random number for first and
second player
String FirstPlayer = getName(rand());
String SecondPlayer =
getName(rand());
System.out.println("First player
chose "+FirstPlayer);
// check the FirstPlayer and
SecondPlayer choice and determine who is winner according to rules
if(FirstPlayer.equals(SecondPlayer)){
System.out.println("First player
chose "+FirstPlayer+" and Second player chose
"+SecondPlayer+" -> draw!");
}
if(FirstPlayer.equals("rock")){
if(SecondPlayer.equals("paper")){
System.out.println("Second
player chose Paper\nPaper covers Rock! Second player WINS!");
Player2wins++;
}
if(SecondPlayer.equals("scissors")){
System.out.println("Second
player chose Scissors!\nRock breaks Scissors! First player WINS! ");
Player1wins++;
}
if(SecondPlayer.equals("spock")){
System.out.println("Second
player chose Spock!\nSpock vaporizes Rock! Second player WINS! ");
Player2wins++;
}
if(SecondPlayer.equals("lizard")){
System.out.println("Second
player chose Lizard!\nRock crushes Lizard! First player WINS! ");
Player1wins++;
}
}// end if
if(FirstPlayer.equals("paper")){
if(SecondPlayer.equals("rock")){
System.out.println("Second
player chose Rock!\nPaper covers Rock! First player WINS! ");
Player1wins++;
}
if(SecondPlayer.equals("scissors")){
System.out.println("Second
player chose Scissors!\nScissors cuts Paper! Second player WINS!");
Player2wins++;
}
if(SecondPlayer.equals("spock")){
System.out.println("Second
player chose Spock!\nPaper disproves Spock! First player WINS! ");
Player1wins++;
}
if(SecondPlayer.equals("lizard")){
System.out.println("Second
player chose Lizard!\nLizard eats Paper! Second player WIN!");
Player2wins++;
}
} // end if
if(FirstPlayer.equals("scissors")){
if(SecondPlayer.equals("rock")){
System.out.println("Second
player chose Rock!\nRock breaks Scissors! Second player WINS!");
Player2wins++;
}
if(SecondPlayer.equals("paper")){
System.out.println("Second
player chose Paper!\nScissors cuts Paper! First player WINS! ");
Player1wins++;
}
if(SecondPlayer.equals("spock")){
System.out.println("Second
player chose Spock!\nSpock smashes Scissors! Second player WINS! ");
Player2wins++;
}
if(SecondPlayer.equals("lizard")){
System.out.println("Second
player chose Lizard!\nScissors decapitates Lizard! First player WINS!");
Player1wins++;
}
}// end if
if(FirstPlayer.equals("lizard")){
if(SecondPlayer.equals("rock")){
System.out.println("Second
player chose Rock!\nRock crushes Lizard! Second player WINS!");
Player2wins++;
}
if(SecondPlayer.equals("paper")){
System.out.println("Second
player chose Paper!\nLizard eats Paper! First player WINS!");
Player1wins++;
}
if(SecondPlayer.equals("spock")){
System.out.println("Second
player chose Spock!\nLizard poisons Spock! First player WINS! ");
Player1wins++;
}
if(SecondPlayer.equals("scissors")){
System.out.println("Second
player chose Scissors!\nScissors decapitates Lizard! Second player
WINS!");
Player2wins++;
}
}// end if
if(FirstPlayer.equals("spock")){
if(SecondPlayer.equals("rock")){
System.out.println("Second
player chose Rock!\nSpock vaporizes Rock! First player WINS!\n");
Player1wins++;
}
if(SecondPlayer.equals("paper")){
System.out.println("Second
player chose Paper!\nPaper disproves Spock! Second player WINS!\n");
Player2wins++;
}
if(SecondPlayer.equals("lizard")){
System.out.println("Second
player chose Lizard!\nLizard poisons Spock! Second player WINS!\n");
Player2wins++;
}
if(SecondPlayer.equals("scissors")){
System.out.println("Second
player chose Scissors!\nSpock smashes Scissors! First player WINS! \n");
Player1wins++;
}
} //
end if
System.out.println("");
// if player 1 wins stop game
if(Player1wins == 4 ){
System.out.println("\n\nFirst
player wins "+Player1wins +" times\n");
System.exit(0);
}
// if player 2 wins stop game
if( Player2wins == 4){
System.out.println("\n\nSecond
player wins "+Player2wins+" times\n");
System.exit(0);
}
}
// end method
}//end class