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.