Java Games: MasterMind

MasterMind - guess the colors, in order, in the fewest number of moves.


Board Width:
Number of colors:


(Note: The above requires that Java and Javascript be enabled in your browser. If they are not enabled, you can create a sample web page to download MasterMind.class as an applet. See the comments in the source of this web page for an example, but if Java or JavaScript is not enabled in your browser for security reasons, you can't play.)


Rules of Mastermind:

The computer will select a board layout randomly. The board layout consists of some number of randomly colored "pegs". To move, press the numbered button above the color of your choice. Your goal is to guess the colors that were selected by the computer in the fewest possible moves. Each color is selected completely randomly - it may duplicate an already selected color. Mastermind is actually an elegantly simple game which, when played correctly, will provide a mental workout.

How to Move

When you move, the move you have made is compared with the secret tableau that the computer has calculated. For every peg that is the correct color and in the correct position, a black dot is displayed. For every peg that is of the correct color but is in the wrong position, and which is not already accounted for by a black dot, a white dot is displayed. Dots are displayed with black dots on the left, then white dots, and then grey dots for the complete mismatches. There is no correspondence between the displayed order of the black and white dots and the position of the correct guesses.

The "New Game" button throws away the current board and starts a new game.

The MasterMind game is a filtering game. Each move becomes a filter. You learn that you must exactly match 3 in this row, and you may match none in that row or 2 in position and 1 out of position. Each move becomes a filter, and as you apply the filters to the move space, it shrinks. To see the remaining move space, check the box next to 'Display Valid Move Count'. You should not have more in common with a line than is allowed by the response dots associated with that line, nor should have less. If a line has two black dots and one white dot, you need to have two pegs in your solution that match in color and position, and one peg which matches in color and which does not match in position, no more and no less, or it will not be the correct solution.

Have fun. I wrote the game as an exercise in learning Java and Java applets. I'm unlikely to make any major changes to it, but if you have any comments, please send them to Nick Simicich and I'll be sure to read them sooner or later. It uses the standard awt, including several of the layout manglers for positioning the various screen elements. And you should be able to play it disconnected. It does not run well under the Java appletviewer, but seems to run well under Netscape 3.01 Gold.

When I used to play MasterMind against a human, I frequently tried to outguess them regarding peg positioning and patterning. What I found was that the hardest people to play against were people who put down their position completely randomly. That is the case here - the computer has no strategy, it just tosses random numbers. To figure out what your par should be, make a random move, and then press the "Make Valid Move" button. You'll proably find that the computer arrives at a solution at between 4-7 moves. You should be able to do at least that well - that is why I only allowed a certain amount of area for the play board, as you should not need any more. If I get too many complaints, I'll put it in a scrolling panel of some sort.