fork(3) download
  1. import java.util.Arrays;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. /**
  6.  * Rock Paper Scissors Lizard Spock
  7.  * <p>
  8.  * http://e...content-available-to-author-only...a.org/wiki/Rock-paper-scissors-lizard-Spock
  9.  * <p>
  10.  * Interface for a human to play against the computer.
  11.  */
  12. class Ideone {
  13.  
  14. /**
  15.   * Set up the rules for the game.
  16.   * What are the moves, and what beats what!
  17.   */
  18. public enum Move {
  19. Rock, Paper, Scissors, Lizard, Spock;
  20.  
  21. static {
  22. Rock.willBeat(Scissors, Lizard);
  23. Paper.willBeat(Rock,Spock);
  24. Scissors.willBeat(Paper, Lizard);
  25. Lizard.willBeat(Spock,Paper);
  26. Spock.willBeat(Rock,Scissors);
  27. }
  28.  
  29. // what will this move beat - populated in static initializer
  30. private Move[] ibeat;
  31. private void willBeat(Move...moves) {
  32. ibeat = moves;
  33. }
  34.  
  35. /**
  36.   * Return true if this Move will beat the supplied move
  37.   * @param move the move we hope to beat
  38.   * @return true if we beat that move.
  39.   */
  40. public boolean beats(Move move) {
  41. // use binary search in case someone wants to set up crazy rules.
  42. return Arrays.binarySearch(ibeat, move) >= 0;
  43. }
  44.  
  45. }
  46.  
  47. // This is a prompt that is set up just once per JVM
  48. private static final String MOVEPROMPT = buildOptions();
  49. private static String buildOptions() {
  50. StringBuilder sb = new StringBuilder();
  51. sb.append(" -> ");
  52. // go through the possible moves, and make a prompt string.
  53. for (Move m : Move.values()) {
  54. sb.append(m.ordinal() + 1).append(") ").append(m.name()).append(" ");
  55. }
  56. // include a quit option.
  57. sb.append(" q) Quit");
  58. return sb.toString();
  59. }
  60.  
  61. /**
  62.   * get some input from the human.
  63.   * @param scanner What we read the input from.
  64.   * @param prompt What we prompt the user for.
  65.   * @param defval If the user just presses enter, what do we return.
  66.   * @return the value the user entered (just the first char of it).
  67.   */
  68. private static final char prompt(Scanner scanner, String prompt, char defval) {
  69. // prompt the user.
  70. System.out.print(prompt + ": ");
  71. // it would be nice to use a Console or something, but running from Eclipse there isn't one.
  72. String input = scanner.nextLine();
  73. if (input.isEmpty()) {
  74. return defval;
  75. }
  76. return input.charAt(0);
  77. }
  78.  
  79. /**
  80.   * Simple conditional that prompts the user to play again, and returns true if we should.
  81.   * @param scanner the scanner to get the input from
  82.   * @return true if the user wants to continue.
  83.   */
  84. private static boolean playAgain(Scanner scanner) {
  85. return ('n' != prompt(scanner, "\nPlay Again (y/n)?", 'y'));
  86. }
  87.  
  88. /**
  89.   * Prompt the user for a move.
  90.   * @param scanner The scanner to get the move from
  91.   * @return the move the user wants to do.
  92.   */
  93. private static Move getHumanMove(Scanner scanner) {
  94. // loop until we get some valid input.
  95. do {
  96. char val = prompt(scanner, MOVEPROMPT, 'q');
  97. if ('q' == val) {
  98. // user does not want to make a move... or just presses enter too fast.
  99. return null;
  100. }
  101. int num = (val - '0') - 1;
  102. if (num >= 0 && num < Move.values().length) {
  103. // we got valid input. Return.
  104. return Move.values()[num];
  105. }
  106. System.out.println("Invalid move " + val);
  107. } while (true);
  108. }
  109.  
  110. /**
  111.   * Run the game.... Good Luck!
  112.   * @param args these are ignored.
  113.   */
  114. public static void main(String[] args) {
  115. final Random rand = new Random();
  116. final Move[] moves = Move.values();
  117. final Scanner scanner = new Scanner(System.in);
  118. int htotal = 0;
  119. int ctotal = 0;
  120. do {
  121. System.out.println("\nBest of 3.... Go!");
  122. int hscore = 0;
  123. int cscore = 0;
  124. bestofthree: do {
  125. final Move computer = moves[rand.nextInt(moves.length)];
  126. final Move human = getHumanMove(scanner);
  127. if (human == null) {
  128. System.out.println("Human quits Best-of-3...");
  129. // quit the best-of-three loop
  130. break bestofthree;
  131. }
  132. if (human == computer) {
  133. System.out.printf(" DRAW... play again!! (%s same as %s)\n", human, computer);
  134. } else if (human.beats(computer)) {
  135. hscore++;
  136. System.out.printf(" HUMAN beats Computer (%s beats %s)\n", human, computer);
  137. } else {
  138. cscore++;
  139. System.out.printf(" COMPUTER beats Human (%s beats %s)\n", computer, human);
  140. }
  141. // play until someone scores 2....
  142. } while (hscore != 2 && cscore != 2);
  143.  
  144. // track the total scores.
  145. if (hscore == 2) {
  146. htotal++;
  147. } else {
  148. // perhaps the human quit while ahead, computer wins that too.
  149. ctotal++;
  150. }
  151.  
  152. String winner = hscore == 2 ? "Human" : "Computer";
  153. System.out.printf("\n %s\n **** %s wins Best-Of-Three (Human=%d, Computer=%d - game total is Human=%d Computer=%d)\n",
  154. winner.toUpperCase(), winner, hscore, cscore, htotal, ctotal);
  155.  
  156. // Shall we play again?
  157. } while (playAgain(scanner));
  158.  
  159. System.out.printf("Thank you for playing. The final game score was Human=%d and Computer=%d\n", htotal, ctotal);
  160.  
  161. }
  162.  
  163. }
Success #stdin #stdout 0.13s 380672KB
stdin
1
1
1
1
1
1
1
1
1
1
1
1
1

q
n
stdout
Best of 3.... Go!
     -> 1) Rock 2) Paper 3) Scissors 4) Lizard 5) Spock  q) Quit:   DRAW... play again!! (Rock same as Rock)
     -> 1) Rock 2) Paper 3) Scissors 4) Lizard 5) Spock  q) Quit:   COMPUTER beats Human (Paper beats Rock)
     -> 1) Rock 2) Paper 3) Scissors 4) Lizard 5) Spock  q) Quit:   HUMAN beats Computer (Rock beats Lizard)
     -> 1) Rock 2) Paper 3) Scissors 4) Lizard 5) Spock  q) Quit:   HUMAN beats Computer (Rock beats Lizard)

 HUMAN
 **** Human wins Best-Of-Three (Human=2, Computer=1 - game total is Human=1 Computer=0)

Play Again (y/n)?: 
Best of 3.... Go!
     -> 1) Rock 2) Paper 3) Scissors 4) Lizard 5) Spock  q) Quit:   COMPUTER beats Human (Spock beats Rock)
     -> 1) Rock 2) Paper 3) Scissors 4) Lizard 5) Spock  q) Quit:   HUMAN beats Computer (Rock beats Scissors)
     -> 1) Rock 2) Paper 3) Scissors 4) Lizard 5) Spock  q) Quit:   COMPUTER beats Human (Paper beats Rock)

 COMPUTER
 **** Computer wins Best-Of-Three (Human=1, Computer=2 - game total is Human=1 Computer=1)

Play Again (y/n)?: 
Best of 3.... Go!
     -> 1) Rock 2) Paper 3) Scissors 4) Lizard 5) Spock  q) Quit:   HUMAN beats Computer (Rock beats Lizard)
     -> 1) Rock 2) Paper 3) Scissors 4) Lizard 5) Spock  q) Quit:   COMPUTER beats Human (Paper beats Rock)
     -> 1) Rock 2) Paper 3) Scissors 4) Lizard 5) Spock  q) Quit:   DRAW... play again!! (Rock same as Rock)
     -> 1) Rock 2) Paper 3) Scissors 4) Lizard 5) Spock  q) Quit:   HUMAN beats Computer (Rock beats Scissors)

 HUMAN
 **** Human wins Best-Of-Three (Human=2, Computer=1 - game total is Human=2 Computer=1)

Play Again (y/n)?: 
Best of 3.... Go!
     -> 1) Rock 2) Paper 3) Scissors 4) Lizard 5) Spock  q) Quit: Human quits Best-of-3...

 COMPUTER
 **** Computer wins Best-Of-Three (Human=0, Computer=0 - game total is Human=2 Computer=2)

Play Again (y/n)?: Thank you for playing. The final game score was Human=2 and Computer=2