import java.util.ArrayList; import java.util.Scanner; /** * The classic Game described at U of Washington Description: * https://sites.math.washington.edu/~mathcircle/mmc/mmc2010/PicoFermiBagel.pdf * @author Chris Thiel, ofmCap * @version May 28, 2021; */ public class PicoFermiBagel { private int[] code; /* Constructors */ public PicoFermiBagel() { this(3); } public PicoFermiBagel(int size) { code = new int[size]; // make a random code: // makeNonRepeatingCode(); // or set the code for testing clues: code = intToArray(489); } private int[] intToArray(int n) { int size = ("" + n).length(); int[] arr = new int[size]; for(int d = size-1; d >=0; d--) { arr[d] = n % 10; n /= 10; } return arr; } /** * makeNonRepeatingCode() will fill the secret * code array with random digits that have * no two alike */ public void makeNonRepeatingCode() { // your code here //the first digit cannot be zero } public void makeRepeatingCode() { code[0] = 1 + (int)(9*Math.random()); // first digit cannot be a 0 for(int i = 1; i < code.length; i++) { code[i] =(int)(10 * Math.random()); } } /** * The clue method is like the U of W description: * "Bagel" returned if no digit correct * "Pico" for every digit found in the wrong place * "Fermi" for every digit found in the right place * * eg.: if the code is "489," the clue for guess "418" * would be "Pico Firmi" (the 8 is the Pico, the 4 * is the Firmi, but the guesser would not know which * digit caused which clue). */ public String clue(int guess) { if ( (""+guess).length() != code.length) return " Your guess should have " + code.length + " digits."; // replace with your code return "clue code not yet written"; } /** * clueForKids gives better clues for kids * A clue for every digit is given in the correct order * * eg.: If the code is "489" the clue for guess "418" * would be "Firmi Bagel Pico" so the kid would know * the 4 is correct, the 1 is incorrect and the 8 * is in the wrong position * * @param guess * @return */ public String clueForKids(int guess) { if ( (""+guess).length() != code.length) return " Your guess should have " + code.length + " digits."; return "clueForKids code not yet written"; // replace with your code } public boolean correct(int guess) { return guess == code(); } /** * @param n a single digit number * @return true if n is in the code array */ private boolean isPico(int n) { for (int d:code) if (n == d) return true; return false; } /** * code() converts the int array into an int * @return an int that represents the secret code */ public int code() { int c = 0; for (int d = 0; d < code.length; d++) { c = 10*c + code[d]; } return c; } /* Static Methods */ public static int ask(Scanner kb, String question) { System.out.print(question+" "); String answer = kb.nextLine(); return Integer.parseInt(answer); } public static void main(String[] args) { Scanner kb = new Scanner(System.in); System.out.print("Welcome to Pico, Fermi, Bagel!"); PicoFermiBagel game = new PicoFermiBagel(); System.out.println ("diag: ("+ game.code() +")"); int guessCount = 1; int guess = ask(kb, "Guess a 3 digit number:"); System.out.println( game.clue(guess)+"\n" ); while (!game.correct(guess)) { guessCount++; guess = ask(kb, "Guess a 3 digit number:"); System.out.println( game.clue(guess)+"\n" ); } System.out.println("It took you "+ guessCount+" guesses"); } }