// A program to play a simple game of prediction. // Raja Sooriamurthi import sraja.lib.java.*; class MR { private static final int MAX = 25; // setup a memory to keep track of past performance Memory mem = new Memory (); // the shift buffer keeps track of the immediate last 4 guesses ShiftBuffer sb = new ShiftBuffer (4); // ************************* // play // ************************* public void play (Applet apl) { char guess, prediction; int right = 0, wrong = 0; do { Stdio.println(); Stdio.println ("Guess head or tails and i'll predict your guess."); // 1. make a predictions prediction = mem.retrieve(sb); // 2. retrieve a guess guess = Stdio.readChar ("What is your guess [h/t] ? "); // 3. check if (guess == prediction) { Stdio.println ("Yes!. I too predicted " + prediction); right++; } else { Stdio.println ("No. I predicted " + prediction); wrong++; } // 4. store i back in memory mem.store (sb, guess); // 5. incorporate the new guess into the short term memory sb.shift (guess); Stdio.println ("Score = " + right + " | " + wrong); } while ((right < MAX) && (wrong < MAX)); // print the final outcome Stdio.println(); Stdio.println("Final tally = " + right + " | " + wrong); Stdio.println ((right > wrong) ? "I out guessed you" : "You beat me :-("); // if (right >= wrong) // Stdio.println("I out guessed you!"); // else // Stdio.println("You beat me :-("); } // ************************* // main // ************************* public static void main (String[] args) { MindReader mr = new MindReader (); Stdio.println (" Welcome to MindReader"); do { mr.play (); } while (Stdio.readChar("\nPlay again [y/n] ? ") == 'y'); } }