In: Computer Science
Write a Java program that plays the game Rock, Paper, Scissors.
The program should generate a random choice (Rock, Paper or Scissors) then ask the user to choose Rock, Paper or Scissors. After that the program will display its choice and a message showing if the player won, lost or tied.
Next, the program should prompt the user to play again or not. Once the player selects to stop playing the game, the program should print the number of wins, losses and ties by the player.
Run your program and show at least 2 examples of playing the game before quitting. For example, the output might look like:
Choose: (0=Rock, 1=Paper, 2=Scissors): 1
You chose Paper
Computer chose Rock
You win
Play again? (y/n): y
Choose: (0=Rock, 1=Paper, 2=Scissors): 0
You chose Rock
Computer chose Scissors
You win
Play again? (y/n): n
You won 2 times
You lost 0 times
You tied 0 times
import java.io.*;
import java.util.*;
public class RockPaperScissor
{
// global named constants for game choices
static final int ROCK = 1;
static final int PAPER = 2;
static final int SCISSORS = 3;
// global names constants for game outcomes
static final int PLAYER1_WINS = 11;
static final int PLAYER2_WINS = 12;
static final int DRAW = 3;
// global named constant for error condition
static final int ERROR = -1;
/**
* 1. Get human player's choice
* 2. Get computer player's (random) choice
* 3. Check human player's choice
* 4. Check computer player's choice
* 5. Announce winner
*/
public static void main(String[] args)
{
char ch = 'y';
int won = 0, tied = 0, lost = 0;
do{
Scanner scan = new Scanner(System.in);
PrintStream output = System.out;
int player1, player2;
// get player 1 input as 1 (rock), 2 (paper or 3 (scissors)
output.print("Choose 1 (rock), 2 (paper), or 3 (scissors):
");
player1 = scan.nextInt();
if(!(player1 >=1 && player1<=3)){
System.out.println("Invalid Choice, Aborting");
System.exit(0);
}
// echo human player's choice
System.out.print(" You chose ");
if (player1 == ROCK) { System.out.println("rock"); }
else if (player1 == PAPER) { System.out.println("paper"); }
else { System.out.println("scissors"); }
// now computer picks one randomly
output.println("Now Computer choose one ...");
Random r = new Random();
player2 = r.nextInt(3) + 1;
System.out.print(" Computer choose ");
if (player2 == ROCK) { System.out.println("rock"); }
else if (player2 == PAPER) { System.out.println("paper"); }
else { System.out.println("scissors"); }
int result ;
if (player1 == ROCK) {
result =
rockChoice(player2);
} else if (player1 == PAPER)
{
result =
paperChoice(player2);
} else {
result =
scissorsChoice(player2);
}
if(result==3) {
System.out.println("\nTHERE IS DRAW");
++tied;
}else if(result==11) {
System.out.println("\nYOU WIN");
++won;
}else {
System.out.println("\nCOMPUTER WIN");
++lost;
}
System.out.print("Play again?
(y/n): ");
ch = scan.next().charAt(0);
}while(ch =='y' || ch=='Y');
System.out.println("You won "+ won + " times");
System.out.println("You lost "+ lost + "
times");
System.out.println("You tied "+ tied + "
times");
} // end main
public static int rockChoice(int choice){
if(!(choice >=1 && choice<=3)){
System.out.println("Invalid Choice,
Aborting");
return ERROR;
}else if(choice==1){
return 3;
} else if(choice==2){
return 12;
} else
return 11;
}
public static int paperChoice(int choice){
if(!(choice >=1 &&
choice<=3)){
System.out.println("Invalid Choice, Aborting");
return ERROR;
}else if(choice==1){
return 11;
} else if(choice==2){
return 3;
} else
return 12;
}
public static int scissorsChoice(int choice){
if(!(choice >=1 &&
choice<=3)){
System.out.println("Invalid Choice, Aborting");
return ERROR;
}else if(choice==1){
return 12;
} else if(choice==2){
return 11;
} else
return 3;
}
} // end class
======================================================================
SEE OUTPUT
Thanks, PLEASE COMMENT if
there is any concern.
========================================================================
Removed and Modified to make it little bit
Simpler
import java.io.*;
import java.util.*;
public class RockPaperScissor {
// global named constants for game choices
static final int ROCK = 1;
static final int PAPER = 2;
static final int SCISSORS = 3;
public static void main(String[] args) {
char ch = 'y';
int won = 0, tied = 0, lost =
0;
do {
Scanner scan =
new Scanner(System.in);
PrintStream
output = System.out;
int player1,
player2;
// get player 1
input as 1 (rock), 2 (paper or 3 (scissors)
output.print("Choose 1 (rock), 2 (paper), or 3 (scissors):
");
player1 =
scan.nextInt();
// echo human
player's choice
System.out.print(" You chose ");
if (player1 ==
ROCK) {
System.out.println("rock");
} else if
(player1 == PAPER) {
System.out.println("paper");
} else {
System.out.println("scissors");
}
// now computer
picks one randomly
output.println("Now Computer choose one ...");
Random r = new
Random();
player2 =
r.nextInt(3) + 1;
System.out.print(" Computer choose ");
if (player2 ==
ROCK) {
System.out.println("rock");
} else if
(player2 == PAPER) {
System.out.println("paper");
} else {
System.out.println("scissors");
}
int
result;
if (player1 ==
ROCK) {
result = rockChoice(player2);
} else if
(player1 == PAPER) {
result = paperChoice(player2);
} else {
result = scissorsChoice(player2);
}
if (result ==
3) {
System.out.println("\nTHERE IS DRAW");
++tied;
} else if
(result == 11) {
System.out.println("\nYOU WIN");
++won;
} else {
System.out.println("\nCOMPUTER WIN");
++lost;
}
System.out.print("Play again? (y/n): ");
ch =
scan.next().charAt(0);
} while (ch == 'y' || ch ==
'Y');
System.out.println("You won " +
won + " times");
System.out.println("You lost " +
lost + " times");
System.out.println("You tied " +
tied + " times");
} // end main
public static int rockChoice(int choice) {
if (choice == 1) {
return 3;
} else if (choice == 2) {
return 12;
} else
return 11;
}
public static int paperChoice(int choice) {
if (choice == 1) {
return 11;
} else if (choice == 2) {
return 3;
} else
return 12;
}
public static int scissorsChoice(int choice)
{
if (choice == 1) {
return 12;
} else if (choice == 2) {
return 11;
} else
return 3;
}
} // end class
Check the Simpler
code above, Let me know if there is any
concern.