Monday, November 1, 2010

Mastermind!

I remember playing mastermind all the time when I was a little kid, so it was fun to have it appear again in college.  And where else would it appear besides CS?  This week, we had the choice between a couple games (Mastermind and Nim) and the creation of phylogenetic trees.  I ended up creating both games, but Mastermind was generally the more interesting because of the ability to customize the graphic.

The program itself began with some simple output into the python shell (the board is simply printed onto the screen as shown):

The output for a game of Mastermind.
While this is not all that exciting, and really was not even that hard to program, the optional extra credit is where the game actually got interesting.

But first, some logistics.  Both versions of the game (text and graphic) allow for the selection of the number of holes, the number of rows, and the number of colors.  This prompting can be seen at the top of the image above.  Of course, the program tests to make sure that this input is valid (meaning it is greater than zero) before allowing the user to continue.  The program itself generates a random code which the user must then guess in the number of rounds (rows) specified.  When the guess is completed (and checked for validity), the program calculates and prints the score before the user's next turn.  It is a simple game of mastermind, and extremely easy to implement.

The fun part comes in the graphic, which evenly spaces the rows and holes so that it fits within the frame of the screen.  I was also able to include colors (which is much more bright and exciting) instead of simply just numbers.  However, each number is still tied to a color in the graphic game in order to allow for easy of user input.

The games can range from fairly simple (only three possible colors):

I could have won sooner if I was paying attention.
Notice, the code is displayed upon victory and a winning message is printed at the top.

But the games can get much more complex with the addition of more holes and more colors:

I had some pretty good luck on this one.

I just could not guess that last color.
In this picture, unfortunately, I lost.  Notice the message at the top: I included an option to view the code if it is not guessed at some point during the game, but in this case, the code is not visible.

All of these graphic displays were controlled by text input similar to the first picture, the only difference being that the board is displayed graphically instead of as text in the python shell.

There was another extra credit option: to implement a code by which the computer would guess a code chosen by the user.  But I ran out of time this week, and was unable to actually get to this part of the problem.  As usual, it was all a lot of fun, with a generally pleasing result (though it had a number of annoying bugs along the way).

Too much procrastination happened this weekend, so I still have quiet a bit to do.  No more fun and games: back to work.

1 comment:

  1. Sweet! I had to program a airline seating system. Based on customer input, I had to reserve the right seats. Smoking? No smoking? Aisle? Window? Exit Row?

    It was really annoying to program that on a dot matrix terminal. Let me tell you.

    ReplyDelete