Applying OOP to Simple Games: MasterMind
Mon, Jul 6 2009 12:36
Someone on the MSDN forums recently asked how to apply object-oriented programming (OOP) principles to a basic game. So I thought it would be an interesting project to develop a very simple sample game using OOP.
I selected MasterMind because it is a relatively simple game. If you are not familiar with the game, you can find a description and picture here.
Defining the Classes
The first step in using OOP with a game is the same as with any application … define the "nouns”.
These are the first nouns I came up with:
- Code maker player
- Code breaker player
- Answer pattern
- Guess pegs
- Key (or Feedback) pegs
The next step is to think through each of these nouns and determine which make sense as classes for building the game.
The application will need to keep track of all of the facets of the game. So a game class makes sense. Since the class will only describe the MasterMind game, I selected to call this class “MasterMind”.
Hmmm. Did the game need a separate board class? Other than defining the board layout, there was not much that the board itself needed to do. So the game could have a Board class with one method to create the board. Or the MasterMind class could have a Board property and the method to create the board. I selected to make the board a property to keep this “introduction to OOP” example as simple as possible.
Code Maker Player
This is the player that defines the pattern that is the correct answer. One of the nice things about playing MasterMind as a computer game is that you don’t need this player. The game itself can select a valid correct answer.
Code Breaker Player
This is the player that is guessing the pattern and is the user of your game. You could track the name of the player and their best score. In “phase 1” of this simple game, the decision was made not to implement a player class. This can be implemented in “phase 2”. For now, every player is a guest and no score is retained after a game is complete.
The pattern of pegs that defines the answer does not really need to perform any actions. It is more a property of a particular game.
As the game progresses, the user puts pegs into the board as a guess. Pegs are a key part of the game, so I selected to include a “Peg” class.
Key (Feedback) Peg
With a board game, it is difficult to draw onto each guess peg to define whether it is correct or incorrect and still reuse the pegs in a new game. Instead, in the real-world game pegs are added to the sides to depict which are correct or incorrect. This is not necessary in the computer game because each guess peg can be marked as correct or incorrect.
Defining the Properties and Methods
The next step is to define what data that each class retains (called properties in OOP) and what functionality that the class provides (called methods in OOP).
After reviewing the nouns, only two classes were defined:
This class manages the basic game including the game board and game play. Because this game is on the computer, there is no need for the user to “pick up” a peg of the desired color and place it into a hole in the board. Rather, the board is a set of “blank” pegs. The user can select to color each peg in a row to form a guess. The correct pegs are left in tact. The incorrect pegs are x’ed out.
The MasterMind class needs properties such as:
- Board: The board property defines the set of pegs that make up the game board. The user can then set the color of each peg in a row to form a guess.
- NumberOfRows: The number of rows to display in the game.
- NumberOfHoles: The number of holes per row to display in the game.
- GameColors: The set of colors used by the game.
- Answer: The list of colors that form the answer.
- CurrentTurn: Keeps track of the number of turns required to find the answer.
The basic set of methods for the MasterMind class include:
- CreateBoard: Creates the internal board structure based on the number of rows and number of holes per row. NOTE: Since good coding practice dictates separating our “business objects” from our UI, this code does not draw the board. Rather, it creates the internal data structures for managing the board.
- CreateAnswer: Uses the Random .NET features to randomly pick a set of colors that form the correct answer.
- ProcessGuess: Checks the colors that the user defined for pegs in a particular row and x’s out the incorrect pegs.
The Peg class manages a particular hole in the game board. The Peg class needs properties such as:
- Column: Column on the game board.
- Row: Row on the game board.
- PegColor: Color of the peg as defined by the user.
- Correct: Whether or not the peg color is correct.
Using the Classes
The user interface portion of the application creates an instance of the MasterMind class and calls its properties and methods as needed. The UI has the code that draws the game board using the MasterMind class and Peg class properties.
Download the sample code (that is currently only in VB) from here.