import sraja.lib.java.*; import java.util.*; // a pair of counters to keep track of the number of heads and tails // following a sequence of 4 guesses public class Memory { // an inner class class HTcounter { int nHead=0, nTail=0; public String toString () { return "[H: " + nHead + " T: " + nTail + "]"; } } private Hashtable store = new Hashtable (); char retrieve (ShiftBuffer sb) { String key = sb.toString(); if (store.containsKey (key)) { HTcounter htc = (HTcounter) store.get(key); return (htc.nHead > htc.nTail ? 'h' : 't'); } else { return (Math.random() > 0.5) ? 'h' : 't' ; } } void store (ShiftBuffer sb, char guess) { HTcounter htc; String key = sb.toString(); if (store.containsKey (key)) { htc = (HTcounter) store.get(key); } else { // make a new pair of counters and store them in memory htc = new HTcounter (); store.put(key,htc); } if (guess == 'h') htc.nHead++; else htc.nTail++; } public String toString () { return "Memory:\n" + store; } // for testing public static void main (String[] args) { Memory mem = new Memory (); Stdio.println (mem.toString()); ShiftBuffer sb = new ShiftBuffer (4); mem.store(sb,'h'); sb.shift('t'); mem.store(sb,'h'); mem.store(sb,'h'); System.out.println (mem); } }