We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Designing a Board Game (Mastermind) in C++ using MVC, State and Observer Design Patterns

High Level Design - MVC - Board Game

Let’s Design a Console Based Board Game in C++.

With this Design you can learn MVC, Observer and State Design Pattern. Moreover, with the same design you can also implement any other Game Like Battleship etc.

Advertisements

In this we will build Console Based Version of MasterMind Game.

Complete Code for this is available at,

https://github.com/VarunAtThisPointer/boardgame-mastermind

What is a MasterMind Game

Mastermind is a code-breaking Board Game for two players. Here one player will be computer and second player will be the user.

Its basically a board game where each Codemaker sets a pattern and Code-Breaker tries to guess that in Limited chances. Here CodeMaker is Computer and CodeBreaker is Player.

The codemaker chooses a pattern of four code pegs. Also, Duplicates are allowed, so the Computer could even choose four code pegs of the same color. The chosen pattern is placed in the four holes covered by the shield, visible to the codemaker but not to the codebreaker.

The codebreaker tries to guess the pattern, in both order and color, within twelve turns.
For each turn Player will get a Row. Each row will have four holes. For each guess player place a code pegs in that row in decoding board.

Once placed, the codemaker provides 4 Code Pegs as feedback for these 4 placed code pegs repectively.

[showads ad=inside_post]

 

These Feedback Code Pegs can be,

  • A colored or black key peg is placed for each code peg from the guess which is correct in both color and position.
  • A white key peg indicates the existence of a correct color code peg placed in the wrong position.
  • If Both Color and position are wrong then no Peg is provided for that i.e. EMPTY is mentioned instead of that Peg.

If in these 12 Turns CodeBreaker is able to Guess color of 4 Pegs then Player (CodeBreaker) Wins else Loses.

For More Game Details check link, https://en.wikipedia.org/wiki/Mastermind_(board_game)

Now Let’s Design The Console Based Version of this Board Game in C++ using MVC Architecture and few Design Patterns Like Observer and State.

HIGH LEVEL DESIGN OF GAME

This MasterMind Game has been designed using MVC architecture.

High Level Design - MVC - Board Game

Game Model

It include,

  • Board
  • Peg Rows in Board
  • Peg slot in Peg Rows
  • State of the Game

Responsibilities Of Game Model,

  • To provide functional core of the game that includes,
    1.)  Encapsulating the appropriate data like Board, Peg Rows and already set Code and Key Pegs on them.
    2.) Exporting procedures to access the data and to perform application specific processing. Like, adding Code          Pegs in different rows
  • Act as Subject and register dependent Observers like Game View etc.
  • Notifies dependent Observers about data changes like State Change.
  • Expose services to controller for data modification like State change, game stop and start etc.

It is important to note that Game View and Game Controller depend on the Game Model. However, model depends on neither view nor controller.

Game View

The Game View renders a presentation of modeled data on console.
Responsibilities of Game View,

  • Using the information that is available to it to produce a presentation interface and different views can present the information of the model in different ways. Current Game view will fetch data from Game Model and generate a console based presentation and accepts inputs from user.
  • Accepts the inputs from user and forward service requests to Game Controller for appropriate action. Like, if user has entered any choice then forward it to controller by calling appropriate API of controller.
  • Each view defines an update procedure that is activated by the change propagation mechanism in Model with which it is registered. Game View will receive notification on Model’s state change and based on the new state Game View will change the current active Scene.

Game Controller

  • The Game controller takes action on service requests forwarded by Game View by modifying the Game Model if required.
  • Based on the current Game State in Model Controller decides what type of action it should take or just neglect the request.
  • Like Game is in MAIN_MENU state and request from view comes to add a Code Peg then Controller will reject that request because in current state this request cannot be handled.

 

STATE TRANSITION OF GAME

Game States:

states transition of Game

There can be only 4 types of Game States i.e.
MAIN_MENU:
It represents that game is in main menu and user have three choices here i.e.

  • Start the game i.e. goes to GAME_IN_PROGRESS state.
  • Go to Help Menu i.e. HELP_MODE
  • Exit the application

GAME_IN_PROGRESS
It represents that game is in progress and user have three choices here i.e.

  • Continue playing game and to be in GAME_IN_PROGRESS state.
  • Go to Main Menu i.e. MAIN_MENU
  • Either win or lose and move to GAME_ENDED state.

GAME_ENDED
It represents that game has ended and user have three choices here i.e.

  • Go to Main Menu i.e. MAIN_MENU

HELP_MODE
It represents that game help is on and user have three choices here i.e.

  • Go to Main Menu i.e. MAIN_MENU

 

LOW LEVEL DESIGN

GAME CONTROLLER

controller_class_Diagram

Game Controller contains a map of StateHandlers based on each game state i.e.
MAIN_MENU <—> MainMenuStateHandler
GAME_IN_PROGRESS  <—> GameInProgressHandler
GAME_ENDED  <—> GameEndedStateHandler
HELP_MODE <—> HelpStateHandler

When a request comes to controller then it forwards the request to a State Handlers corresponding to current active state. If that handler can handle the request then it will handle that otherwise it rejects the
request.

GAME VIEW

It acts as an Observer from Observer Design Pattern. It gets attached with the Subject i.e. Game Model
and gets update notification when Game Model sends a notification.

View_class_Diagram
Game View contains an active Scene and a map of Scene object based on each game state i.e.

MAIN_MENU <–> MainMenuScene
GAME_IN_PROGRESS <–>GameScene
GAME_ENDED <–>EndScene
HELP_MODE <–>HelpScene

Each Scene fetches the data from Game Model and then displays that on console. It also accepts user inputs and forward service requests to Game Controller for action.
It also listens for update notifications from Game Model and on receiving state change notifications it changes the currently active scene. It continuously renders the current active scene.

GAME MODEL

It acts as Subject from Observer Design Pattern. Different Observers i.e. Game View objects get attached with it. When its internal state changes, then it notifies all the attached views by sending a update notification.

 

model_class_Diagram

It contains Board objects which internally contains Peg Rows and Peg objects. It completely implements the core functionality of the Game. It also provides operations to update the data and fetch the data from Board.

Game View fetches the data from Game Model.

Game Controller modifies the data in Game Model through exposed interfaces. Game Model sends update notification to Game View on state change.

 

COMPLETE CODE

GIT Repository for complete code is MASTERMIND BOARD GAME CODE

Cart

Your Cart is Empty

Back To Shop