Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Guessing Game - Statistics in Java

    Hello! I'm a new programmer and I got stuck with my task.

    I'm trying to create a Guessing Game.
    The game randomize a number between 1 and 100 and let's the user guess.
    When correct, program asks if player want to play a new game or return to menu. While this is happening the program will also count the guesses and the number of games played, for statistics.

    The menu contains of:
    1. Play
    2. Statistics
    3. Exit

    I've got the game and exit part working alright. It is the statistics that is giving me a hard time.
    The statistics contains of:
    Number of games played
    The lowest number of guesses
    The highest number of guesses
    The avarage number of guesses

    The avarage is no problem. NumberOfGuesses / NumberOfGames
    The problem is the lowest and highest number of guesses. The program must keep track of my best game and my worst game, even if i play 50 games.

    Here is where i'm stuck.
    And i'm not sure that my main class is correct, looks awefully empty. It works though.

    Appreciate help!



    MAIN CLASS:


    Code:
    package thegame;
    
    
    /**
     *
     * @author Emil
     */
    public class TheGame {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
     
        newGame theGame = new newGame();
        
            
        theGame.showMainMenu();
        theGame.playGame();
        theGame.showStatistics();

    THE GAME CLASS. Here i have included all my methods for new game, statistics, menu, exit etc.

    Code:
    package thegame;
    import java.util.Random;
    import java.util.Scanner;
    /**
     *
     * @author Madeleine
     */
    public class newGame {
        
        //Initialize variables
        int numberOfGames = 0;
        int numberOfGuesses = 0;
        //int counter1;
       // int counter2;
       // private static final int MIN_NUMBER = 1;
        //private static final int MAX_NUMBER = 100;
        //double avarageGuesses;
        int highestGuesses;
        int lowestGuesses;
       // int currentGuess = 0;
        
        
        
    
        
        
        //Method for the main menu.
        public void showMainMenu() {
            
        System.out.println("Welcome to The Game. Choose an option.");
        System.out.println("1. New Game");
        System.out.println("2. Statistics");
        System.out.println("3. Exit");
        System.out.println();
        int menuOption;
            Scanner input = new Scanner(System.in);
            System.out.print("Selection: ");
            menuOption = input.nextInt();
            System.out.println();
            if (menuOption < 1 || menuOption > 3){
            System.out.println("Invalid Menu Selection!");
            System.out.println("Please make another selection.");
            showMainMenu(); }
            
            //Reads users input and display the method chosen. If not 1,2 or 3, error 
            //message is displayed.
        if (menuOption == 1)playGame(); 
        if (menuOption == 2)showStatistics();
        if (menuOption == 3)exitGame();
        
             //End Method
        }
             
        //Method for the game.
        public void playGame() {
       
        //Variables and scanner.
        boolean win = false;
                
        Random rand = new Random();
        int theNumber = rand.nextInt(100);
        int guess;
        Scanner input = new Scanner(System.in);
        
        System.out.println("Welcome. I would like to play a game. Guess a number"
                + " between 1 and 100!");
        
        //While guess is not right, loop. When guess is right, end loop.
        while (win == false){
            
        guess = input.nextInt(); 
        while (guess < 0 || guess > 100){
                System.out.println("Please type a number between 1 and 100."
                        + " Don't worry, this guess doesn't count.");
                System.out.println("Try again:");
                 guess = input.nextInt();   }
        
        if (guess == theNumber){ win = true;numberOfGuesses++; 
        
        
        }
        //Adding a guess each time a user guesses wrong. If user guess under 0 
        //or over 100, error message is displayed and guess is not counted.
        
        else if (guess > theNumber){
                    System.out.println("The number guessed is too high!"
                            + " Guess again:");numberOfGuesses++;
                          
        }
        else if (guess < theNumber){
                    System.out.println("The number guessed is too low!"
                            + " Guess again:");numberOfGuesses++;
                                              
        }
       
        
        }
        
        
        //Game end. +1 for numberOfGames
            System.out.println("You beat the game. Congratulations!");
            System.out.printf("\nThe number was indeed %d.\n", theNumber);
            numberOfGames++;
            
            System.out.println("Want to start a new game? Type in 1 to start a new game"
                    + " or type 2, to return to the main menu.");
            int playAgain = input.nextInt();
            
            //Asks if user will play again or return to menu. 
         
            while (playAgain > 2 || playAgain < 0){
                System.out.println("Choose a number between 1 and 2.");
                playAgain = input.nextInt();
                }
            if (playAgain == 1)playGame();
            else showMainMenu();
            
            
            //Ends Method
        }
        
        //Method for statistics.
        public void showStatistics (){
          //countGuesses();
          
          Scanner input = new Scanner(System.in);
          
          if (numberOfGames == 0){
              System.out.println("Please, play a game before statistics can"
                      + " be shown.");
              showMainMenu();
          }
          
          
          
          //Shows statistics.
          System.out.println("Statistic:");
            System.out.println();
          System.out.println("Played games:" +numberOfGames);
            System.out.println("Number of Guesses:" +numberOfGuesses);
          System.out.println("Lowest number of guesses:" +lowestGuesses) ;
          System.out.println("Highest number of guesses: " +highestGuesses); 
          double avarageGuesses;
          avarageGuesses = numberOfGuesses / numberOfGames;
          System.out.printf("Avarage number of guesses: %.1f" , avarageGuesses);
          System.out.println();
          System.out.println("Please, type 1 to return to the main menu."
          + "\nPress 2 to share result on Facebook!\n");
          
          //Choice to return to menu or "post on facebook".
          int returnMenu;
          returnMenu = input.nextInt();
          while (returnMenu < 1 || returnMenu > 2){
          System.out.println("Please, type 1 or 2.");
          returnMenu = input.nextInt();
          }
          if (returnMenu == 1)showMainMenu();
          if (returnMenu == 2)
          System.out.println("Open your broswer. Log on Facebook and type"
          + " that stuff in bro! You're awesome!");
          System.out.println("And here is the menu again, please play"
          + " one more time!"); System.out.println(); showMainMenu();
          
          //If user chooses 2, it goes to menu after a short message.
          //End Method
          }
        
        //Method to exit game.
        public void exitGame(){
            
            System.out.println("Are you sure that you want to exit The Game?"
                    + "\nType YES to exit and NO to return to the main menu.\n");
            Scanner input = new Scanner(System.in);
            String exit;
            exit = input.next();
            exit = exit.toUpperCase();
            //Prompt user to input YES or NO. Also ignores case-sensitivity.
            while (!(exit.equals ("YES") || (exit.equals ("NO") ) ) ){
                System.out.println("Please type Yes or No.");
                exit = input.next();
                exit = exit.toUpperCase();
            }
            
            if (exit.equals ("YES"))
                System.out.println("The game has ended. Please come again!");System.exit(0);
            
            if (exit.equals ("NO")) showMainMenu();
        
    }//End Method
    
    //End Class
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Dangit, I had such a nice long one typed up here; stupid back mouse button >.>. Remind me to come back in case I forget since these mark them as read.
    This looks like homework, so I can't give the answer, but can provide ideas; its quite simple:
    Make a running least and most amount of guesses as a property to the class. When verifying and completing the game, check and see if this game score is better/worse than one of them, and if so, simply replace it. Most of it's in the playGame() method, but you'll need to do the one for statistics to show them as well.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    enm1l (10-16-2013)

  • #3
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Solved it!

    Thank you for the help, it was the best =).

    As u said, simple. I just forgot to initialize numberOfGuesses to 0 everytime I start a new game.

  • #4
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Alright, i'm back with a new problem. Still the same program.

    I should implement the menu as a method showMainMenu() that returns an integer depending on the user's choice. The method showMainMenu() is found in the GAME class.
    And here is what i got so far:

    Code:
    public static void main(String[] args) {
            
    
             newGame test = new newGame();
              
              
                int callMethod = test.showMainMenu();
              
                while (callMethod == 1){
                    test.playGame();          
                }
                if (callMethod == 2)test.showStatistics();

    When i run this, it seems to be working. The game plays out fine and the statistics show a friendly error message if numberOfGames == 0. The problem is, that when i now use showMainMenu(); anywhere in my code, i get an error.

    The menu pops up like it should, but any choice i make just give me errors.
    I think it has something to do with that the menu just returns an integer and i can't seem to make my methods go back to main after they are executed.

    Anyone have an idea? And as previous poster wrote, this is an assignment. So i would enjoy tips but not the entire solution.

    I do appreciate quick answers, since it is due to Sunday.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •