Write a program that lets a user play "Rock, Paper, Scissors" against the computer. The program should ask the user to choose one of the three choices, and then the computer randomly picks one (without knowing what the user has chosen). For this problem, the user should be asked to enter an integer: 0 for rock, 1 for paper, or 2 for scissors. Rock beats scissors, scissors beats paper, paper beats rock. The program should say who wins, and then keep playing until someone (the user or the computer) has won 10 rounds. The computer needs to keep track of the current score and should also display it before each round.
import java.util.Scanner;
public class rock_paper
{
public static void main(String args[])
{
int count_win=0;
int user_choice;
String choice=" ";
int num_comp_wins=0;
int num_user_wins=0;
while(num_comp_wins!=10||num_user_wins!=10)
{
/* Read user input and determine choice */
System.out.println("\n Let us play Rock Paper Scissors \n");
System.out.println("\n Enter your choice 0 for rock, 1 for paper, or 2 for scissors \n");
Scanner keyboard = new Scanner(System.in);
user_choice=keyboard.nextInt();
System.out.println("\n Your choice was \n"+user_choice);
if(user_choice==0)
{
choice="rock";
}
else if(user_choice==1)
{
choice="paper";
}
else if(user_choice==2)
{
choice="scissor";
}
else
{
System.out.println("Incorrect choice");
choice=null;
}
System.out.println(choice);
/* Randomly generate computer input */
Random rand = new Random();
int computer_choice = rand.nextInt(3) + 1;
String choice1="";
if(computer_choice==0)
{
choice1="rock";
}
else if(computer_choice==1)
{
choice1="paper";
}
else if(computer_choice==2)
{
choice1="scissor";
}
else
{
System.out.println("Incorrect choice");
choice1=null;
}
System.out.println(choice1);
/* Compare user and computer response*/
if (choice.equalsIgnoreCase("rock")) {
if (choice1.equalsIgnoreCase("scissor"))
{
System.out.println("\nThe user wins!");
num_user_wins++;
}
else if (choice1.equalsIgnoreCase("paper"))
{
System.out.println("\nThe computer wins!");
num_comp_wins++;
}
else
System.out.println("Tie");
}
else if (choice.equalsIgnoreCase("paper"))
{
if (choice1.equalsIgnoreCase("scissor"))
{
System.out.println("\nThe computer wins!");
num_comp_wins++;
}
else if (choice1.equalsIgnoreCase("rock"))
{
System.out.println("\nThe user wins!");
num_user_wins++;
}
else
System.out.println("Tie");
}
else if (choice.equalsIgnoreCase("scissor"))
{
if (choice1.equalsIgnoreCase("rock"))
{
System.out.println("\nThe computer wins!");
num_comp_wins++;
}
else if (choice1.equalsIgnoreCase("paper"))
{
System.out.println("\nThe user wins!");
num_user_wins++;
}
else
System.out.println("Tie");
}
}
}
}