Building the Wall
The first step before the game can be played is that the four "walls" need to be built. Physically, this involves shuffling the deck and stacking the tiles into four rows of a certain length and 2 tiles tall.
In code, this is broken up into a few steps:
Determine the size of the deck
Depending on the variation, some of the "suits" are excluded. In the standard variation, there are the following tile quantities:
Suit | Examples | Quantity of Each Tile |
---|---|---|
Character | ๐๐๐ | 4 per number |
Bamboo | ๐๐๐ | 4 per number |
Circle | ๐๐๐ | 4 per number |
Winds | ๐๐๐๐ | 4 of each |
Dragons | ๐๐ ๐ | 4 of each |
Flowers/Seasons | ๐ข๐ค๐ฆ๐ฉ | 1 of each |
Create and shuffle the deck
For the standard deck, there's a single argument constructor that automatically creates a deck with the correct number of tiles.
using MahjongTiles: TilePile
deck = TilePile(:standard)
๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐ ๐ ๐ ๐ ๐ก๐ก๐ก๐ก๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐
๐
๐
๐
๐๐๐๐๐ข๐ฃ๐ค๐ฅ๐ฆ๐ง๐จ๐ฉ
Then, we shuffle the deck.
using Random
shuffle!(deck)
๐ฅ๐๐๐๐๐๐๐๐๐๐
๐๐๐๐๐๐๐๐ง๐๐๐๐๐๐๐๐๐๐๐๐๐ค๐๐๐๐๐๐๐ ๐๐๐๐
๐๐๐๐๐๐ฃ๐๐๐ ๐๐๐๐๐๐
๐๐๐๐ก๐๐๐๐๐๐๐๐ก๐๐๐๐จ๐๐๐๐๐๐๐๐๐๐๐๐๐๐ ๐๐๐๐
๐๐๐๐๐๐๐๐๐๐๐ฆ๐๐ฉ๐๐๐๐๐๐๐๐๐๐๐๐ข๐๐๐๐๐๐๐ก๐๐๐ ๐ก๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐
In a physical game of Mahjong, dice are rolled to determine where to start distributing tiles. However, this is mostly an anti-cheat measure and doesn't meaningfully affect the game itself. We could roll some dice and then use circshift!
to adjust the deck, but that's mostly for show (e.g. in a GUI implementation).