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:

SuitExamplesQuantity 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).