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:
- Different colours, same numbers (e.g.
R1-B1-G1
) - Same colours, sequence of numbers (e.g.
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:
- Replace all Jokers with the tile they are standing in for.
- Validate the "normalized" group with the existing code (unless step 1 threw an error because it already saw the combination is invalid).
Now, how to do step 1? I think it's fairly simple if only one Joker is allowed per group:
- If the tiles adjacent to the joker are of the same colour and the right one is 2 higher than the left one, replace with the number inbetween
- If the adjacent tiles have the same number but different colours, replace with a colour that is not yet present.
- And the same but modified a bit if the Joker is at the beginning or end
Unfortunately, multiple Jokers are allowed, which makes this a bit more complex, and I'm stuck on how to solve that.