0

You will write a Java program to play the game of Pico, Fermi, Bagel. 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. 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"

The program will have a main class and a Bagels class. The bagels class will call 3 other methods to

  1. generate the secret number

  2. determine whether the current guess is a winner

  3. evaluate the current guess and print hints

My Problem -- When I run the program, it asks me to enter a 3 digit code, but then it just keeps asking me to enter a 3 digit code instead of comparing the user's guess number to the random generated number. I'm assuming I'm doing something dumb with the classes or methods since we just learned them not too long ago and they still confuse me.

The first part of the program is my main class, the second is the Bagels class.

package assignment.iii;
import javax.swing.JOptionPane;
import java.util.Scanner; 

public class AssignmentIII {

public static void main(String[] args) 
   {
   int playagain = JOptionPane.showConfirmDialog(null, "Would you like to play?", "Message", JOptionPane.YES_NO_OPTION);

    while (playagain == JOptionPane.YES_OPTION) {      

     Bagels myBagels = new Bagels();
     myBagels.playGame() ;     
     myBagels.randNumber = 0;
     playagain = JOptionPane.showConfirmDialog(null, "Would you like to play again?", "Message", JOptionPane.YES_NO_OPTION);
}
}
}



 // start of Bagels class

package assignment.iii;
import java.util.Random;
import javax.swing.JOptionPane;

public class Bagels{


public int randNumber; 
private int Guess; 
private int Rand1, Rand2, Rand3;
private int Guess1, Guess2, Guess3;
private int guessCount;


public void playGame() 
{

    do {
    Guess = Integer.parseInt(JOptionPane.showInputDialog("Enter a three digit number"));
    }
    while (Guess != randNumber);                   

 }

private int generateSecretNumber()
{
         Random randN = new Random();
         return randN.nextInt(999)+1; 
}  

private void printHint(String guess)        
{
   if(randNumber == Guess)
        System.out.println("Correct");

      else
         {
         Guess1 = (Guess)/100;
         Guess2 = (Guess%100)/10;
         Guess3 = (Guess%100)%10;
         }

    if(Guess1 == Rand1)
    {
        System.out.println("Fermi");
    }
    if(Guess2 == Rand2)
    {
        System.out.println("Fermi");
    }
    if(Guess3 == Rand3 )
    {
        System.out.println("Fermi");
    }
    if(Guess2 == Rand1)
    {
        System.out.println("Pico");
    }
    if(Guess3 == Rand1)
    {
        System.out.println("Pico");
    }
    if(Guess1 == Rand2)
    {
        System.out.println("Pico");
    }
    if(Guess3 == Rand2)
    {
        System.out.println("Pico");
    }
    if(Guess1 == Rand3)
    {
        System.out.println("Pico");
    }
    if(Guess2 == Rand3)
    {
        System.out.println("Pico");
    }
    else if(Guess1 != Rand1 && Guess1 != Rand2 && Guess1 != Rand3 &&
            Guess2 != Rand1 && Guess2 != Rand2 && Guess2 != Rand3 &&
            Guess3 != Rand1 && Guess3 != Rand2 && Guess3 != Rand3)
    {
        System.out.println("Bagels");
    }
    guessCount++;
   }
}
Alex
  • 155
  • 1
  • 12
  • Still not sure why it is just repeatedly asking me to enter 3 digits? I am thinking it has something to do with that in my bagels class, my generateSecretNumber and printHint methods say they are unused? But i am not sure how to make it so they are used within the program? – Alex Oct 20 '14 at 02:28

1 Answers1

1

Without testing it myself, it really looks like the error is in the main method of your Assignment class. When you create the bagel object, the first thing that happens is it's playGame method gets called.

Bagels myBagels = new Bagels();
myBagels.playGame() ;

Your playGame method relies on a comparison to randNumber, but randNumber never get initialized! Java member variables get a default value, 0 for ints (see: Default Values and Initialization in Java). So unless the user inputs "0", the loop condition will be true (Guess does not equal 0) and the user will be prompted again.

One suggestion I will make is to write a constructor that takes a single int value, and sets the randNumber to that.

Community
  • 1
  • 1
David K
  • 46
  • 3
  • So I initialize it in the main class right? I tried doing (int randNumber = 0;) in the main class under the string but it just says randNumber is not used. Or do i need to add it in the bagels class under the playGame? – Alex Oct 20 '14 at 01:39
  • Right, that is because randNumber is a field in the 'Bagels' class. The instance of 'Bagels' that you have created, called myBagels, has the randNumber field. So from outside of that class, (like in your main method), you would access it with `myBagels.randNumber`. I would have a look at [this page](http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html) and try to add a constructor for your Bagels class that takes an int, and uses it to initialize the randNumber field. Calling the constructor would then look like `Bagels myBagels = new Bagels(10);` to initialize that to 10. – David K Oct 20 '14 at 01:49