/** * This method displays the in-game help menu. It includes a table of all the prizes currently in * the machine. */ private void displayHelp() { System.out.println( "----- [Display Game Help] -----\n" + "The objective of the Lucky Vending Machine game is for the\n" + "player to guess a number that corresponds to a prize. A\n" + "list of all the prizes available and its cost and worth is\n" + "shown below.\n"); if (prizes.getSize() > 0) prizes.displayPrizes(); else System.out.println("[No prizes currently in the system.]"); System.out.println( "\nIf the player's guess matches the number generated by the\n" + "machine, they win the prize.\n\n" + "To begin, set up a new player with [1] and then guess with\n" + "[2].\n\n" + "If you are the administrator, type [0] in the main menu to\n" + "access the prize configuration menu."); }
/** Reads a password from the user and checks if it matches. */ private void checkPassword() { Scanner console = new Scanner(System.in); System.out.print("Administrator password: "******"Success!\n"); prizes.displayMenu(); } else System.out.println("Password is incorrect.\n"); }
/** * This method prompts the user for a guess, checks that it is valid, and then compares it to a * random lucky number. If both numbers match, the Player wins the Prize. */ private void guessPrize() { Scanner console = new Scanner(System.in); System.out.println( "----- [Guess a Prize] -----\n" + "Please guess a number between 1 and " + prizes.getSize() + ":"); int guess = 0; while (!validGuess(guess)) { System.out.print("> "); try { guess = console.nextInt(); if (validGuess(guess)) compareLuckyNumber(guess); else System.out.println( "Error! Please guess a number between 1 and " + prizes.getSize() + ":"); } catch (Exception e) { System.out.println("Error! Invalid guess.\n"); break; } } }
/** * Compares the number given with a random lucky number. If they match, the Player object will be * updated with details of the new Prize they have won. * * @param guess The number that is to be compared. */ private void compareLuckyNumber(int guess) { System.out.println("You have guessed: " + guess); int randomNumber = LuckyGuessGenerator.generateRandomNumber(prizes.getSize()); System.out.println("The lucky number is: " + randomNumber); if (guess == randomNumber) { players.getPlayer(currentPlayer).addPrize(prizes.getPrize(guess - 1)); System.out.println( "Congratulations! you have won a " + prizes.getPrize(guess - 1).getName() + " worth $" + prizes.getPrize(guess - 1).getWorth() + "!\n"); players.getPlayer(currentPlayer).addWorth(prizes.getPrize(guess - 1).getWorth()); } else System.out.println("Too bad! You didn't win anything.\n"); players.getPlayer(currentPlayer).addSpent(prizes.getPrize(guess - 1).getCost()); }
/** * This method checks that a guess is valid. * * @param guess The guess that needs to be validated. * @return True if the guess is valid. */ private boolean validGuess(int guess) { boolean isValid = false; if (guess >= 1 && guess <= prizes.getSize()) isValid = true; return isValid; }
/** Stops the Lucky Vending Machine game. */ private void exitGame() { System.out.println("----- [Exit Game] -----"); System.out.println("The game will now exit. Have a nice day."); players = new PlayerList(); // Deletes all players. prizes.writePrizes("prizes.txt"); // Writes all data from the PrizeList to a file. }