Posted by & filed under いろいろ.

This week I am going to write about the game “Ma-Jan”. I was told about this by takamura-san, inagaki-san and sugawara-san. Thanks to them for explaining me the rules.Firstly I will describe the rules and then the algorithm.
Disclaimer: The rules listed are my interpretation of the game. They might be incorrect. Similarly the algorithm looks correct to me, but it might have some flaws on some corner cases. Please excuse me if you find any mistakes and report it in comments.

Objective
There are 14 digits (0-9) given to you. You need to form matchings as given by the rules. The code assumes you know the 13 digits in advance and tries to find all possibilities to complete the game for you.

Rules for matchings

• From the 14 digits, you have to form 4 triplets( 3) and 1 pair (2). So 3*4 +1 *2 =14
• The triplets can be of the form, (xxx) all same numbers, or (x, x+1, x+2); i.e three consecutive numbers. So (333) and (567) is correct but (568) and (112) are incorrect.
• The pair has to be of the form (xx) same digits.So (22) is correct but not (45)
• You know the 13 digits, and want to figure out all possibilities to finish the game. So you have to write within square brackets ‘[ ]’ the matching which is expected to complete by the 14th digit. So if the 13 digits are 1112233444555, then you need to output (111)(33)(444)(555)[22] meaning that the square bracket is where the 14th card is going to be placed and according to rules it must be a 2. Note that there are other possibilities as well. The code has to report all such possibilities.

Algorithm

I have attached my code along. It is not clean and not refactored properly. But you can take a reference if you want.