Write a program that lets the user play the game of Rock, Paper, Scissors agains
ID: 3537932 • Letter: W
Question
Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows.
1. When the program beings, a random number in the range of 1-3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors (dont display the computers choice yet).
2. The user enters his or her choice of "rock, paper or scissors" at the keyboard. (You can use a menue if you prefer).
3. The computers choice is displayed.
4. A winner is selected according to the followings rules:
- If one player chooses rock and the other player chooses scissors, then rock wins. (The rock smashes scissors).
- If one player chooses scissors and the other player chooses paper, then the scissors wine. (Scissors cuts paper).
- If one plaer chooses paper and the other chooses rock, then paper wins. (Paper wraps rock).
- If both players make the came choice, the game must be player again to determine the winner.
Be sure to divid the program into mehtods that perform each major task.
Explanation / Answer
please rate - thanks
(I'm trusting you to do the right ting, and rate)
import java.util.*;
public class rockpaperscissor
{public static void main(String[] args)
{Scanner in = new Scanner(System.in);
int usermove;
int computermove;
Random r=new Random();
do
{
computermove=computer(r);
usermove= user(in);
displayComputer(computermove);
}while(score(usermove,computermove));
}
public static int user(Scanner in)
{int move=0;
System.out.print("Enter 1=Rock 2-Paper 3-Scissors >> ");
move=in.nextInt();
while(move<1||move>3)
{System.out.print("Enter 1=Rock 2-Paper 3-Scissors >> ");
move=in.nextInt();
}
return move;
}
public static void displayComputer(int move)
{System.out.print("I chose ");
if(move==1)
System.out.println("Rock");
else if(move==2)
System.out.println("paper");
else
System.out.println("Scissors");
}
public static int computer(Random r)
{int move=r.nextInt(3)+1;
return move;
}
public static boolean score(int user,int computer )
{
if(user==computer)
{System.out.println("Tie!");
return true;
}
else if(user==1&&computer==3)
System.out.println("You smash my sissors. you win!");
else if(user==1&&computer==2)
System.out.println("I cover your rock. you lose!");
else if(user==2&&computer==3)
System.out.println("I cut your paper. you lose!");
else if(user==2&&computer==1)
System.out.println("You cover my rock. you win!");
else if(user==3&&computer==1)
System.out.println("I smash your sissors, you lose!");
else
System.out.println("You cut my paper. you win!");
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.