11
2
Tichu is a card game in which players take turn playing sets of cards from a deck consisting of a standard 52-card deck, plus 4 additional cards:
- the dragon, which has a greater value than any other card
- the phoenix, which can act as a wildcard
- the dog, which passes the turn to your partner
- the Mah Jong, which has value 1 (and the person holding it plays first)
The first player (who is said to have "lead") can choose to play one of the following types of card combinations:
- a single (e.g.
6
) - a pair (
JJ
) - a triple (
555
) - a full house (
QQQ33
) - a triple and a pair - a straight (
56789
) - 5 or more consecutive cards - a tractor (
223344
) - any consecutive sequence of pairs
Subsequent players are then only allowed to play a set of cards of the same type, but strictly higher. For example, QQ
can be played on top of JJ
, but QQKK
cannot (it is a tractor, not a pair). Full houses are ordered by the triple (e.g. 77722
> 44499
), and straights and tractors must be the same length (456789
cannot be played on top of 23456
). Aces are high.
There is one exception: any 4 of the same card is a bomb, and can be played on top of anything except a higher bomb.1
The dragon can be played by itself on top of any single or with lead (but nowhere else). The phoenix, aside from being a wildcard, can also be played on top of any single except the dragon.2 The dog can only be played by itself with lead and immediately ends the turn.3
Your challenge is to determine whether a given Tichu play is valid, given the previous play.
You may accept both plays in any order as either lists of integers or strings -- in either case, you may choose any mapping of cards to integers/characters. If there was a previous play, it will always be valid, and if not (i.e. the player has lead), the first input will be the empty array/string (matching the type of the other input). The cards are not guaranteed to be given in any particular order.
Your output must be selected from a set of exactly two distinct values, one of which indicates the play is legal and one that indicates it is not.
There is no need to test for whether the set of cards actually exists in the deck (e.g. 77766
followed by 88877
is impossible because there are only four 7's) -- such cases will never be given.
In the following test cases, 234567890JQKA
represent 2 through ace, and RPD1
represent the dragon, phoenix, dog, and Mah Jong respectively. The empty string is shown here as -
. These plays are legal:
6 J
JJ QQ
555 KKK
44499 77722
23456 56789
223344 QQKKAA
49494 72727
A R
A P
P R
66 7P
P6 77
58304967 6P0594J7
5P304967 680594J7
57446765 788657P5
- D
- 1
- 12345
3344556677889900 JJJJ
5555 7777
And these are not:
9 3
66 55
888 444
44 888
77722 44499
44499 777
44499 777JJJ
45678 34567
34567 456789
556677 334455
5566 778899
72727 49494
A A
R A
R P
77 RP
77 6P
P7 66
680594J7 5P304967
6P0594J7 58304967
57446765 3645P536
1 D
2 D
2 1
- 1234
7777 5555
- 223355
1: actually, a straight flush is also a bomb, but since this is the only place in the game that the suit of the cards matter, I have chosen to leave it out for simplicity's sake
2: the value of the phoenix played on top of a card with value n is actually n+0.5 (a phoenix on a 9 is a 9 and a half); since this requires knowledge of additional history to adjudicate, no test cases involve a single played on top of a single phoenix
3: so the first input will never be dog
@Arnauld Not all are valid combinations (e.g.
777JJJ
,1234
,223355
). However,RP
is not valid: "The dragon can be played by itself on top of any single or with lead (but nowhere else)." – Doorknob – 2019-09-25T15:41:57.677Indeed, I was just about to edit my comment to say that all first hands seem to be always valid. – Arnauld – 2019-09-25T15:43:44.233
1@Arnauld Ah - yes, you may assume all first hands are valid. – Doorknob – 2019-09-25T15:46:01.540
It may be worth mentioning that a Phenix may not be used as a wildcard in a bomb.
5555 777P
should be an invalid play, but both current answers mark it as valid. – Jitse – 2019-09-30T08:24:35.093