Mind Reader: a program that predicts choices
Raja Sooriamurthi, Indiana University, raja@indiana.edu

The Assignment

The theme of the assignment is to write a Java program that will "read" the mind of the user J.

The program/user interaction is as follows:

The user needs to pick either heads or tails. Before the user makes his/her pick the program will make a prediction as to whether the user is going to pick head or tail. The programs prediction is hidden from the user until he makes his actual pick. Once the user makes his pick the programs prediction is revealed. If the program correctly predicted the users pick it gets a point otherwise if the program misses the user gets a point. Who ever (program or user) gets 25 points first is deemed the winner.
Below is a console snapshot of the program in action as well as the GUI version (the GUI is from my .net version of this Java program):
               Welcome to MindReader

Guess head or tails and I'll predict your guess.
What is your guess [h/t] ? t
No. I predicted h
Score = 0 | 1

Guess head or tails and I'll predict your guess.
What is your guess [h/t] ? h
Yes!. I too predicted h
Score = 1 | 1

Guess head or tails and I'll predict your guess.
What is your guess [h/t] ? t
No. I predicted h
Score = 1 | 2

Guess head or tails and I'll predict your guess.
What is your guess [h/t] ?

...
        
Intrigueingly the program does quite well and often ends up beating the human user by correctly predicting their guesses.

Acknowledgement: This assignment is based on a problem given by Professor Gregory Rawlins.

SIGCSE description

The description of the assignment in SIGCSE nifty assignment terms is as follows:

Summary The program will predict whether a user will pick head or tails. The version I give as an assignment uses Hash tables but any form of Map will suffice. Can be written as a console application or with a GUI.
Topics Time series prediction; rudimentary AI technique in the flavor of case-based reasoning; maps and hashtables; GUIs
Audience CS1 or CS2
Difficulty Intermediate to advance. Typically takes 1 to 2 weeks.
Strengths
  • Students have enjoyed it and found it to be an interesting program to write. As mentioned earlier, the program does a pretty good job of predicting the users picks and most often ends up winning.
  • While I typically issue this assignment as an exercise involving Hashtables, it can also be used to discuss the ADT of a map and how lookup could be written even in an ArrayList based implementation.
  • I have given this assignment in two parts. In part-1 they write a console version of the program and in part-2 they wrap a GUI around it and implement it as an applet to share with their friends :-) .
Weaknesses It does seem to take the students a while to understand the conceptual flow of information in this program. While the GUI part is fun it is also tedious to do by hand. I have given this same assignment when teaching with .net and the GUI building facilities of Visual Studio have been very convenient.
Dependencies Requires a discussion of hashtables (or at least the ADT of a map).
Variants
  • As mentioned earlier, this program can be written as either a console version or a GUI based version (I typically have them write it as an applet.)
  • I will be teaching a web applications course in the Spring and am thinking of the possibility of making a Web app of this program (servlet or JSP).
  • The program builds a "user profile" in making its predictions. Until a sufficiently rich profile is built (in the hashtable) it will make random guesses. The program could be extended to store the hashtable to disk and read it back next time at the beginning of a game session. This would enable the program to even have its initial guesses based on its user profile rather than randomly determined. This would be a nice exercise in serializing a hashtable.

Implementation

The program builds a hashtable capturing the users guessing pattern. The key of the hashtable is the last four guesses made by the user. The value is a count of the number of times either heads or tails is picked by the user given a particular pattern of last 4 guesses. The high level structure of the program is: The program is written as 3 + 2 Java classes:

Core classes MindReader.java, ShiftBuffer.java, HTcounter.java
Console driver MR_console.java
GUI driver MR_applet.java

Demo

Following is the (functional) applet version (with garish colors :-) )





The difference between an AI programmer and a non-AI programmer
is that a non-AI programmer stares at his program and wonders why it doesn't work
wheras an AI programmer stares at his program and wonders why it works.

 


Extra info about this assignment: