As typical Hongkongers, a friend’s home with 4 or more people often leads to a classic activity – playing Mahjong. It’s one of the top choices, no questions asked.

Last Sunday, my girlfriend and visited a couple living outside London. Mahjong popped into our heads when one of us asked, “What are we going to do before dinner?”

The Mahjong scoring system is complex. Even Hong Kong Mahjong, with its clssical rules, includes over 15 types of winning hands and more than 10 additional scoring rules. In the past, perhaps by luck, there was always at least 1 friend who knew the scoring system. This time, no one did – not even me, the most experienced player among the 4 of us. It was quite embarrassing.

“Life would be easier if there were an app that could calculate the score of the game.”

I decided to take on my first-ever programming project. I wanted to:

  • Answer my own question.
  • Build something tangible after countless LeetCode challenges.
  • Teach myself how to build an end-to-end app.
  • Eliminate excuses, like not knowing front-end development.
  • Most importantly have fun.

I will write my work journal here to track my learning progress.

Basics

Since Mondy, I’ve been reading about Mahjong from various sources to understanding its scoring system better. There are many variations in the scoring system. People often mix up the rules, such as failing to distinguish between the scoring systems of Hong Kong Mahjong and Guangdong Mahjong.

There are 144 Mahjong tiles.

SetTypeCountExamples
HonorWinds16🀀🀁🀂🀃
Dragons12🀄🀅🀆
SuitedCharacters36🀇🀈🀉
Bamboos36🀐🀕🀘
Dots36🀙🀜🀝
BonusFlowers4🀢🀣🀤🀥
Seasons4🀦🀧🀨🀩

Melds are groups of tiles within a player’s hand. There are 4 types of melds:

  • Chow: 3 sequential suited tiles, e.g., 🀜🀝🀞
  • Pong: 3 identical tiles, e,g, 🀂🀂🀂
  • Kong: 4 identical tiles, e.g. 🀕🀕🀕🀕
  • Eyes: A pair of identical tiles, e.g., 🀏🀏

A winning hand consists of 4 sets of Chows, Pongs, Kongs and a pair of Eyes. The player who completes first wins the game. Since the probability of constructing a giving winning hand varies, different winning hands worth different Faan (a unit of score in Mahjong). The Common Hand 🀐🀑🀒 🀙🀚🀛 🀜🀝🀞 🀋🀌🀍 🀏🀏 worths 1 Faan while the All in Triplets 🀉🀉🀉 🀊🀊🀊 🀕🀕🀕 🀙🀙🀙 🀏🀏 worths 3 Faans. There are additional bonus scoring rules and special hands, which I won’t cover here.

The Mahjong scoring system is very complex, involving complex algorithms to identify winning hands. Furthermore, there are many variations over the world. It’s no wonder that online resources only provide lists of winning hands, not calculators.

This gave me a great opportunity of solving a “new” problem after practising on LeetCode for over 170 days where I found no one had discuss it before.

This week, my goal this is to build a Java backend application that identifies the basic winning hands – excluding bonus scores and special hands.

Stay tuned as I navigate tiles, code and algorithem in the weeks to come. Tell me your comments too!