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
generate the secret number
determine whether the current guess is a winner
- 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++;
}
}