// MindReader.java // Raja Sooriamurthi import java.util.*; public class MindReader { private Hashtable memory; private ShiftBuffer sb; // ************************* // MindReader // ************************* public MindReader() { memory = new Hashtable(); sb = new ShiftBuffer(4); } // ************************* // getPrediction // ************************* public char getPrediction() { String key = sb.toString(); if (memory.containsKey (key)) { HTcounter htc = (HTcounter) memory.get(key); return (htc.getHead() > htc.getTail() ? 'h' : 't'); } else { return (Math.random() > 0.5) ? 'h' : 't' ; } } // ************************* // storePlayerGuess // ************************* public void storePlayerGuess (char guess) { HTcounter htc; String key = sb.toString(); if (memory.containsKey (key)) { htc = (HTcounter) memory.get(key); } else { // make a new pair of counters and store them in memory htc = new HTcounter(); memory.put(key,htc); } // htc now contains a -reference- to the object in the hash table // hence after incrementing the appropriate counter below // we DO NOT have to store it back in the hash table if (guess == 'h') htc.incrHead(); else htc.incrTail(); // finally shift the buffer sb.shift(guess); } }