Advertisement
Guest User

Untitled

a guest
Sep 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4. *
  5. * @author
  6. * Section 2
  7. */
  8. public class RockPaperScissors{
  9.  
  10. //these are class constants (all methods in the class can see them)
  11. static final int ROCK = 1, PAPER = 2, SCISSORS = 3, YOU_WIN = 4, COMP_WIN = 5, DRAW = 0;
  12.  
  13. public static void main(String[] args) {
  14. Scanner scan = new Scanner(System.in);
  15. String userInput;
  16. int yourScore=0, compScore=0;
  17. int gameResult;
  18.  
  19. System.out.println("========================");
  20. System.out.println("ROCK PAPER SCISSORS GAME");
  21. System.out.println("========================");
  22. System.out.println("\nHello! Let's get started!");
  23.  
  24. do {
  25. //calling the playGame method
  26. gameResult = playGame(scan);
  27.  
  28. if (gameResult == YOU_WIN)
  29. yourScore++;
  30. else if(gameResult == COMP_WIN)
  31. compScore++;
  32.  
  33. printScore(yourScore,compScore);
  34. System.out.println(" You : Comp");
  35. System.out.println("Score: -----------");
  36. System.out.println(" | "+yourScore+" : "+compScore+" |");
  37. System.out.print("Do you want to play some more? (y or n): ");
  38. userInput = scan.next();
  39.  
  40. } while (userInput.equalsIgnoreCase("y") || userInput.equalsIgnoreCase("yes"));
  41.  
  42. System.out.println("========GAME OVER=======");
  43. System.out.println(" FINAL SCORE:");
  44. printScore(yourScore,compScore);
  45. }
  46.  
  47.  
  48. /**
  49. This method "plays" the game.
  50. The input parameter is the Scanner object created in main()
  51. The method return the int result of the game.
  52. */
  53. private static int playGame(Scanner scan) {
  54. int yourChoice, compChoice;
  55. //1. Prompt the user and use scan to read her input (R, P, or S)
  56. System.out.println("Rock, Paper, or Scissors? (R,P,S):");
  57. String usermove = scan.nextLine();
  58.  
  59. //2. Use a while loop to continue asking for user input if it is not
  60. // one of the three above ("r", "p", or "s", ignoring case)
  61. while(!usermove.equalsIgnoreCase("r") || !usermove.equalsIgnoreCase("p") || !usermove.equalsIgnoreCase("s"));
  62. System.out.println(usermove+" Is not suported. Try again");
  63. usermove = scan.nextLine();
  64.  
  65. //3. Once the user input is satisfactory
  66. //print out the choice (e.g. "You chose Rock!"),
  67. //translate it to one of the int constants (ROCK, PAPER, or SCISSORS),
  68. //and assign to yourChoice variable (e.g. yourChoice = ROCK)
  69. if(usermove.equalsIgnoreCase("R")){
  70. System.out.println("You chose Rock!");
  71. yourChoice= ROCK;
  72. }
  73. else if (usermove.equalsIgnoreCase("P")){
  74. System.out.println("You chose Paper!");
  75. yourChoice= PAPER;
  76. }
  77. else if (usermove.equalsIgnoreCase("S")){
  78. System.out.println("You chose Scissors!");
  79. yourChoice= SCISSORS;
  80. }
  81.  
  82. //4. Use Math.random() to randomly generate computer's choice
  83. //Remember, you need to split the range between 0 and 1 into
  84. // three equal intervals and if the random number is in one of them
  85. // assign a corresponding constant to compChoice.
  86. //Print out computer's choice as well.
  87. double x= Math.random();
  88. if (x<.33){
  89. System.out.println("Computer Chose Rock!");
  90. compChoice= ROCK;
  91. }
  92. else if (x>.66){
  93. System.out.println("Computer Chose Paper!");
  94. compChoice= PAPER;
  95. }
  96. else{
  97. System.out.println("Computer Chose Scissors!");
  98. compChoice= SCISSORS;
  99. }
  100. //Call the method whoWins using yourChoice and compChoice as parameters-----------
  101. int gameResult = whoWins(result);
  102.  
  103. //return the game result to main()------------------------------------------------
  104. return gameResult;
  105. }
  106.  
  107.  
  108. /**
  109. This method takes two int parameters yourChoice and compChoice,
  110. and returns the result of the game.
  111.  
  112. It will return YOU_WIN if you win, COMP_WIN if computer wins
  113. and DRAW if it is a draw. (Remember, these are constants defined in this class)
  114.  
  115. In addition, the method prints out the corresponding description of
  116. the result of the game e.g. "It's a draw!!",
  117. or "Scissors cut Paper! You WIN!".
  118. or "Rock is covered by Paper! You lose :(", and so on.
  119. */
  120. private static int whoWins(int yourChoice, int compChoice){
  121. int result;
  122. if(yourChoice==ROCK && compChoice==SCISSORS){
  123. System.out.println("Rock smashes Scissors! You WIN!");
  124. result= YOU_WIN;
  125. }
  126. else if(yourChoice==ROCK && compChoice==PAPER){
  127. System.out.println("Paper wraps Rock! You LOSE!");
  128. result= COMP_WIN;
  129. }
  130. else if(yourChoice==ROCK && compChoice==ROCK){
  131. System.out.println("Rock hits Rock! It's a DRAW!");
  132. result= DRAW;
  133. }
  134. if(yourChoice==PAPER && compChoice==SCISSORS){
  135. System.out.println("Scissors gets cut! You LOSE!");
  136. result= COMP_WIN;
  137. }
  138. else if(yourChoice==PAPER && compChoice==PAPER){
  139. System.out.println("Both Papers fold! It's a DRAW!");
  140. result= DRAW;
  141. }
  142. else if (yourChoice==PAPER && compChoice==ROCK){
  143. System.out.println("Paper wraps Rock! You LOSE!");
  144. result= YOU_WIN;
  145. }
  146.  
  147. if(yourChoice==SCISSORS && compChoice==SCISSORS){
  148. System.out.println("Both SCissors fold! It's a DRAW!");
  149. result= DRAW;
  150. }
  151. else if(yourChoice==SCISSORS && compChoice==PAPER){
  152. System.out.println("Scissors cuts Paper! You WIN!");
  153. result= YOU_WIN;
  154. }
  155. else if(yourChoice==SCISSORS && compChoice==ROCK){
  156. System.out.println("Rock smashes Scissors! You LOSE!");
  157. result= COMP_WIN;
  158. }
  159. return result;
  160. }
  161.  
  162. /**
  163. This method takes in yourScore and compScore as parameters and prints
  164. them out in a formatted way, as shown in the project description.
  165. */
  166. private static void printScore(int yourScore, int compScore){
  167. System.out.println(" You : Comp");
  168. System.out.println("Score: -----------");
  169. System.out.println(" | "+yourScore+" : "+compScore+" |");
  170. return;
  171. }
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement