# Bagels game

• March 28th, 2013, 03:27 PM
Pinares
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:

Code Java:

```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

Code Java:

```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.
• March 28th, 2013, 03:41 PM
Norm
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
• March 28th, 2013, 04:12 PM
Pinares
Re: Bagels game
That did it. Here is my new code:
Code Java:

```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
}
• March 28th, 2013, 04:31 PM
Norm
Re: Bagels game
Quote:

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

Quote:

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.
• March 28th, 2013, 04:48 PM
Pinares
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.
• March 28th, 2013, 05:07 PM
Norm
Re: Bagels game
boolean variables can have the values: true or false
• March 28th, 2013, 08:59 PM
Pinares
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?
• March 29th, 2013, 06:23 AM
Norm
Re: Bagels game
Quote:

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.
• March 29th, 2013, 01:57 PM
Pinares
Re: Bagels game

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.
• March 29th, 2013, 02:03 PM
Norm
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.
• March 29th, 2013, 02:37 PM
Pinares
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?
• March 29th, 2013, 02:53 PM
Norm
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:
Code :

```play = JOP.sIP("dialog")   if(play == null) { // The user has asked to quit }```

If that works as expected, how can the program quit?
• March 29th, 2013, 04:14 PM
Pinares
Re: Bagels game
How can I check what is returned when the user clicks cancel? and that code does not work. I tried it.
• March 29th, 2013, 04:20 PM
Norm
Re: Bagels game
Quote:

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).

Quote:

that code does not work
Please explain what happened.
• March 29th, 2013, 04:49 PM
Pinares
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?
• March 29th, 2013, 04:53 PM
Norm
Re: Bagels game
Quote:

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.
• March 29th, 2013, 05:14 PM
Pinares
Re: Bagels game
That did it! Thanks a ton.
• April 11th, 2013, 06:20 PM
wcho92
Re: Bagels game
What should it look like in the class?
• April 11th, 2013, 06:39 PM
Norm
Re: Bagels game
Quote:

What should it look like in the class?
What is the "it" you are asking about?
• April 11th, 2013, 07:47 PM
wcho92
Re: Bagels game
Quote:

Originally Posted by Norm
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()
}
???????
• April 11th, 2013, 07:59 PM
Norm
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.
• April 11th, 2013, 08:12 PM
wcho92
Re: Bagels game
Quote:

Originally Posted by Norm
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?
• April 11th, 2013, 08:21 PM
Norm
Re: Bagels game
Code :

```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
• April 11th, 2013, 08:27 PM
wcho92
Re: Bagels game
oh. ok. thank you.