<?php
package gamefiles;
/*Author: Karil Votek
* Purpose: Class simulates a number guessing game.
* Summary: Random number is selected->user guesses what number is->program tells if they are correct/higher/or lower
*/
import java.util.Random;
import java.util.Scanner;
public class HigherLower {
/*Private variables:
* randomNumber - Holds the random number generated by the java.util.Random class
* playerGuess - Used to store the value the player enters->See passGuess()
* attempts - Keeps track of amount of times player has guessed for correct number
* rand - Instance of Random class that is used to 'randomly' choose a number 1-100
* input - Instance of Scanner class that is used to receive user input for game
* started - Boolean used to signal player has guessed once->See passGuess()
* isHigher - Boolean used when deciding which message player receives->See run() and passGuess()
*/
private int randomNumber, playerGuess, attempts;
private Random rand = new Random();
private Scanner input = new Scanner(System.in);
private boolean started = false;
private boolean isHigher;
String playAgain, winningMessage;
//Method used to run the methods below (can see as gear that gets other gears moving)
public void run() {
//Making randomNumber a 'random' integer between 0 and 100
randomNumber = rand.nextInt(100);
//Loop used to keep player guessing until they are correct
while (playerGuess != randomNumber) {
//Setting playerGuess variable to the user's input->See passGuess()
playerGuess = passGuess();
//If the player's guess is higher than randomNumber, isHigher becomes false->See passGuess()
if (playerGuess > randomNumber) {
isHigher = false;
//Otherwise, isHigher is set to true (playerGuess is lower than randomNumber)->See passGuess()
} else {
isHigher = true;
}
//Increasing attempts by one because player tried to guess
attempts++;
}
//If player guesses correctly, loop exits to here.
winningMessage = String.format(
"You have won in %,d attempt(s)!\n", attempts);
System.out.println(winningMessage);
//Running tryAgain()->See tryAgain()
tryAgain();
}
//Method used to find out if player wishes to play again
private void tryAgain() {
//Setting playAgain to input from user
playAgain = passChoice();
//If player enters yes, reset attempt to 0, started to false, and starts run() to replay game
if (playAgain.equals("yes")) {
attempts = 0;
started = false;
run();
//Otherwise, game exits loop in Game class
} else {
System.out.println("Thank you for playing!\n");
//exitGame (Games static boolean) is set to true to break out of game loop
Games.exitGame = true;
}
}
//String method used for passing player's input on playing again
private String passChoice() {
System.out.println("Would you like to play again? (yes/no): ");
return input.nextLine().toLowerCase(); //Added toLowerCase() in case player uses caps
}
//String method used to pass player's guess
private int passGuess() {
//If this is player's first guess, sends this message to let them know number range.
if (started == false) {
System.out.println("Guess a number between 0-100: ");
//Setting started variable to true to skip this message next time
started = true;
//If this is not player's first guess and previous guess was lower than randomNumber, this gets called
} else if (started == true && isHigher == true) {
System.out.println("Guess a higher number: ");
//If this is not player's second guess and previous guess was higher than randomNumber, this gets called
} else if (started == true && isHigher == false) {
System.out.println("Guess a lower number: ");
}
//Returning player's input->see tryParse()
return tryParse(input.nextLine());
}
//Int method used to validate the player's input
private int tryParse(String string) {
//Creating a variable to pass value back
int value;
//Attempting to parse the player's input in a try/catch statement
try {
//Setting value to the input
value = Integer.parseInt(string);
//If player inserts value over 100, value is set to previous guess
if(value > 100) {
value = playerGuess;
System.out.println("Cannot guess higher than 100. \n");
//If input is lower than 0, value is set to previous guess
} else if(value < 0) {
value = playerGuess;
System.out.println("Cannot guess a number lower than 0. \n");
}
} catch (NumberFormatException e) {
//Value is set to previous guess if input cannot be parsed
value = playerGuess;
System.out.println("Please enter a number between 0-100 only");
}
//Returning the player's input/previous guess if mistakes were made
return value;
}
}