package com.javasm.exerices02; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Scanner; /** * *TODO Rock-paper-scissors box-guessing game, best of three games * @author caolei May 2, 2018 at 10:51:23 PM * RockPaperScissors */ public class RockPaperScissors { // used to store the guessing result private static List<String> gameResult; public static void crGuessingGame(Scanner sc) { gameResult = new ArrayList<String>(); // store the result set char[] finger = { 'hammer', 'scissors', 'bag' }; // used to receive each guessing result String result; // for computer guessing Random random = new Random(); // record the number of computer wins int computerWinNumber = 0; // Count how many times the player has won int playerWinNumber = 0; // record the number of rounds of guessing int i = 0; // loop punch while (true) { System.out.println("The first" + (i + 1) + "round, please punch: "); // computer generated random number int computerGet = random.nextInt(3); // the number entered by the user int playerInput = sc.nextInt(); // record the guessing result result = finger[computerGet] + "," + finger[playerInput]; // put the result into the collection gameResult.add(result); /* * The first is to judge the result by if * * if (computerGet == 0 && playerInput != 0) { * if (playerInput == 1) * computerWinNumber++; * playerWinNumber++; * * } else if (computerGet == 1 && playerInput != 1) { * if (playerInput == 2) * computerWinNumber++; * playerWinNumber++; * } else if (computerGet == 2 && playerInput != 2) { * if (playerInput == 0) * computerWinNumber++; * playerWinNumber++; * } */ /** * The second is to judge the result by switch */ switch (computerGet) { case 0: if (playerInput == 1) computerWinNumber++; if (playerInput == 2) playerWinNumber++; break; case 1: if (playerInput == 2) computerWinNumber++; if (playerInput == 0) playerWinNumber++; break; case 2: if (playerInput == 0) computerWinNumber++; if (playerInput == 1) playerWinNumber++; break; default: break; } // record the number of game rounds i++; // Determine if anyone wins if (computerWinNumber == 2 || playerWinNumber == 2) { break; } } // determine the winner if (computerWinNumber > playerWinNumber) { System.out.println("Computer wins!"); } else { System.out.println("Congratulations on your victory!"); } // output the guessing result System.out.println("The specific results are as follows:"); System.out.println("computer, you"); for (String results : gameResult) { System.out.println("{ " + results + " }"); } } }