This is the game Mastermind, implemented in a command line driven Ruby script.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Josh McFarland 83bfec338c Initial commit 3 months ago
README.md Initial commit 3 months ago
mindreader.rb Initial commit 3 months ago

README.md

Mind Reader

In this lab, you will program the computer to read the user's mind.

Your program will guess in advance whether the user will choose heads or tails, then get the actual response from the user.

If your program guesses correctly, it gets a point; otherwise the user gets a point.

The first one to 15 points wins.

When run, your program might look something like this:

I am the Mind Reader!

****************************

I've made my prediction about what you will enter. Input 'h' or 't' below.

Enter 'h' or 't': h

Phooey. My prediction was wrong.

Score is now human 1, computer 0

****************************

I've made my prediction about what you will enter. Input 'h' or 't' below.

Enter 'h' or 't': t

I was right!

Score is now human 1, computer 1

****************************

I've made my prediction about what you will enter. Input 'h' or 't' below.

Enter 'h' or 't': t

I was right!

Score is now human 1, computer 2

Underneath the hood, your program will be using Ruby's Hash to track the user's responses, using that history to predict their guesses.

The "mind reading" will be implemented by remembering the last four responses and the last response that followed such a "response history".

Humans are not very good at producing genuinely random output, and this simple method is surprisingly effective at predicting their actions.

Include a SizedQueue class that accepts the maximum queue size at instantiation (e.g., queue = SizedQueue(4)).

You should be able to enqueue new items, but if enqueuing a new item exceeds the size, then an item is dequeued (and lost).

The class also includes a to_s method that returns a string'd version of the contents of the queue.

The MindReader class will include a SizedQueue and a Hash as data members.

The user of the MindReader class will provide the number of items to remember in the history.

The MindReader will also implement two methods, makeGuess and update(response).

The makeGuess method will make a guess based on the recent history.

For example, if the history is 'ttht', the MindReader will predict that the player will make the same next choice as it did the last time that 'ttht' had been guessed.

The update method will accept the user's actual guess and update its history accordingly.

The main program will use the MindReader class to play the game, playing until the human or the computer gets fifteen points.