In the game of Rummikub, for those who don't know it, you have tiles in 4 colours and with 13 different numbers on them (so 4 x 13 = 52 unique tiles) which you must use to make groups. There are two kinds of groups:
R1-B1-G1
)G6-G7-G8
)I'm writing code that takes a list of tiles and checks if it's a valid combination. So far it works and is pretty straightforward.
It gets difficult when we introduce Joker tiles. You can use these as any tile to complete a combination (e.g. G6-R6-J
) and you can use multiple (e.g. R4-R5-J-J-R8
).
I figured that I'd validate combinations with Jokers in two steps:
Now, how to do step 1? I think it's fairly simple if only one Joker is allowed per group:
Unfortunately, multiple Jokers are allowed, which makes this a bit more complex, and I'm stuck on how to solve that.
If you insist to perform step 1 as the first thing you do, you're making trouble for yourself. It's simply computationally inefficient to approach the problem from that angle. What you would end up doing with that approach, is to try all combinations of stand-ins for the jokers. That is a bad idea.
Here's an alternative approach that will work with little effort:
Perform all those checks in turn, and you will find if a group of pieces is a valid group.
For step 3 in this, you have to consider that some sequences could be in reverse, say (joker, 3, 2, 1). to detect such cases, you can do a quick scan over the non-jokers, to see if they increment or they decrement, and then take that into account (the jokers will then have the value one less than the previous).
Notice that only in step 2 are the colors significant, and only in step 3 are the numbers significant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With