Reputation: 39
I have to make a random number guessing game, where each round I have have a limit of 10 guesses. The part that I am confused about is where I need to check if any of the guesses are repeated in that round. I know I have to make an array and store the numbers in there but I am not sure how to do that.
Here is my entire code, everything else works as intended:
import java.util.Scanner;
import java.util.Random;
public class GuessingGame {
private static final int MIN_NUMBER = 1;
private static final int MAX_NUMBER = 205;
private static final int QUIT_VALUE = -1;
private static final int LOSE_VALUE = -2;
private static final int MAX_GAMES = 2;
private static final int MAX_GUESSES = 10;
private static final int HINT_THRESHOLD = 5;
private static final int BACKDOOR_VALUE = -314;
private static final String NOPE_MSG = "nope...";
private static final String NOPE_NOPE_MSG =
"you've already guessed that wrong guess...";
private static final String INVALID_INPUT_BEGIN =
"*** invalid input -- ";
private static final String INVALID_INPUT_LESS_MIN_MSG =
INVALID_INPUT_BEGIN + "must be greater than " + (MIN_NUMBER - 1);
private static final String INVALID_INPUT_GREATER_MAX_MSG =
INVALID_INPUT_BEGIN + "must be less than " + (MAX_NUMBER + 1);
private static final String INVALID_INPUT_YN_MSG =
INVALID_INPUT_BEGIN + "must be n or y";
private static final String WINNER_MSG =
"you're a winner... # of guesses: ";
private static final String LOSER_MSG =
"too many guesses entered... the number was ";
private static final String QUITTER_MSG =
"you're a quitter... the number was ";
private static final String MAX_GAMES_PLAYED_MSG =
"you've played the maximum number (" + MAX_GAMES + ") of games";
private static final String ENTER_GUESS_PROMPT =
"enter a guess between " + MIN_NUMBER + " and " + MAX_NUMBER +
" (" + QUIT_VALUE + " to quit): ";
private static final String PLAY_AGAIN_PROMPT =
"Do you want to play again (n or y)? ";
private static final String BOLD_BEGIN = "*** ";
private static final String BOLD_END = " ***";
private static final String PLAY_MSG = " playing the CSC" + MAX_NUMBER +
" guessing game." + BOLD_END;
private static final String WELCOME_MSG =
BOLD_BEGIN + "Hello! Have fun" + PLAY_MSG;
private static final String THANKS_MSG =
BOLD_BEGIN + "Thanks for" + PLAY_MSG;
static Random rng = new Random(System.currentTimeMillis());
static Scanner stdin = new Scanner(System.in);
static Scanner scan = new Scanner(System.in);
static int guess;
static int numberOfGames = 0;
static int numberOfWins = 0;
static int numberOfLoses = 0;
static boolean endStart = true; // Check if this boolean is used
public static void main(String[] args) {
PlayGame();
PostGame();
EndGame();
}
public static void PlayGame() {
int guessCounter = 0;
int n = MIN_NUMBER + rng.nextInt(MAX_NUMBER);
System.out.println(WELCOME_MSG);
boolean test = true;
do {
System.out.println();
System.out.print(ENTER_GUESS_PROMPT);
guess = stdin.nextInt();
guessCounter = guessCounter + 1;
if (guess < MIN_NUMBER && guess != BACKDOOR_VALUE &&
guess != QUIT_VALUE) {
System.out.println(INVALID_INPUT_LESS_MIN_MSG + "\n");
}
if (guess > MAX_NUMBER) {
System.out.println(INVALID_INPUT_GREATER_MAX_MSG + "\n");
}
// Giving the user the answer
if (guess == BACKDOOR_VALUE) {
System.out.println(n);
}
// Quiting the round and giving the chance to end the game
if (guess == QUIT_VALUE) {
System.out.println((QUITTER_MSG + n) + "\n" +
PLAY_AGAIN_PROMPT);
numberOfLoses++;
String val = scan.next();
if (val.equalsIgnoreCase("y")) {
numberOfGames++;
PlayGame();
}
if (val.equalsIgnoreCase("n")) {
System.out.println(THANKS_MSG);
test = false;
PostGame();
break;
}
}
// Correct guess on the last try
if (guessCounter == MAX_GUESSES || guess == n) {
numberOfGames++;
System.out.println(WINNER_MSG + guessCounter);
numberOfWins++;
System.out.println(PLAY_AGAIN_PROMPT);
String val = scan.next();
if (val.equalsIgnoreCase("y"))
PlayGame();
if (val.equalsIgnoreCase("n")) {
System.out.println(THANKS_MSG);
test = false;
PostGame();
break;
}
}
// Max games
if (numberOfGames == MAX_GAMES) {
System.out.println(MAX_GAMES_PLAYED_MSG);
test = false;
PostGame();
break;
}
// Max guesses
if (guessCounter == MAX_GUESSES) {
numberOfGames++;
System.out.println(LOSER_MSG + n + "\n" + PLAY_AGAIN_PROMPT);
numberOfLoses++;
String val = scan.next();
if (val.equalsIgnoreCase("y"))
PlayGame();
if (val.equalsIgnoreCase("n")) {
System.out.println(THANKS_MSG);
test = false;
PostGame();
break;
}
}
// Guessing in the range of the bonds
if (guess > MIN_NUMBER || guess < MAX_NUMBER) {
if (guess != n) {
System.out.print(NOPE_MSG);
}
if (guess == n) {
numberOfGames++;
System.out.println(WINNER_MSG + guessCounter);
numberOfWins++;
System.out.println(PLAY_AGAIN_PROMPT);
String val = scan.next();
if (val.equalsIgnoreCase("y"))
PlayGame();
if (val.equalsIgnoreCase("n")) {
System.out.println(THANKS_MSG);
test = false;
PostGame();
break;
}
}
}
// Giving hints after 5 tries until the max # of guesses
if (guessCounter == HINT_THRESHOLD ||
guessCounter == (HINT_THRESHOLD + 1) ||
guessCounter == (HINT_THRESHOLD + 2) ||
guessCounter == (HINT_THRESHOLD + 3) ||
guessCounter == (HINT_THRESHOLD + 4) ||
guessCounter == (HINT_THRESHOLD + 5)) {
if (guess != n && guess > n) {
System.out.println(" lower");
}
if (guess != n && guess < n) {
System.out.println(" higher");
}
}
}
while (guess > MIN_NUMBER || guess < MAX_NUMBER);
}
// Post game information
public static void PostGame() {
int winningPct = 0;
System.out.print("Played: " + numberOfGames + ";" +
" Won: " + numberOfWins + ";" +
" Lost: " + numberOfLoses + ";");
if (numberOfGames != 0) {
System.out.println(" winningPct: " +
(winningPct = numberOfWins / numberOfGames));
} else {
winningPct = 0;
}
EndGame();
}
// Ending the game
public static void EndGame() {
}
}
Upvotes: 0
Views: 162
Reputation: 631
Using array to check if a number is guessed earlier is not optimal way. It limits the range of numbers you can guess even if the number of tries is not very high. Also most of the indexes of the array will remain empty in general.
So, better you can use Set<Integer>
(or Set<Long>
depending on allowed range of number that is guessed). Here is an example code:
Set<Integer> guessedNumbers = new HashSet<Integer>();
if(guessedNumbers.contains(guess)){
System.out.println("You already guessed this earlier");
}
else{
guessedNumbers.add(guess);
}
Upvotes: 3
Reputation: 1310
The part that I am confused about is where I need to check
Huh, i was confused too when i read this code...
So, let's start!
static Random rng = new Random(System.currentTimeMillis());
- it's unmeaningful name. Rename to random
, randomGenerator
or something else.static int guess;
why don't initialize to 0 or remove other initializations. Looks inconsistent.static boolean endStart = true;
endStart? Really?! That variable name contrary to yourself. // Check if this boolean is used
- i checked with Ctrl+f in browser -> 0 usages.boolean test = true;
test of what? For what? numberGuessed
, isGuessed
would be better.guessCounter = guessCounter + 1;
change to guessCounter++;
just for simplicity.PlayGame();
recursive? Looks odd.These are just some stranges in code for me, but overall design is overcomplicated too:
Try to concentrate on "main loop" that presents in many games and hide all cluttering service actions in separate functions.
I'm not sure but you can use objects to make interaction with game more "natural". Something like:
Game game = new Game();
Player player = new Player();
boolean numberGuessed = false;
while (!numberGuessed) {
int guess = player.makeGuess();
if (game.checkGuess(guess)) {
System.out.println("You win!");
} else {
System.out.println("Please, try again");
}
class Player {
private int guessCount;
public String grabInput() {
System.out.println("Make a guess");
System.out.print("> ");
// take Scanner input, parse number or exit message
}
public int makeGuess() {
//present your guess
guessCount++;
}
}
That is very very rough outline but i think it helps you rethink application design.
Ah, your original question... almost forget about it.
Write the function (actually method) that iterates over array and checks given number for existence. In my example you can incapsulate such function in player.makeGuess()
function
Upvotes: 0