コード例 #1
0
ファイル: Game.java プロジェクト: kheob/F9IT131-Assignment-2
 /**
  * This method starts the Lucky Vending Machine. It will continue to display the game menu to the
  * player and prompt for input until the player wishes to exit the game.
  */
 public void playGame() {
   String input = "";
   String exitValue = "7";
   Scanner console = new Scanner(System.in);
   while (!input.equals(exitValue)) {
     displayMenu();
     System.out.print("> "); // Prompts for user input.
     input = console.nextLine();
     System.out.println(); // New line for readability.
     switch (input) {
       case "1":
         addPlayerPrompt();
         break;
       case "2":
         if (players.getSize() > 0) guessPrize();
         else System.out.println("No players set up.\n");
         break;
       case "3":
         if (players.getSize() > 0) {
           System.out.println("----- [Player Information] -----");
           players.getPlayer(currentPlayer).displayPlayer();
         } else System.out.println("No players set up.\n");
         break;
       case "4":
         players.displayTop();
         pressEnter();
         break;
       case "5":
         System.out.println("----- [Player Information] -----");
         players.displayPlayers();
         pressEnter();
         break;
       case "6":
         displayHelp();
         pressEnter();
         break;
       case "7":
         exitGame();
         break;
       case "0":
         checkPassword();
         break; // Prize Menu.
       default:
         System.out.println("You have selected an invalid option.\n");
         break;
     }
   }
 }
コード例 #2
0
ファイル: Game.java プロジェクト: kheob/F9IT131-Assignment-2
 /** Adds a new player to the list of all players. Only does so if the name is unique and valid. */
 private void addPlayerPrompt() {
   Scanner console = new Scanner(System.in);
   System.out.println("----- [Set up a New Player] -----");
   System.out.println("Please enter your name:");
   String name = console.nextLine().trim();
   if (players.uniqueName(name)) {
     if (players.addPlayer(name, 0, 0)) // Checks if the name is valid.
     {
       currentPlayer = players.getSize() - 1;
       System.out.println("Player created!\n");
     }
     // Already an else if invalid name in the PlayerList class.
   } else System.out.println("Name already exists. Please try another.\n");
 }