---
# System prepended metadata

title: Mahjong Game

---

# Mahjong Game
## The relations between the classes
### Class Diagram
![](https://i.imgur.com/FskSQ5k.png)
[Link to Lucid](https://lucid.app/lucidchart/7442e0ae-2da4-41ec-9695-ff369e1c3d36/edit?viewport_loc=-225%2C-191%2C2744%2C1273%2C0_0&invitationId=inv_0bb08a04-516b-4d57-9e7c-cbc618b7fcf7#)
### Classes
* Mahjong
    1. Initialize the game, and create objects for the classes.
    2. Control the game flow.
    3. Call ActionChecker to get the priority of possible actions for each player. Process the actions accordings to the priority. 
    4. Send the state of the game to the scoreHandle to calculate the scores. 
* Player
    1. Store the parameters for user data.
    2. Provide information to the Mahjong and scoreHandler.
* Wall
    1. Record the tiles in the wall.
    2. Send the tiles to the players.
* Tile
    1. Belong to one of the four kinds of the tiles, SuitedTile, DragonTile, WindTile and BonusTile.
    2. SuitedTile has three derivatives Dot, Bamboo and Character.
    3. Provide the information to the ActionChecker. 
    4. Provide the information to the scoreHandler to calculate the score.
* ActionChecker
    1. Mahjong decides whether an action can be performed by the player by ActionChecker.
    2. There are WinChecker, PongChecker, KongChecker and ChowChecker.
    3. Create Action object for the Mahjong to perform an action.
* Action
    1. execute a specific action choose by the player.
* scoringHandler
    1. Calculate the score according to the final state of the game.
## OO Designs
* Open Close Principle : 
  Majong calculates score by using ScoringHandler interface. Any classes that imlement ScoringHandler interface can be used by Majong, so you can switch to any scoring rules you like without modifying other classes.
  
* Chain of Responsibility : 
  I use chain of responsibility design pattern to design ActionChecker. If a certain Action can be performed by the player, ActionChecker will add that Action to a list. After checking all kind of actions, the list will contain all th actions that can be performed by the player. The benefit of this design is that you can add or remove any actions withour modifying other classes, just need to revise the corresponding ActionChecker and Action.
## Game Flow
* In a round, a player draws a tile and discards one. 
![](https://i.imgur.com/qrXQ0gi.png)
* If the previous player discards a tile which fits your meld, the player can pick it up or pass it.
![](https://i.imgur.com/dhfEneA.png)
* If you pick it up, the system places the meld in the right hand side.
![](https://i.imgur.com/85KXHT6.png)
* If a player can pong, the system shows the option. Player needs to choose to either pong or pass it.
![](https://i.imgur.com/5vlYp3B.png)
* If a player can kong, the system would show the multiple options including kong and pong. 
![](https://i.imgur.com/4Ul7MxP.png)
* If a player can win the game, there would be an option to end the game. 
![](https://i.imgur.com/gkSuXd8.png)
* It shows the scores of each player at the end of the game and asks whether resume the game or not.
![](https://i.imgur.com/ophtu4u.png)
