Algorithm to find streets and same kind in a hand

前端 未结 3 1591
有刺的猬
有刺的猬 2020-12-15 23:16

This is actually a Mahjong-based question, but a Romme- or even Poker-based background will also easily suffice to understand.

In Mahjong 14 tiles (tiles are like ca

3条回答
  •  时光取名叫无心
    2020-12-15 23:42

    There is a special case that you need to do some re-work to get it right. This happens when there is a run-of-three and a pair with the same value (but in different suit).

    Let b denates bamboo, c donates character, and d donates dot, try this hand:

    b2,b3,b4,b5,b6,b7,c4,c4,c4,d4,d4,d6,d7,d8
    
    d4,d4 should serve as the pair, and c4,c4,c4 should serve as the run-of-3 set.
    

    But because the 3 "c4" tiles appear before the 2 d4 tiless, the first 2 c4 tiles will be picked up as the pair, leaving an orphan c4 and 2 d4s, and these 3 tiles won't form a valid set.

    In this case, you'll need to "return" the 2 c4 tiles back to the hand (and keep the hand sorted), and search for next tile that meets the criteria (value == 4). To do that you'll need to make the code "remember" that it had tried c4 so in next iteration it should skip c4 and looks for other tiles with value == 4. The code will be a bit messy, but doable.

提交回复
热议问题