MasterMind: MasterMind Class

Posted Mon, Jul 6 2009 13:00 by Deborah Kurata

This entry describes the MasterMind class from this example in further detail:

Public Class MasterMind

End Class

Properties

The properties of the class are as follows:

Private _Answer As List(Of Color)
''' <summary>
''' Gets the answer.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Answer() As List(Of Color)
    Get
        Return _Answer
    End Get
    Private Set(ByVal value As List(Of Color))
        _Answer = value
    End Set
End Property

The Answer property is a list of correct colors. This property has a Private setter because the answer itself is managed internal to the MasterMind class.

Private _Board As List(Of Peg)
''' <summary>
''' Gets or sets the board for the game.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Board() As List(Of Peg)
    Get
        Return _Board
    End Get
    Private Set(ByVal value As List(Of Peg))
        _Board = value
    End Set
End Property

The Board property is a list of Pegs. This property also has a Private setter because the board is managed internal to the MasterMind class.

Private _CurrentTurn As Integer
''' <summary>
''' Gets or sets the number of the current turn.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks>
''' The user cannot go back to any prior turn.
''' </remarks>
Public Property CurrentTurn() As Integer
    Get
        Return _CurrentTurn
    End Get
    Private Set(ByVal value As Integer)
        _CurrentTurn = value
    End Set
End Property

The CurrentTurn property counts the number of guesses. This property also has a Private setter because it is managed internal to the MasterMind class.

Private Shared _GameColors As List(Of Color)
''' <summary>
''' Gets the game colors.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Property GameColors() As List(Of Color)
    Get
        If _GameColors Is Nothing Then
            _GameColors = New List(Of Color)
            _GameColors.Add(Color.Red)
            _GameColors.Add(Color.Yellow)
            _GameColors.Add(Color.Green)
            _GameColors.Add(Color.Purple)
            _GameColors.Add(Color.Blue)
            _GameColors.Add(Color.Black)
            _GameColors.Add(Color.Orange)
            _GameColors.Add(Color.Pink)
        End If
        Return _GameColors
    End Get
    Private Set(ByVal value As List(Of Color))
        _GameColors = value
    End Set
End Property

The GameColors property defines the set of available peg colors. These are hard-coded into the application, but could instead be settable by a game configuration feature (which is not implemented in this sample). Because the set of colors is the same for every game (at least in this implementation), they are defined using a Shared property.

Private _NumberOfRows As Integer = 8
''' <summary>
''' Gets the number of rows that should be allowed in the game.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property NumberOfRows() As Integer
    Get
        Return _NumberOfRows
    End Get
    Private Set(ByVal value As Integer)
        _NumberOfRows = value
    End Set
End Property

The NumberOfRows property defines the number of rows in the game. This implementation defines a default value of 8 rows. This could instead by set by the user with a configuration feature (which is not implemented in this sample).

Private _NumberOfHoles As Integer = 4
''' <summary>
''' Gets the number of holes in each row that should be allowed in the game.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property NumberOfHoles() As Integer
    Get
        Return _NumberOfHoles
    End Get
    Private Set(ByVal value As Integer)
        _NumberOfHoles = value
    End Set
End Property

The NumberOfHoles property defines the number of holes in each row in the game. This implementation defines a default value of 4 holes. This could instead by set by the user with a configuration feature (which is not implemented in this sample).

Constructors

The following are the constructors defined in the MasterMind class:

''' <summary>
''' Construct the game with the default holes and rows
''' </summary>
''' <remarks></remarks>
Public Sub New()
    ' Initialize the game
    InitializeGame()
End Sub

''' <summary>
''' Construct the game with a defined number of holes and rows.
''' </summary>
''' <param name="holesPerRow"></param>
''' <param name="rows"></param>
''' <remarks></remarks>
Public Sub New(ByVal holesPerRow As Integer, ByVal rows As Integer)
    Me.NumberOfHoles = holesPerRow
    Me.NumberOfRows = rows

    ' Initialize the game
    InitializeGame()
End Sub

When a new instance of the game is created, the InitializeGame method is called to set up the board and define an answer.

Methods

The following are the MasterMind class methods:

#Region " CreateAnswer"
    ''' <summary>
    ''' Creates the answer for the game
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub CreateAnswer()
        Dim maxColorIndex As Integer = GameColors.Count - 1
        Dim colorIndex As Integer

        ' Clear the answers
        Answer = New List(Of Color)

        ' Set up for random numbers
        Dim randomColor As New Random()

        ' Build the answer
        For hole As Integer = 0 To NumberOfHoles - 1
            colorIndex = randomColor.Next(0, maxColorIndex)

            Answer.Insert(hole, GameColors(colorIndex))
        Next hole

    End Sub
#End Region

The CreateAnswer method uses the Random .NET Framework class to create a valid answer. Notice that this is a private method and cannot be called directly.

#Region " CreateBoard"
    ''' <summary>
    ''' Creates the board for the game.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub CreateBoard()
        ' Create the board as a list of potential pegs
        Board = New List(Of Peg)

        Dim peg As Peg

        ' Build a control for each hole in the board.
        For rowIndex As Integer = 0 To NumberOfRows - 1
            For pegIndex As Integer = 0 To NumberOfHoles - 1
                ' Create a peg
                peg = New Peg(pegIndex, rowIndex)

                ' Add it to the board
                Board.Add(peg)
            Next
        Next

    End Sub
#End Region

The CreateBoard method builds the structure of the board with the defined number of rows and holes. Notice that this is a private method and cannot be called directly.

#Region " InitializeGame"
    ''' <summary>
    ''' Initialize the game.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub InitializeGame()
        ' Create the board
        CreateBoard()

        ' Create the answer
        CreateAnswer()

        ' Initialize the try
        CurrentTurn = 1
    End Sub
#End Region

The InitializeGame method creates the board and defines the answer. It is also a private method that is called when the game is constructed.

#Region " ProcessGuess"
    ''' <summary>
    ''' Processes the user's guess.
    ''' </summary>
    ''' <remarks></remarks>
    Public Function ProcessGuess() As Boolean
        ' Row is 0-based; turns are 1-based
        Dim row As Integer = CurrentTurn - 1
        Dim allCorrect As Boolean = True

        ' Get the pegs for the row
        Dim rowPegs = Board.Where(Function(p) p.Row = row)

        For Each p As Peg In rowPegs
            If p.PegColor = Answer(p.Column) Then
                ' This answer is correct
                p.Correct = True
            Else
                ' This answer is incorrect
                p.Correct = False
                allCorrect = False
            End If
        Next

        ' Increment the turn
        CurrentTurn += 1

        Return allCorrect
    End Function
#End Region

The ProcessGuess method is a public method that is called by the UI each time the user is finished with a row and wants to submit it as a guess.

Download the sample code (that is currently only in VB) from here.

Enjoy!

Filed under: , ,

Leave a Comment

(required) 
(required) 
(optional)
(required)