Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 24 of 24

Thread: Bagels game

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Bagels game

    I have to program a game called Bagels. This is how it is suppose to go:
    -------------------------------------------------------------------------------
    This assignment is to write an interactive Java program to play the game of Bagels. Here are the rules of the game:

    The computer will generate a "secret" three digit number at random. The first number will not be 0, and all the digits will be different. The user tries to guess the number. If the user guesses correctly, then the game is over.

    If not, the computer gives a hint and the player tries again (or quits).

    The hints:

    • for each digit that matches the secret number in the proper place, the computer prints "Fermi"

    • for each digit that matches, but not in the proper place, the computer prints "Pico"

    • if none of the digits match, the computer prints "Bagels"

    Examples (supposing that the secret number is 482):

    guess = 637, Bagels

    guess = 381, Fermi

    guess = 382, Fermi Fermi

    guess = 832, Fermi Pico

    guess = 328, Pico Pico

    guess = 428, Fermi Pico Pico

    guess = 482, Winner! (the game is over)

    When the game is over, the results are printed: whether the user won or quit, and the number of guesses made
    -----------------------------------------------------------------------------

    Here is my code so far:

    import java.util.Random;
    import javax.swing.JOptionPane;
    //import javax.swing.JOptionPane;
     
    /**
     * A class to play the game of Bagels
     * @John Pinares
     */
    public class Bagels
    {
       // instance variable declarations go here
        private int secretNumber; //the randomly generated number
        private int userGuess; //the number guessed by user
        private int secret1; //the first secret digit
        private int secret2; //the second secret digit
        private int secret3; //third secret digit
        private int input1; //first digit of user guess
        private int input2; //second digit of user guess
        private int input3; //third digit of user guess
        private int guessCount;
     
       // creates a random number generator object
       private static Random r = new Random();
     
       /**
        * Creates a Bagels object
        */
       public Bagels()
       {
     
       }
     
       /**
        * Conducts a game and prints the results
        */
       public void play()
       {
          this.generateSecretNumber();
          do
          {
              String play = JOptionPane.showInputDialog("Enter a 3-digit number"
                      + " not starting with 0.");
              userGuess = Integer.parseInt(play);
     
              this.evaluateGuess();
          }
     
          while(userGuess != secretNumber);
     
          guessCount++;
     
       }
     
       /**
         Generates three random single digit ints.  The first cannot be zero
         and all three will be different. Called by public method play()
       */
       private void generateSecretNumber()
       {
          do
          {
              secretNumber = r.nextInt(900)+100;
              secret1 = secretNumber/100;
              secret2 = (secretNumber%100)/10;
              secret3 = (secretNumber%100)%10;
          }
          while ((secret1 == secret2) || (secret2 == secret3) || (secret3 == secret1));
          System.out.println(secretNumber);
       }
       /**
        Evaluates the user's guess and prints the guess and hints to System.out.
        Called by public method play()
       */
       private void evaluateGuess()
       {
           if(secretNumber == userGuess)
           {
               System.out.println("You Win");
           }
     
            else
           {
            input1 = (userGuess)/100;
            input2 = (userGuess%100)/10;
            input3 = (userGuess%100)%10;
     
     
            if(input1 == secret1)
            {
                System.out.println("Fermi");
            }
            if(input2 == secret2)
            {
                System.out.println("Fermi");
            }
            if(input3 ==secret3 )
            {
                System.out.println("Fermi");
            }
            if(input2 == secret1)
            {
                System.out.println("Pico");
            }
            if(input3 == secret1)
            {
                System.out.println("Pico");
            }
            if(input1 == secret2)
            {
                System.out.println("Pico");
            }
            if(input3 ==secret2 )
            {
                System.out.println("Pico");
            }
            if(input1 == secret3)
            {
                System.out.println("Pico");
            }
            if(input2 ==secret3 )
            {
                System.out.println("Pico");
            }
            else if(input1 != secret1 || input1 != secret2 || input1 != secret3 &&
                    input2 != secret1 || input2 != secret2 || input2 != secret3 &&
                    input3 != secret1 || input3 != secret2 || input3 != secret3)
            {
                System.out.println("BAGLES!");
            }
     
           }
     
       }
    --------------------------------------------------------------------------------

    My current problem is that the out put will always print Bagels when it should only print bagels when none of the digits from secretNumber match the digits from userGuess. I know for sure the problem is here

    else if(input1 != secret1 || input1 != secret2 || input1 != secret3 ||
                    input2 != secret1 || input2 != secret2 || input2 != secret3 ||
                    input3 != secret1 || input3 != secret2 || input3 != secret3)
            {
                System.out.println("BAGLES!");
            }

    Thanks in advance for any help.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Bagels game

    The problem with using ORs to connect expressions in a large condition is that if any one is true the condition is true.
    If you want all of the subexpressions to be true, use ANDs
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Pinares (March 28th, 2013)

  4. #3
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Bagels game

    That did it. Here is my new code:
    import java.util.Random;
    import javax.swing.JOptionPane;
    //import javax.swing.JOptionPane;
     
    /**
     * A class to play the game of Bagels
     * @John Pinares
     */
    public class Bagels
    {
       // instance variable declarations go here
        private int secretNumber; //the randomly generated number
        private int userGuess; //the number guessed by user
        private int secret1; //the first secret digit
        private int secret2; //the second secret digit
        private int secret3; //third secret digit
        private int input1; //first digit of user guess
        private int input2; //second digit of user guess
        private int input3; //third digit of user guess
        private int guessCount;
     
       // creates a random number generator object
       private static Random r = new Random();
     
       /**
        * Creates a Bagels object
        */
       public Bagels()
       {
     
       }
     
       /**
        * Conducts a game and prints the results
        */
       public void play()
       {
          this.generateSecretNumber();
     
          do
          {
              String play = JOptionPane.showInputDialog("Enter a 3-digit number"
                      + " not starting with 0.");
              userGuess = Integer.parseInt(play);
     
              System.out.println("Your guess was " + userGuess);
     
              this.evaluateGuess();
              guessCount++;
          }
     
          while(userGuess != secretNumber);
     
          System.out.println("It took you " + guessCount + " guesses.");
     
          if(userGuess == secretNumber)
          {
              String playagain = JOptionPane.showInputDialog("Do you want to play again? y/n");
              if(playagain.equalsIgnoreCase("y"))
              {
                  this.play();
              }
          }       
     
       }
     
       /**
         Generates three random single digit ints.  The first cannot be zero
         and all three will be different. Called by public method play()
       */
       private void generateSecretNumber()
       {
          do
          {
              secretNumber = r.nextInt(900)+100;
              secret1 = secretNumber/100;
              secret2 = (secretNumber%100)/10;
              secret3 = (secretNumber%100)%10;
          }
          while ((secret1 == secret2) || (secret2 == secret3) || (secret3 == secret1));
     
       }
       /**
        Evaluates the user's guess and prints the guess and hints to System.out.
        Called by public method play()
       */
       private void evaluateGuess()
       {
           if(secretNumber == userGuess)
           {
               System.out.println("You Win");
           }
     
            else
           {
            input1 = (userGuess)/100;
            input2 = (userGuess%100)/10;
            input3 = (userGuess%100)%10;
     
     
            if(input1 == secret1)
            {
                System.out.println("Fermi");
            }
            if(input2 == secret2)
            {
                System.out.println("Fermi");
            }
            if(input3 ==secret3 )
            {
                System.out.println("Fermi");
            }
            if(input2 == secret1)
            {
                System.out.println("Pico");
            }
            if(input3 == secret1)
            {
                System.out.println("Pico");
            }
            if(input1 == secret2)
            {
                System.out.println("Pico");
            }
            if(input3 ==secret2 )
            {
                System.out.println("Pico");
            }
            if(input1 == secret3)
            {
                System.out.println("Pico");
            }
            if(input2 ==secret3 )
            {
                System.out.println("Pico");
            }
            else if(input1 != secret1 && input1 != secret2 && input1 != secret3 &&
                    input2 != secret1 && input2 != secret2 && input2 != secret3 &&
                    input3 != secret1 && input3 != secret2 && input3 != secret3)
            {
                System.out.println("BAGLES!");
            }
     
           }
     
       }
     
       // Returns a value of true or false indicating whether the current
       // guess is a winner. Called by public method play()
       private boolean isWinner()
       {
          // write method body here
     
          // NOTE: this return statement is here only so the class skeleton
          // will compile.  Feel free to use or discard
          return false;
       }
    }


    Now how can I incorporate whether the user quits into a boolean so that if the user does quit i can print "the user quit"?

    --- Update ---

    Also, where it asks the user if he wants to play again, if he says no, how can I tell the program to quit?

    if(playagain.equalsIgnoreCase("n"))
    {
    //what would I put here to make the program stop
    }

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Bagels game

    to make the program stop
    Several choices: set a boolean variable and exit the loop the code is in. Outer loops need to check the boolean to see if the code is to exit.
    Call the System.exit(0) method to end the program's execution

    whether the user quits into a boolean so that if the user does quit
    Set the boolean if the user quits and later test the boolean's value to know if the user has quit.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Bagels game

    What reserve word do I use for when the user quits? I cannot use null in a boolean because they are incompatible types.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Bagels game

    boolean variables can have the values: true or false
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Bagels game

    I tried if(int == null) but it says i can't compare the two. Would I have to initialize a new variable to compare?

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Bagels game

    it says i can't compare the two
    Can you post the line of code that is giving the error message?
    Also say what data types all the variables in that line are.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Bagels game

    before i had:

    if(userGuess == null)
    {
    //stuff
    }

    where userGuess is an int. It said "need int, found <null> incomparable types", so I added a string called play where I stored the userGuess in the JOP.sIP, so now i have this:

    private String play;

    play = JOP.sIP("dialog")

    userGuess = Integer.parseInt(play);

    if(play == null)
    {
    //
    }

    My goal is to be able to print a message that says the user quit and how many guesses he made before he quit.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Bagels game

    How do you know if the user has quit? What does the user enter that says "I quit"?
    Can the program then test for that input from the user and do what you want done?

    There are two issues:
    1)determining the user wants to quit
    2) quiting

    Solve one and when that works, solve the other.

    How step 2) is done depends on the program's structure.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Bagels game

    Well clicking cancel normally means the user quit. I guess that's what I was going for when I was using null. Is that right or should I be trying something different?

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Bagels game

    Is null what is returned to the play variable when the user clicks cancel?
    Then the code you posted would detect that:
    play = JOP.sIP("dialog")
     
    if(play == null)
    {
    //  The user has asked to quit
    }

    If that works as expected, how can the program quit?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Bagels game

    How can I check what is returned when the user clicks cancel? and that code does not work. I tried it.

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Bagels game

    How can I check what is returned when the user clicks cancel?
    Use the println() method to print out the value of the variable that was assigned what was returned by the method (play in the above code).


    that code does not work
    Please explain what happened.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Bagels game

    Nothing happened. The code compiled and ran like it should except for that piece of code. It might as well have not been there.

    Same thing happens when I use println() to print the value of play when the user quits.

    When I do click cancel, this shows up in output:

    Exception in thread "main" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:454)
    at java.lang.Integer.parseInt(Integer.java:527)
    at Bagels.play(Bagels.java:48)
    at BagelsTest.main(BagelsTest.java:12)
    Java Result: 1

    Does that have some kind of clue as to what is going wrong?

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Bagels game

    Exception in thread "main" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:454)
    at java.lang.Integer.parseInt(Integer.java:527)
    at Bagels.play(Bagels.java:48)
    The value of the variable passed to the parseInt() method on line 48 was null. The code should test the variable's value before passing it the the parseInt() method. The code in post#9 is wrong. The code in post#12 tests the value before calling parseInt().

    If the value is null, do NOT call the parseInt() method.
    If you don't understand my answer, don't ignore it, ask a question.

  18. The Following User Says Thank You to Norm For This Useful Post:

    Pinares (March 29th, 2013)

  19. #17
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Bagels game

    That did it! Thanks a ton.

  20. #18
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bagels game

    What should it look like in the class?

  21. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Bagels game

    What should it look like in the class?
    What is the "it" you are asking about?
    If you don't understand my answer, don't ignore it, ask a question.

  22. #20
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bagels game

    Quote Originally Posted by Norm View Post
    The value of the variable passed to the parseInt() method on line 48 was null. The code should test the variable's value before passing it the the parseInt() method. The code in post#9 is wrong. The code in post#12 tests the value before calling parseInt().

    If the value is null, do NOT call the parseInt() method.
    Is there something to be done for line 48? I did all that is posted here and whenever i press cancel to quit, i get this.Exception in thread "main" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:454)
    at java.lang.Integer.parseInt(Integer.java:527)
    at Bagels.play(Bagels.java:48)
    And what do you mean by not calling the parseInt() method?
    would it be something like this?
    play = JOP.sIP("dialog")

    if(play == null)
    {
    // The user has asked to quit

    }
    else (play!=null)
    {
    print statement
    parseInt()
    }
    ???????

  23. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Bagels game

    Yes the code would be like that so it would not call the parseInt() method with a null value.

    The (play!=null) test is redundant. The (play==null) test in the if guarantees that play will not be null.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #22
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bagels game

    Quote Originally Posted by Norm View Post
    Yes the code would be like that so it would not call the parseInt() method with a null value.

    The (play!=null) test is redundant. The (play==null) test in the if guarantees that play will not be null.
    So, is
    if(play == null)
    {
    // The user has asked to quit
    parseInt()
    correct?
    Am i suppose to get
    Exception in thread "main" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:454)
    at java.lang.Integer.parseInt(Integer.java:527)
    at Bagels.play(Bagels.java:44)
    at BagelsTest.main(BagelsTest.java:12)
    after quitting?

  25. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Bagels game

    if(play == null)
    {
      // The user has asked to quit
      // >>>>  add program quitting logic here<<<<
     
    }
    else 
    {
      x = Integer.parseInt(play)
    }

    I didn't say to remove the else clause. Just remove the if(play!=null) part
    If you don't understand my answer, don't ignore it, ask a question.

  26. #24
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bagels game

    oh. ok. thank you.

Similar Threads

  1. LWJGL Game: Odd behavior in game. Need help.
    By vividMario52 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 2nd, 2013, 05:43 PM
  2. Replies: 3
    Last Post: March 1st, 2013, 11:01 PM
  3. Help with Snake Game Java Code: It's Impossible to Lose the Game
    By haruspex_icis in forum What's Wrong With My Code?
    Replies: 20
    Last Post: December 17th, 2012, 12:21 PM
  4. Game Maker Language and Game Maker and Zelda Classic thread
    By Fira in forum Other Programming Languages
    Replies: 3
    Last Post: April 17th, 2012, 08:59 AM
  5. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 09:21 PM

Tags for this Thread