Advertisement
Guest User

Untitled

a guest
Oct 20th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. package com.mycompany.rockpapperscissor;
  2.  
  3. import java.util.Random;
  4.  
  5. /**
  6. * @author Roner Reked
  7. * Handles computers move.
  8. */
  9. public class Computer {
  10.  
  11. /**
  12. * @return a random object from the enum RPS,
  13. * which contains ROCK, PAPER or SCISSORS.
  14. */
  15. public RPS getHand() {
  16. RPS[] moves = RPS.values();
  17. Random random = new Random();
  18. int index = random.nextInt(moves.length);
  19. return moves[index];
  20. }
  21. }
  22.  
  23.  
  24. package com.mycompany.rockpapperscissor;
  25.  
  26. /**
  27. *
  28. * @author Roner Reked
  29. */
  30. public class GameRunner {
  31.  
  32. Player human = new Player();
  33. Computer computer = new Computer();
  34.  
  35. private int playerWonGames = 0,
  36. playerLostGames = 0,
  37. numberOfGames = 0;
  38.  
  39. /**
  40. *
  41. */
  42. public void runGame() {
  43. RPS playerHand = human.getHand(); // "Hand" = Rock, paper or scissors.
  44. RPS computerHand = computer.getHand();
  45. System.out.println("\nYou played " + playerHand + ".");
  46. System.out.println("Computer played " + computerHand + ".\n");
  47. int comapreHands = playerHand.compareHands(computerHand);
  48. switch (comapreHands) {
  49. case 0: // Tie
  50. System.out.println("Tie!");
  51. break;
  52. case 1: // Player wins
  53. System.out.println(playerHand + " beats " + computerHand + ". You won!");
  54. playerWonGames++;
  55. break;
  56. case -1: // Computer wins
  57. System.out.println(computerHand + " beats " + playerHand + ". You lost.");
  58. playerLostGames++;
  59. break;
  60. }
  61. numberOfGames++;
  62. if (human.playAgain()) {
  63. runGame();
  64. } else {
  65. printStats();
  66. }
  67. }
  68.  
  69. /**
  70. * Prints out the number of games you've played.
  71. * Calculates how many games you have tied,
  72. * also prints out won games and lost games.
  73. */
  74. public void printStats() {
  75. final int tiedGames = numberOfGames - playerWonGames - playerLostGames;
  76. System.out.printf("| %6s | %6s | %6s | %12s |\n",
  77. "WINS", "LOSSES", "TIES", "GAMES PLAYED");
  78.  
  79. System.out.printf("| %6d | %6d | %6d | %12d |\n",
  80. playerWonGames, playerLostGames, tiedGames, numberOfGames);
  81. }
  82.  
  83. /*public void printStats() {
  84. StringBuilder sb = new StringBuilder();
  85. final int tiedGames = numberOfGames - playerWonGames - playerLostGames;
  86. sb.append("[")
  87. .append("Wins: ")
  88. .append(playerWonGames)
  89. .append(" Losses: ")
  90. .append(playerLostGames)
  91. .append(" Ties: ")
  92. .append(tiedGames)
  93. .append(" Games Played: ")
  94. .append(numberOfGames)
  95. .append("]");
  96. System.out.println(sb);
  97. }*/
  98. }
  99.  
  100. package com.mycompany.rockpapperscissor;
  101.  
  102. import java.util.Scanner;
  103.  
  104. /**
  105. * @author Roner Reked
  106. *
  107. */
  108. public class Player {
  109.  
  110. Scanner userInput = new Scanner(System.in);
  111.  
  112. /**
  113. * The user gets to type in rock, paper or scissor.
  114. *
  115. * @return a object from the Enum "RPS"
  116. * which in this case is ROCK, PAPER or SCISSORS.
  117. */
  118. public RPS getHand() {
  119. // Prompt the user
  120. System.out.print("Rock, paper, or scissors? ");
  121.  
  122. // Get the user input
  123. String playerMove = userInput.nextLine().toUpperCase().replaceAll(" ", "");
  124. char firstLetter = playerMove.charAt(0);
  125. if (playerMove.startsWith("R") || playerMove.startsWith("P") || playerMove.startsWith("S")) {
  126. // User has entered a valid input
  127. switch (firstLetter) {
  128. case 'R':
  129. return RPS.ROCK;
  130. case 'P':
  131. return RPS.PAPER;
  132. case 'S':
  133. return RPS.SCISSORS;
  134. }
  135. }
  136. // User has not entered a valid input. Try again.
  137. System.out.println("Wrong input!");
  138. return getHand();
  139. }
  140.  
  141. /**
  142. * @return true if the player inputs a Y as a first letter.
  143. */
  144. public boolean playAgain() {
  145. System.out.print("Do you want to play again? ");
  146. String yesOrNo = userInput.nextLine().toUpperCase();
  147. return yesOrNo.charAt(0) == 'Y';
  148. }
  149. }
  150.  
  151. package com.mycompany.rockpapperscissor;
  152.  
  153. /**
  154. * @author Roner Reked
  155. * Holds our objects and compare method for Rock paper scissors game.
  156. * RPS = Rock Paper Scissors.
  157. */
  158. public enum RPS {
  159.  
  160. ROCK,
  161. PAPER,
  162. SCISSORS;
  163.  
  164. /**
  165. * Compares the player and computer hands
  166. * to calculate a tie, win or loss.
  167. *
  168. * @param hand hand to compare to.
  169. * @return 1 If this > hand.
  170. * If it's a tie return 0.
  171. * If hand > this return -1.
  172. */
  173. public int compareHands(RPS hand) {
  174. if (this == hand) { // Tied game.
  175. return 0;
  176. }
  177.  
  178. switch (this) {
  179. case ROCK:
  180. return (hand == SCISSORS ? 1 : -1);
  181. case PAPER:
  182. return (hand == ROCK ? 1 : -1);
  183. case SCISSORS:
  184. return (hand == PAPER ? 1 : -1);
  185. }
  186. return 0; // Never reaches this.
  187. }
  188. }
  189.  
  190.  
  191. package com.mycompany.rockpapperscissor;
  192.  
  193. /**
  194. *
  195. * @author Roner Reked
  196. */
  197. public class RockPaperScissors {
  198.  
  199. public static void main(String[] args) {
  200.  
  201. GameRunner game = new GameRunner();
  202. game.runGame();
  203.  
  204. }
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement