Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import java.util.*;
  2. public class GameRPS {
  3. public static final Scanner SCNR = new Scanner(System.in);
  4. public static final Random RAND = new Random();
  5. // Define Hands enumeration here
  6. enum Hands{ ROCK, PAPER, SCISSORS }
  7.  
  8. public static Hands getUserHand() {
  9. System.out.print("Enter (R)ock, (P)aper, or (S)cissors: ");
  10. String hand = SCNR.next();
  11. while(!hand.equals("R") && !hand.equals("P") && !hand.equals("S")){
  12. System.out.print("Invalid input. Enter again (R)ock, (P)aper, or (S)cissors: ");
  13. hand = SCNR.next();
  14. }
  15. if(hand.equals("R"))
  16. return Hands.ROCK;
  17. else if(hand.equals("P"))
  18. return Hands.PAPER;
  19. else
  20. return Hands.SCISSORS;
  21. // Add your code here, must check for correct input
  22.  
  23. }
  24.  
  25. public static Hands randomHand() {
  26. int number = RAND.nextInt(3);
  27. switch(number){
  28. case 0:
  29. return Hands.ROCK;
  30. case 1:
  31. return Hands.PAPER;
  32. default:
  33. return Hands.SCISSORS;
  34. }
  35. // Add your code here, must use a switch statement
  36.  
  37. }
  38. public static boolean isMyWin(Hands me, Hands user) {
  39. return (me == Hands.ROCK && user == Hands.SCISSORS) || (me == Hands.PAPER && user == Hands.ROCK) ||
  40. (me == Hands.SCISSORS && user == Hands.PAPER);
  41. // Add your code here, must be a single conditional expression
  42.  
  43. }
  44.  
  45. public static void playRPS() {
  46. Hands user = getUserHand();
  47. Hands computer = randomHand();
  48. System.out.println("User draws " + user);
  49. System.out.println("Computer draws " + computer);
  50. boolean userWon = isMyWin(user, computer);
  51. boolean computerWon = isMyWin(computer, user);
  52. while(!userWon && !computerWon){
  53. System.out.println("It's a DRAW. Play again!\n");
  54. user = getUserHand();
  55. computer = randomHand();
  56. System.out.println("User draws " + user);
  57. System.out.println("Computer draws " + computer);
  58. userWon = isMyWin(user, computer);
  59. computerWon = isMyWin(computer, user);
  60. }
  61. if(userWon)
  62. System.out.println("User WON!\n");
  63. else
  64. System.out.println("Computer WON!\n");
  65.  
  66. // Add your code here, must continue until one of the players wins
  67.  
  68. }
  69.  
  70. public static void main(String[] args) {
  71. char ans = 'Y';
  72. System.out.println("Start the game.");
  73. do{
  74. playRPS();
  75. System.out.print("Want to play again (Y or N)?: ");
  76. ans = SCNR.next().charAt(0);
  77. System.out.println();
  78. }while(ans == 'Y' || ans == 'y');
  79. // Add your code here, must repeat the game until the user chooses to stop
  80.  
  81.  
  82. }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement