I wrote the Mastermind code sample to learn more about writing an application for Silverlight for Windows Embedded. I will be sharing the lessons I learn in more detail. Check the links at the end of this article. You can use the Mastermind code sample to play a game of Mastermind. To download the code sample, go to  Mastermind Code Sample and follow the directions there.


The name "Mastermind," the toy, and distinctive likenesses thereof are the properties of Hasbro Inc. This article describes an unofficial, not-for-profit version that is not endorsed by Hasbro in any way.

The game begins with the computer selecting a series of four colors that represent the code you are supposed to break. You try to deduce the code by clicking on a color at the right of the screen and then clicking on the peg that you want to set to that color. After you have selected a color (colors can be used more than once) for each peg in the row, click the Guess button to receive feedback on your choices. The feedback comes in the form of black or white rectangles to the right of the row with your guess.

A black rectangle indicates that one of the pegs has both the correct color and the correct location. A white rectangle indicates that one of the pegs has the correct color, but it is not in the correct location. Be aware that the position of a black or white rectangle does not map to the position of the pegs on the guessing row. That is, the first rectangular feedback peg does not correspond to the first peg in the guessing row. It can refer to any peg in the row. A lack of feedback indicates that a chosen color is not part of the code.

In Figure 1, above, the player has made a guess, and the feedback is that three of the colors in that row are part of the code, but none are in the right location. The feedback does not imply that it is the first three pegs in the guess that are the right color. The feedback could refer to the first, third, and fourth pegs, or the first three pegs, or any other combination of three pegs. The purpose of the game at this point is to figure out which pegs the feedback does refer to by refining future guesses.

The game ends when you either guess the correct code or you make eight guesses without successfully guessing the code. When a game ends, the bottom four pegs display the code that you were trying to break.

The Mastermind code sample demonstrates how to create a simple Silverlight storyboard in Expression Blend 3, and then provide the code for the state transitions in Microsoft Visual Studio 2008. You can also see how to create brushes programmatically and how to add a title bar to a Silverlight-based app. Links to these articles can be found below.