CSCI 136
Fundamentals of Computer Science II
Spring 2022

Montana Tech of The University of Montana
Computer Science & Software Engineering

ASSIGNMENT 4 - Mind Reader

In this assignment, you will be use a hash map to implement a straightforward game you can play against the computer. The fun part of this is that the computer usually wins.

This is how the Mind Reader game should work. Prior to user input, the program is going to predict whether a user is going to input a “head” or a “tail”, as if a coin has been tossed. Without being told what the computer guessed, the user will then choose to enter “heads” or “tails”. The program’s guess and the user’s input will then be compared. If the program guessed correctly, it gets a point. If not, the user gets a point. The program continues until either the program or the user reaches 25 points.

The program will keep track of past behavior of the user, and over time, build a "history" of what the user has input. Each piece of user history will consist of four consecutive head/tail choices, as shown below. Let’s say the user has entered the following 10 choices:

1 2 3 4 5 6 7 8 9 10

The first four times the program guesses what the user will input next, it has no history, and must make random guesses. After that, it may then start looking at past user behavior. In the above example, the computer has the following cases available by the 10th user input.

1. H H T H
2. H T H T
3. T H T T
4. H T T T
5. T T T H
6. T T H T
7. T H T H

For each unique set of entries, the computer can keep track of what choice the user makes next, either heads or tails, and how many times the user chose that particular choice. So, if the value we are storing for each is a 2-element integer array with the first element representing the number of times "H" was chosen, and the second the number of times "T" was chosen, the outcome would look like this:

1. H H T H 0 1
2. H T H T 0 1
3. T H T T 0 1
4. H T T T 1 0
5. T T T H 0 1
6. T T H T 1 0
7. T H T H 0 0

Thus, when the user has guessed a series of HHTH in the past, the next guess was T, so if the program sees this pattern again, it should guess the user will input an T next. Let's say that sequence did occur again, and the user entered a T again. The value stored should now be:

H H T H 0 2

If the user entered the same pattern again, but this time their next choice was H, the stored values would be:

H H T H 1 2

If the exact pattern isn’t available in the hash map, the program should simply make a random guess. This will happen before the computer has enough data (the first few rounds of play), or if a pattern has not yet been seen. The computer will also need to make a random guess if the number of times H or T was chosen is equal in the hash map.

You must use a hash map (dictionary) where the input patterns are the keys, and the value stored has two components to keep track of the number of times H and T were chosen by the user after that key pattern was used. I used a 2 element array of integers, but you can use whatever works for you. You will also have to think about what kind of structure would work best for keeping track of user choices as they are input. What you use is up to you. In this program, I am also leaving it up to you on how to structure your methods. In other words, I am not requiring that you use a pre-defined API. Unfortunately, with no API, this also means I cannot provide you with an autograder... :(

Part of an example run interaction and output is shown below:

Enter H for heads or T for tails: H
Computer predicted: H Player chose: H
Computer: 1 Player: 0

Enter H for heads or T for tails: T
Computer predicted: H Player chose: T
Computer: 1 Player: 1


Enter H for heads or T for tails: T
Computer predicted: T Player chose: T
Computer: 25 Player: 3

Computer WINS!!!

Run your program 10 times, and report the outcomes. You can even have an unsuspecting friend run your program. How many times does the program win, how many times does the user win? What do you think this means? Write up your approach to the problem, the results you obtained, and your conclusions in a short report in the header comment of your code.

Grade ItemPoints PossiblePoints Earned
Program Compiles
Program Runs
Comments on All Classes and Methods
Hash map stores keys/values correctly
Computer prediction is based on patterns
Game operates correctly
Write Up of Results across10 Test Runs

Submission. Submit your program using Moodle. Be sure each submitted source file has the required header with your name, and a description of the program. Also, each method should have appropriate comments. For extra-credit, please submit a single zip file containing all your programs/graphics/sounds via the special extra-credit drop box at the top of the Moodle page.

I'm not sure how to get started. What do you recommend? A good way to prooceed in this assignment (and most programs) is to implement methods one at-a-time, starting with the simplest method first. After creating each method, write test code to see if the new method does what is suppose to. I would print out information as much as possible on each method you build so that you know the correct data is being used.

Extra credit (up to 5 points). If an exact pattern match is not available, you might see if other patterns that are "similar" have been used, and base your (computer) prediction off these instead of making a random guess. This is known as case based reasoning.

Page last updated: January 06, 2022