
Game Description
This is the game of Mastermind written in C#. The game is played by clicking on a set of 4 colors and then hitting the score button. Colors can repeat themselves in this game, so be wary! The score is determined each time with the following rules:
A black peg to the right of your guess indicates that one of the pegs is in the right location in the row (although you do not necessarily know which position it is referring to). Four black pegs indicates you have won the game because all 4 colors are in the right position in the row. A white peg indicates that one of the pegs is correct, but its in the wrong position in the row. An empty peg (hollow circle), indicates that one of the colored pegs is completely wrong and is not part of the puzzle. Anotherwords, if after pressing the Score button, you have 4 hollow pegs, then you can eliminate all the pegs in the row as being part of the solution to the puzzle.
Strategy
Use the previous guesses and logic to figure out which pegs are referring to which colors.
Application Design
This is a GDI+ application and utilizes the DrawEllipse and FillEllipse routines to draw the pegs on the board. It is also a good application for examining multidimensional arrays. Below is the UML design for Mastermind:
Fig 1.2 - UML Design Reverse Engineered using WithClass 2000
You'll notice that a lot of the design is centralized around the Board class. The Board class draws the color pegboard and calls the ScoreBoard class to draw the score peg column. The ColorPanel is also drawn directly onto the form and has a routine for determining which color is chosen by the user called GetColorAt. Below is the sequence of events that happens when a user chooses a color on the panel: 
Fig 1.3 - Sequence of events after pressing ColorPanel using WithClass
The Mouse Coordinates on the form are used to determine the location of the color on the color panel and then the next available peghole in the row is filled.
The board is drawn using a 2 dimensional Grid array to store the color peg positions. The position is scaled to the appropriate location on the form:
public void Draw(Graphics g)
{
// cycle through the Grid Matrix and place the
// indicated colored peg at the indicated location
for (int i = 0; i < Grid.GetLength(0); i++)
{
for (int j = 0; j < Grid.GetLength(1); j++)
{
// a Grid value of 0 indicates an empty slot
// draw an empty hole
if (Grid[i,j] == 0)
{
Rectangle r = new Rectangle(PegHole.Left, PegHole.Top, PegHole.Width, PegHole.Height);
r.Offset(i* (PegHole.Width + SPACING), j* (PegHole.Height + SPACING));
r.Offset(MARGIN, MARGIN);
g.DrawEllipse(Pens.Black, r);
g.FillEllipse(Brushes.Black, r);
}
else
{
// there is a peg here, draw the colored peg
Rectangle r = new Rectangle(Peg.Left, Peg.Top, Peg.Width, Peg.Height);
r.Offset(i* (PegHole.Width + SPACING), j* (PegHole.Height + SPACING));
r.Offset(MARGIN, MARGIN);
Brush aBrush = Brushes.Black;
// This routine retrieves the brush matching
// the integer at the grid point
aBrush = GetBrush(Grid[i,j]);
// draw the colored peg with the determined brush
Pen aPen = new Pen(aBrush,1);
g.FillEllipse(aBrush, r);
g.DrawEllipse(Pens.Black, r);
aPen.Dispose();
}
}
}
// draw the score column
TheScore.Draw(g);
// draw the color peg choice panel
ThePanel.Draw(g);
}
Listing 1.0 - Drawing the entire board of Mastermind
The last interesting code to look at in this project is the scoring algorithm. The score is determined by first figuring out the locations that the player matched exactly and marking them. Then the white pegs are determined by going through each peg in the solution row and checking it against all the pegs of the player row. Again, arrays are used extensively here:
public int CalcScore()
{
int nExact = 0;
int nThere = 0;
int nCount = 0;
int[] places = new int[4]{-1, -1, -1, -1};
int[] places2 = new int[4]{-1, -1, -1, -1};
// match exact rows first, this is easier
for (int i = 0; i < 4; i++)
{
if (GuessingRow[i] == Grid[i, CurrentRow])
{
nExact++;
TheScore.AddBlackPeg(CurrentRow, nCount);
nCount++;
places[i] = 1;
places2[i] = 1;
}
}
// check the special case where the player may have solved the puzzle
if (nExact == 4)
{
return nExact;
}
// now check for all the white pegs, a bit trickier
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
// make sure the positions aren't the same and the pegs in the different positions
// both in the solution and the player row haven't already been determined
if ((i != j) && (places[i] != 1) && (places2[j] != 1))
{
// if the GuessedRow color matches the Grid Color at a different position
// We have a white peg
if (GuessingRow[i] == Grid[j, CurrentRow])
{
nThere++;
TheScore.AddWhitePeg(CurrentRow, nCount);
nCount++;
places[i] = 1;
places2[j] = 1;
j = 5; // force a break, (I know, you can also use break;)
}
}
}
}
return nExact; // return the # of exact (black pegs) that matched
}
Summary
This is the first cut. While playing the game, one thing I noticed would be a nice improvement would be to be able to specify the row hole you want to fill, rather than fill in order from left to right. This will come out in the next version. Have fun!

manolo perotoPosted Mar 23, 2023, 10:09 PM
Excellent work:Could you explain the CalculateFromMastermindBoard function. why are you looking for matching pegs and not a white and black evaluation?
Karthi SooryaPosted Aug 1, 2013, 5:58 AM
Nice Job
Rui SilvaPosted May 25, 2011, 4:10 PM
Very Well, And if we save all data in xml file to load when we want to make a game?
Suchan KooprasitPosted May 7, 2010, 3:14 PM
Excuse me, sir I'm load opencode Mastermild game from http://www.c-sharpcorner.com/UploadFile/desaijm/MasterMindWithDragDrop11152005021555AM/MasterMindWithDragDrop.aspx?ArticleID=27644b65-b34c-4f64-8f1d-0bb36b52958c I wanna fix program for research tool help, but i'm basic C#.Net Language Program graphic. i'm medium Language ASP.Net C#. please to guide or recommend for fix **1. the show peg. yes, Answer agree Duplicate color. I'm wanna fix Answer not Duplicate color. but, i' not understand graphic C# code. So Please, Respect
tho nguyenPosted Oct 14, 2008, 9:43 AM
it's been died. can you post again,please??????????????
max collinsPosted Feb 24, 2008, 1:02 PM
Hi I am aged 11 and love C#, and am trying to do things that will help me to learn more about programeing. I would like to start making games like mastermind or checkers or chess or somthing. Do you have any tips, helping points or guides i can please use for my programing. So far I have not managed to ecumplish much (E.G. click buttons to make a button turn different colours, and clicking buttons to make other dialoge boxes appear.) I would be very greatfull if you could help me. Thanks, Max
karen lowPosted Jul 22, 2007, 12:34 AM
Hi!I'm currently doing a mini project using programming C#.I need to hand in by next week.My lecturer only taught me the basic of c#.I'm stuck with my project.Can you help me by adding me to ur Msn?