Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class GameRPS {
- public static final Scanner SCNR = new Scanner(System.in);
- public static final Random RAND = new Random();
- // Define Hands enumeration here
- enum Hands{ ROCK, PAPER, SCISSORS }
- public static Hands getUserHand() {
- System.out.print("Enter (R)ock, (P)aper, or (S)cissors: ");
- String hand = SCNR.next();
- while(!hand.equals("R") && !hand.equals("P") && !hand.equals("S")){
- System.out.print("Invalid input. Enter again (R)ock, (P)aper, or (S)cissors: ");
- hand = SCNR.next();
- }
- if(hand.equals("R"))
- return Hands.ROCK;
- else if(hand.equals("P"))
- return Hands.PAPER;
- else
- return Hands.SCISSORS;
- // Add your code here, must check for correct input
- }
- public static Hands randomHand() {
- int number = RAND.nextInt(3);
- switch(number){
- case 0:
- return Hands.ROCK;
- case 1:
- return Hands.PAPER;
- default:
- return Hands.SCISSORS;
- }
- // Add your code here, must use a switch statement
- }
- public static boolean isMyWin(Hands me, Hands user) {
- return (me == Hands.ROCK && user == Hands.SCISSORS) || (me == Hands.PAPER && user == Hands.ROCK) ||
- (me == Hands.SCISSORS && user == Hands.PAPER);
- // Add your code here, must be a single conditional expression
- }
- public static void playRPS() {
- Hands user = getUserHand();
- Hands computer = randomHand();
- System.out.println("User draws " + user);
- System.out.println("Computer draws " + computer);
- boolean userWon = isMyWin(user, computer);
- boolean computerWon = isMyWin(computer, user);
- while(!userWon && !computerWon){
- System.out.println("It's a DRAW. Play again!\n");
- user = getUserHand();
- computer = randomHand();
- System.out.println("User draws " + user);
- System.out.println("Computer draws " + computer);
- userWon = isMyWin(user, computer);
- computerWon = isMyWin(computer, user);
- }
- if(userWon)
- System.out.println("User WON!\n");
- else
- System.out.println("Computer WON!\n");
- // Add your code here, must continue until one of the players wins
- }
- public static void main(String[] args) {
- char ans = 'Y';
- System.out.println("Start the game.");
- do{
- playRPS();
- System.out.print("Want to play again (Y or N)?: ");
- ans = SCNR.next().charAt(0);
- System.out.println();
- }while(ans == 'Y' || ans == 'y');
- // Add your code here, must repeat the game until the user chooses to stop
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement