0

I basically have everything done I just don't know how to get the program to record the best score per session and I also don't understand how to get the program to save the information of the player to a .txt file everytime the "session" ends. The .txt file needs to be in the format username, number of goes, number of successful goes and best score. My code so far is

        import java.util.Random;
import java.util.Scanner;
import java.util.Writer;
import java.util.File; 

public class GuessingGame3 {

    public static void main(String[] args)
    {

        Random generator = new Random(); //This is were the computer selects the Target

        int guess;
        int count = 0;
        int Target;
        String userName;
        String playagain;
        boolean play = true;
        int session = 0;
        int sessions = 0;

        Scanner consoleIn = new Scanner(System.in); 
        Scanner name = new Scanner(System.in); 

        System.out.println("Hello! Please enter your name:\n"); //This is were the user enters his/her name
        userName= name.nextLine();

        System.out.println("Hello "+ userName + " :) Welcome to the game!\n");


        while (play = true)
        {
            session++;
            Target = generator.nextInt(100) + 1;
            System.out.println("Can you guess the number i'm thinking off? You will have 6 attempts to guess the correct number"); //This is where the computer asks the user to guess the number and how many guesses they will have

            do {
                guess = consoleIn.nextInt();
                count++;

                if (guess > Target)
                System.out.println("Sorry! Your guess was too high! Guess again :)"); //This is to help the player get to the answer 
                else 
                if (guess < Target)
                System.out.println("Sorry! Your guess was too low! Guess again :)"); //This is to help the player get to the answer 
               }        
                while(guess != Target && count <6);

                if(guess == Target) {
                System.out.println("Congratulations "+  userName + ", it took you "+ count +" attempts to guess correctly!"); //This tells the player that they got the correct answer and how many attempts it took
                    sessions++;
                        }

                else 
                {
                System.out.println("Sorry "+ userName + ", You've used up all of your guesses! The correct answer was "+ Target + "!");  //This tells the player that they failed to find the number and then tells them what the correct answer  
                }
                {
                Scanner answer = new Scanner(System.in);
                System.out.println("Would you like to play again "+ userName +"? [Y/N]");//This asks the player if they would like to play again
                playagain = answer.nextLine();
               if(playagain.equalsIgnoreCase("Y"))//This is what happens if the player opts to play again
                {
                play = true;
                count = 0;
                count++;

                } else if(playagain.equalsIgnoreCase("N"))//This is what happens if the player opts to exit the game
                {
                    play = false;
                    System.out.println("Thanks for playing "+ userName +"! :) Please come back soon!");

                    System.out.println("The number of goes you had: "+ session +"");
                    System.out.println("The number of times you guessed correctly: "+ sessions +"");
                    PrinterWriter writer = PrinterWriter("Record");
                    writer.println(userName, session, sessions);
                    break;
                }

             }
        }
    }
}       

1 Answers1

1

To determine the best score per session, all you need to do is first save the count from the session and compare with the previous session.

So after your do..while.. it would look something like

int lowestScore = 6;// or max number of guesses

...

while(play == true)
{
    ...

    do...while...
    if(count < lowestScore)
    {
         lowestScore = count;
    }

    ...
}

This will compare the lowestScore with the current score evertime the user starts a new session. Also, note the double equal sign in the while statement.

techMonkey
  • 41
  • 1
  • 7