laetee
laetee

Reputation: 41

Displaying # of games, best game, and total tries in guessing game

This is a guessing game program that receives a random number and prompts the user to guess what the number can be. Each guess is followed by whether the number is lower, higher, or it is the number. At the end of each game the program asks if the user wants to play again. If the user input has the letter y, the game starts over again, if it contains the letter n, the overall statistics of the game(s) are displayed (how many games played, total number of guesses, average guesses, best game (lowest # of guesses), etc).

My issue is that when at least two games are played, the overall statistics displays only one game was played, the total guesses displays the guesses in only the last game played, and the best game displays the number of guesses from both of the games played (I tested mostly playing 2 games).

I would really appreciate it if someone could point out all my issues and where I could consolidate some code (I feel like there are easier ways of writing my code). Im a beginner so please forgive my stupid mistakes. This was worked on over several days so I couldve easily skimmed over something.

public class GuessingGame {

public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
playGame(reader);

}


public static void playGame(Scanner reader) {
System.out.println("I'm thinking of a number between 1 and 100... "
+ "\nYour guess?");

int guess = 0;
int number = (int)(java.lang.Math.random()*100)+1;
int guessCount = 0;
int minGuess = 0;
int game = 0;
int count = 1;
guess = reader.nextInt();
String userInput;


for(; guess < number || guess > number; count++) {

if (guess > number) {
System.out.println("It's lower.");
guessCount++;
}
if (guess < number) {
System.out.println("It's higher.");
guessCount++;
}
System.out.println("Your guess? ");
guess = reader.nextInt();


}




if (guess == number) {
System.out.println("You got it right in " + count + " guesses!");
guessCount++;
game++;
}

System.out.println("Do you want to play again?");
userInput = reader.next();

if(userInput.indexOf("y")!=-1) {
playGame(reader);
game++;
//System.out.println("Starting game...");
}


else {
int avgGuess = guessCount/game;
System.out.println("Overall results: ");
System.out.println("Total games  = " + game);
System.out.println("Total guesses  = " + count);
System.out.println("Guesses/game  = " + avgGuess);

}
for (int x = 1; x <= guessCount; x++) {
if(guessCount < minGuess || x == 1) {
minGuess = guessCount;

}
}

System.out.println("Best game  = " + minGuess + " guesses.");
}

}

Upvotes: 1

Views: 610

Answers (1)

Chris Young
Chris Young

Reputation: 66

The issue is where you are declaring and initializing your game variable.

having

int game = 0;

in your playGame function is causing it to get overridden to 0 on each game played.

To fix this you could have a class level int variable that counts the games and gets incremented on each game played.

(as good practice you probably want the new class variable to be private and accessed/mutated through a getter function and a setter, or in this case an increment function that can 'abstract' the implementation of increment away from the caller.)

Upvotes: 1

Related Questions