Sei sulla pagina 1di 5

Christopher Pina

CST338 Final: MasterMind MVC

Mastermind Game MVC Implementation

Understanding the Game

This is a simple code breaking game in which the computer will create a
random four color code chosen from eight possible colors. The code can have
repeating colors. The user will be tasked with guessing the code. After every
guess the computer will provide the user with feedback. The computer will
indicate how many of the colors were guessed correctly and how many
colors were guessed correctly and in the appropriate position.

Implementation: Model Classes

Guess Class: Contains all data for a single guess from the user
Key Private Member Data
o Int [ ] Code array of integer values. The length of the array is
determined by the length of the code. Each integer values
represents a color value.
o Int positionMatches, colorMatches integer values to store the
number of matches in the current guess.
o codeLength integer value to determine the length of the secret
code and length of each guess.
Key Methods
o Guess(int[ ], int, int): basic constructor
o Get methods: basic get methods for all private members
o Set methods: basic set methods for all private members
o toString: useful method for testing. Converts Guess into
displayable String.

MasterMindFramework Class: main model class that contains all data for the
game.
Key Private Member Data:
o int codeLength: integer value to determine the length of the
secret code and length of each guess.
o int currentGuessIndex: points to currentGuess in guessArray.
Allows game to track number of guesses to avoid errors.
o int guessesAllowed: number of guesses allowed.
o Guess[ ] guessArray: array of Guess objects. Length determined
by number of guesses allowed.
Key Methods:
o MasterMindFramework(int, int): constructor that requires integers
for guessesAllowed and codeLength
o addGuess(int [ ]): adds guess to guessArray from an integer
array. Uses the Guess constructor to create new Guess.
o CheckCurrentGuess(): void method that compares current guess
(determined by currentGuessIndex) to the secretCode. This is
one of the more complicate methods. The method creates two
integer arrays with each array index representing a color value.
When the method encounters a color value in the secret code
and in the users guess, the method increments the value at that
index in the integer arrays it created. By comparing the values of
the at each index, the program will determine how many times a
color value appears in both the secret code and the user guess.
The minimum of these two values provides the number of color
matches found in the users guess. The method also compares
the value of the guess to the value of the secret code at each
index to determine the number of position matches. The number
of position matches is subtracted from the number of color
matches to avoid double counted a correct guess.
o checkForWinLoss(): Method checks for a win by comparing the
number of position matches of the current guess with the length
of the code. If the two values are equal, then the user has
guessed the color and position of every color. The method also
checks for a loss by comparing the currentGuessIndex to the
number of guesses allowed. The method updates the class
Booleans accordingly.
o Get Methods: all basic get methods should be included
o Set Methods: all basic set methods should be included
o CreateSecretCode(): method uses a random integer method to
create the secret code.
Testing: Both of these methods can be used to test the logic of the
checkGuess function before any GUI is implemented. Use toString() in
Guess along with System output to test.

Implementation: View Classes

GuessView Class: handles all GUI functionality for a single user guess.
Key Private Member Data:
o JLabel[ ] colorIcons: JLabel array to store/display all color peg
icons
o Boolean fullGuess : set to true when the guess is complete. Used
to prohibit user from submitted an incomplete guess.
o Int[ ] guessValues: used to store the integer color values of the
users guess.
o JPanel pnlGuesses, pnlResults: panels to display guess and
results.
Key Methods
o addColorIcon(int): this is the most crucial method of this class. It
takes an integer value (later provided by an ActionListener) to
change the color icon of the current colorIcon. Provides most
user interaction.
o addResults(int, int): adds results to the Guess panel based on the
number of matches for the current guess. This method is
responsible for providing feedback on the users guess.
o display(): Display method used to add the calling Guess object to
the pnlGuesses. Display allows the controller to update the view.
MasterMindBoardView Class: this class creates the main window and board
for the Mastermind games. The board has four major areas (pnlColorTray,
pnlGameRules, pnlGuessArea, pnlStatus). Color tray sets the available colors.
Colors will be clickable buttons that place the clicked color on the current
guess. Every click of a color button will iterate through the guess area. Only
until the submit button is clicked will the guess be submitted. GuessArea
displays all user guesses. Active guess is maintained active until the submit
button is selected and a valid guess has been added to the model.
GameRules and Status panel will display the rules of the game as well as the
win/loose message.
Implementation: View Classes

MasterMindController Class: Controller class handles all interactions between


the views (GUI) and the model.
Key Responsibilities:
o Controlled adds ActionListeners to all button in the view. This
is done by creating three inner classes that handle the user
interaction with the color buttons, submit button and new
game button.
UML

Potrebbero piacerti anche