import java.util.Scanner; import java.util.random.RandomGenerator; import java.util.Random;
public class GuessingGame {
//The amount of guesses they have
private static final int MAX_GUESSES = 7;
// The secret number
private static final int SECRET_NUMBER = getRandomInt();
// The range they can guess from
private static final int MIN_NUM =-64;
private static final int MAX_NUM = 64;
//where we will be storeing guesses for the long term
private static int[] guessArray = new int[MAX_GUESSES];
// variable that users guesses go to then they get put in the array
private static int guessContainer;
private void ggRules()
{
System.out.println();
System.out.println("----------------------------- Welcome -------------------------------");
System.out.println("\n\tRules:\n ");
System.out.println("\t1) You have 7 gesses");
System.out.println("\t2) If you guesses the wrong number, a hint will be displayed");
System.out.println("\t3) Your guess has to be between -64 and 64\n\n");
System.out.println("4) Good luck, and may the odds be in your favor! ");
System.out.println("---------------------------------------------------------------------");
System.out.println();
}
// Generating random number , and setting the range between -64 and +64
public static int getRandomInt() {
int max = MAX_NUM - MIN_NUM;
Random rand = new Random();
int zeroToMax = rand.nextInt(max + 1);
return zeroToMax + MIN_NUM;
}
// Method for storing user guesses in array
public static void storeGuess(int userGuess) {
guessArray[guessContainer] = userGuess;
guessContainer++;
}
// Shows previous guesses
public static void showoldGuesses(int[] arr) {
System.out.print("Your last guess(es): ");
for (int i = 0; i < guessContainer; i++) {
System.out.print(guessArray[i] + " ");
}
System.out.println();
}
// Tells them the amount they have left
public static void guessesRemaining(int guessesLeft) { System.out.println("You have " + guessesLeft + " guesses left."); } // Looks for a duplicate guess public static boolean checkduplicateGuesses (int userGuess){ for (int i = 0; i < guessContainer; i++){ if (guessArray[i] == userGuess){ System.out.println("You have already entered that number! Try again."); return true; } } return false; } // Tells them if they've reached the max amount of guesses public static boolean checkMaxGuesses() { if (guessContainer == MAX_GUESSES) { System.out.println("Sorry, you have reached the maximum number of guesses."); return true; } else { return false; } }
// Tells them if the won the game
public static void endGame() {
System.out.println("Congratulations you a winner, you guessed the correct number");
}
// Method to get a valid int from the user utilizing getInput entered via the keyboard
@SuppressWarnings({ "resource", "unused" }) // instruct the compiler to suppress leakage warning
\ im actally suppose to put private static boolean startGame() here But im having a hard time implementing the methods in this part of the program
private static int getInput() { Scanner sc = new Scanner(System.in); System.out.print("Enter a number between -64 and 64: "); int userGuess = sc.nextInt(); while (userGuess < MIN_NUM || userGuess > MAX_NUM) { System.out.println("Sorry, that's not a valid number. Please enter a number between -64 and 64: "); userGuess = sc.nextInt();
// Giving the user hints if they guess the wrong number
if (userGuess > SECRET_NUMBER) {
System.out.println("Hint: The number you are looking for is lower than your guess.");
}
else if (userGuess < SECRET_NUMBER ) {
System.out.println("Hint: The number you are looking for is higher than your guess.");
}
}
return userGuess;
}}