2

I'm trying to create a logic to analyze a winning hand in Mahjong and list out all the possible combination of a winning hand.

Let me start with some background of the game:

  • In MahJong there are 4 type of tiles - Dots, Bamboos, Characters and Honors Tiles

  • (D)ots, (B)amboos and (C)haracters come in numbers of 1-9

  • Honers consist of 7 unique tiles (namely North, East, South, West, Green, Red and White)

  • Pong is getting three of a kind

  • Chow is consecutive three tiles of the same suit, i.e. 2 Dot, 3 Dot, 4 Dot (skipping kong here to make this less complicated)

  • To get a winning hand, you will need five combinations of Pong or Chow plus a extra pair (skipping the special winning hand here to make this less complicated)

here is an example of a winning hand

2D(ot) 3D 4D ; 5D 5D 5D ; Red Red Red; 7D 8D 9D; 1C(haracter) 2C 3C ; White White


While above is obvious and have only one unique solution, sometimes there will be cased where multiple combination is possible, just like below hand

1D 1D 1D 2D 2D 2D 3D 3D 3D 4D 4D 4D 5D 5D 5D 6D 6D

this can be interpreted easily as 5 Pong + a pair of 6D winning hand

111; 222; 333; 444; 555; 66

but it is also possible to be a winning hand of

123; 123; 123; 444; 555; 66

111; 222; 33; 345; 456; 456


Now onto my goal -

since different winning hand will score different points, I'm trying to build a logic to test the 17 tile hand and compare all the possible winning combination to get the highest score hand. and this is where I am stuck in, my current logic is:

1. for each tile in hand, search for each other tile and see if can form combination
2. remove the matched 3 tile from the array and add them to the winning hand array
3. repeat step (1) for the rest of the tile
4. only the pair should be left - add this to winning hand array and get winning hand[1]

after this I'm stuck - what can I do to avoid my loop to get the same ouput? i can't just ask the code to skip the 1D 1D 1D pong because as illustrate above it's also possible for other winning hand to have that pattern. and it'll be also super slow if I brute force all the 17! possible combination and filter out the non winning hand.

Would be grateful if anyone can help me with a logic flow! Thanks

P.S. and if it matters I'm doing this in js

Osman Wong
  • 170
  • 9

0 Answers0