package Bagels; import java.util.*; import java.io.BufferedReader; import java.io.InputStreamReader; public class Bagels { private Random rnd = new Random(); private Boolean Running = true; private Boolean GameOver = false; private Integer[] Digits = {0,0,0}; public static void main(String[] args) { Bagels game = new Bagels(); game.run(); } public void run() { System.out.print("*** BAGELS : JAVA ***"); Integer guess; Integer attempt; Integer score = 0; Running = true; if(askToSeeTheRules())displayRules(); while(Running) { GameOver = false; attempt = 1; createTheNumber(); System.out.println("OK, I HAVE A NUMBER IN MIND."); while(!GameOver) { guess = askForGuess(attempt); if(checkGuess(guess)) { System.out.println("YOU GOT IT!!!"); score++; GameOver = true; } else { attempt++; if(attempt > 20) { displayGameOver(); GameOver = true; } } } System.out.println("*** GAME OVER ***"); Running = askToPlayAgain(); } System.out.println("A " + score + " POINT BAGELS BUFF!"); System.out.println("HOPE YOU HAD FUN. BYE!"); } private int askForGuess(Integer attempt) { int ans = getIntInput("GUESS #" + attempt); while(true) { if(ans < 0) { System.out.println("PLEASE GUESS A NON-NEGATIVE NUMBER."); } else if(ans < 100 || ans > 999) { System.out.println("PLEASE GUESS A NUMBER BETWEEN 100 AND 999."); } else { return ans; } } } private void displayRules() { System.out.println("I WILL THINK OF A THREE-DIGIT NUMBER. YOU WILL TRY TO"); System.out.println("GUESS MY NUMBER AND I WILL GIVE YOU CLUES AS FOLLOWS:"); System.out.println(" PICO - ONE DIGIT CORRECT BUT IN THE WRONG POSITION"); System.out.println(" FERMI - ONE DIGIT CORRECT AND IN THE RIGHT POSITION"); System.out.println(" BAGELS - NO DIGITS ARE CORRECT"); System.out.println(); System.out.println("YOU GET TWENTY TRIES BEFORE YOU LOSE."); System.out.println(); } private void displayGameOver() { int number = makeIntFromArray(Digits); System.out.println("OH WELL, THAT'S TWENTY GUESSES."); System.out.println("MY NUMBER WAS " + number + "."); } private Integer makeIntFromArray(Integer[] arrayIn) { return (arrayIn[0] * 100) + (arrayIn[1] * 10) + arrayIn[2]; } private Integer[] makeArrayFromInt(Integer valIn) { Integer[] retArray = {0,0,0}; retArray[0] = valIn / 100; retArray[1] = (valIn - (retArray[0] * 100)) / 10; retArray[2] = (valIn - (retArray[0] * 100) - (retArray[1]) * 10); return retArray; } private void createTheNumber() { Boolean gotNum = false; Digits[0] = generateRandomNumber(1, 10 , false); while(!gotNum) { Digits[1] = generateRandomNumber(0, 10, false); gotNum = (Digits[1] != Digits[0]); } gotNum = false; while(!gotNum) { Digits[2] = generateRandomNumber(0, 10, false); gotNum = (Digits[2] != Digits[0] && Digits[2] != Digits[1]); } } private Boolean checkGuess(Integer guess) { Integer[] temp = makeArrayFromInt(guess); Integer[] vals = {0,0,0}; if(temp[0] == Digits[0]) vals[0] = 1; if(temp[1] == Digits[1]) vals[1] = 1; if(temp[2] == Digits[2]) vals[2] = 1; if(makeIntFromArray(vals) == 111) return true; if(vals[0] == 0) { if(temp[0] == Digits[1] || temp[0] == Digits[2])vals[0] = 2; } if(vals[1] == 0) { if(temp[1] == Digits[0] || temp[1] == Digits[2])vals[1] = 2; } if(vals[2] == 0) { if(temp[2] == Digits[0] || temp[2] == Digits[1])vals[2] = 2; } if(makeIntFromArray(vals) == 0) System.out.println("BAGELS"); else { if(vals[0] == 1)System.out.print("FERMI "); else if(vals[0] == 2)System.out.print("PICO "); if(vals[1] == 1)System.out.print("FERMI "); else if(vals[1] == 2)System.out.print("PICO "); if(vals[2] == 1)System.out.println("FERMI"); else if(vals[2] == 2)System.out.println("PICO"); } return false; } // --- COMMON LIBRARY --- // private Integer generateRandomNumber(Integer minimum, Integer maximum, Boolean show) { Integer value = rnd.nextInt(maximum)+minimum; if(show)System.out.println(value); return value; } private Boolean isAnInt(String input) { try { Integer.parseInt(input); return true; }catch(Exception ex) { return false; } } private String askAQuestion(String prompt) { while(true) { System.out.print(prompt + "?"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { String ans = br.readLine(); if(ans == null || ans == "") { System.out.println("PLEASE ANSWER THE QUESTION."); } else { return ans.toUpperCase(); } }catch(Exception ex) { } } } private Integer getIntInput(String prompt) { while(true) { System.out.print(prompt + " "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { String ans = br.readLine(); if(isAnInt(ans)) { return Integer.parseInt(ans); }else { System.out.println("YOU DID NOT ENTER AN INTEGER."); } }catch(Exception ex) { } } } private Boolean askToPlayAgain() { while(true) { String ans = askAQuestion("DO YOU WANT TO PLAY AGAIN"); if(ans.equalsIgnoreCase("YES") || ans.equalsIgnoreCase("Y")) return true; if(ans.equalsIgnoreCase("NO") || ans.equalsIgnoreCase("N")) return false; System.out.println("PLEASE ANSWER \"YES\" OR \"NO\""); } } private Boolean askToSeeTheRules() { while(true) { String ans = askAQuestion("DO YOU WANT TO SEE THE RULES"); if(ans.equalsIgnoreCase("YES") || ans.equalsIgnoreCase("Y")) return true; if(ans.equalsIgnoreCase("NO") || ans.equalsIgnoreCase("N")) return false; System.out.println("PLEASE ANSWER \"YES\" OR \"NO\""); } } }