TopCoder problem "HeartsGame" used in SRM 220 (Division I Level Two)

Problem Statement


Hearts is a card game in which each of four players is dealt 13 cards from a standard deck. Play always starts with the two of clubs. We'll call the player who starts with the 2 of clubs player 1. Play continues clockwise from this player, and we'll call the player to the right of player 1 "player 2", and so on. Once all four players have played one card (this is called a "trick"), the player who played the highest card in the same suit as the first card played sets aside the four cards played and leads the next trick. At the end of the round (exactly thirteen tricks are played, to exhaust the players' hands), points are counted up for each player. The object of the game is to have the lowest number of points. Each heart taken by a player is worth one point, and the Queen of Spades is worth 13 points. However, if one player gets all of the hearts and the Queen of Spades, rather than getting 26 points, they give each of the other three players 26 points (this is often called "shooting the moon"). Scoring should be counted the same even if the game isn't legit; A player gets the points on the tricks that player takes even if he only took those points as a result of other players cheating.

During each trick, the player who has control of the game (because he either took the last trick or he has the 2 of clubs if it's the first trick) plays his leading card, and all other players must play a card of the same suit if they have one. If they do not have any cards of that suit, they may play any other card in their hand instead. The leading player may not lead with a heart unless hearts have been "broken", which means that some player has played a heart in a previous trick (including illegally playing a heart), or unless they have only hearts left in their hand. Also, no scoring cards (hearts or the Queen of Spades) may be played on the first trick, unless that player has no non-scoring cards in his hand.

You will be given a String[] tricks, which will have exactly 13 elements, each with exactly 11 characters in the format <suit1><card1> <suit2><card2> <suit3><card3> <suit4><card4>. The suits will be either 'C' for clubs, 'D' for diamonds, 'S' for spades, or 'H' for hearts. The cards will be, in ascending order of value, '2', '3', '4', '5', '6', '7', '8', '9', 'T' (for Ten), 'J' (for Jack), 'Q' (for Queen), 'K' (for King), or 'A' (for Ace). These will represent the four cards played in a specific trick, in the order in which they are played. You are to return a String[] representing the scores of each player, as integer strings, with no unnecessary leading zeros. The order should start with player 1, then player 2, and so on (in the order that they played in the first trick). If a player cheated, however, that player will have the String "CHEATER!" rather than a score. A player cheated if he had a card of the same suit led in a trick but played a card of a different suit, or if he led with a heart before hearts had been broken and he had another non-heart he could have led with, or if he played a scoring card in the first trick and had a non-scoring card that he could have played.



Method signature:String[] score(String[] tricks)
(be sure your method is public)


-"Scoring cards" refer to hearts and the queen of spades.
-The highest card of the same suit as the first card takes the trick. The highest card is the Ace, followed by the King, then the Queen, then the Jack, and then 10 and so on, with the lowest card being the 2.
-Note that you are given the cards in the order that they're played, not necessarily in the numerical order of the players.


-tricks will have 13 exactly elements.
-Each element of tricks will have exactly 11 characters and will have 4 cards in the format <suit><card> separated by single spaces.
-The first card in the first element of tricks will be the 2 of clubs.
-Each of the 52 valid cards in the deck will appear exactly once in tricks.


{"C2 CA DA C9",
 "S9 S8 S7 S5",
 "ST S6 S3 S4",
 "C6 HK C3 CK",
 "DQ HQ D9 D5",
 "D3 H7 D4 D2",
 "D6 SK DJ H8",
 "H5 H2 H6 H3",
 "H9 H4 HT CQ",
 "D8 CJ DT SA",
 "HJ C7 SQ CT",
 "HA C5 D7 C8",
 "S2 SJ DK C4"}
Returns: { "6",  "0",  "20",  "0" }
A pretty typical game with no cheating. The players who led each trick were: 1 2 2 2 1 1 3 1 3 1 3 3 3. And player 4 took the last trick.
{"C2 D2 S2 H2",
 "C3 D3 S3 H3",
 "C4 D4 S4 H4",
 "C5 D5 S5 H5",
 "C6 D6 S6 H6",
 "C7 D7 S7 H7",
 "C8 D8 S8 H8",
 "C9 D9 S9 H9",
Returns: { "0",  "26",  "26",  "26" }
In this bizarre game, each player got 13 cards of the same suit. The result is the clubs player shooting the moon.
{"C2 C3 CK SA",
 "DA DK DT D8",
 "SJ ST S3 SK",
 "S5 S6 S2 CQ",
 "C6 DQ CT HK",
 "H3 H6 H2 H4",
 "S4 S9 S8 HQ",
 "C4 HA CJ HT",
 "C7 HJ C5 H9",
 "C8 D9 C9 CA",
 "H5 H7 D7 H8",
 "D2 D6 D4 D5",
 "D3 S7 SQ DJ"}
Returns: { "CHEATER!",  "4",  "17",  "CHEATER!" }
Player 4 turned out to have a club even though he didn't play it in the first trick and player 1 turns out to have a spade that he should have played on the fourth trick.
{ "C2 C9 CA CT",
 "D4 D2 D3 DJ",
 "S9 S2 S7 SA",
 "C4 C7 D5 CJ",
 "D6 C6 DK DT",
 "CK H7 C8 CQ",
 "HA HJ H8 H6",
 "ST HK S3 S4",
 "SK H5 SJ S6",
 "S8 H9 D8 HQ",
 "SQ HT DA C3",
 "C5 D9 H2 H3",
 "S5 D7 DQ H4" }
Returns: { "26",  "0",  "26",  "26" }
The second player shoots the moon!
{"C2 CQ HK D7",
 "H9 H7 HJ H4",
 "S2 S8 S5 D2",
 "C9 DA CT CK",
 "DT C8 D9 D4",
 "DK S3 H6 C6",
 "CJ D6 C5 S9",
 "D8 SA H8 DJ",
 "S7 S6 ST CA",
 "HA H5 H3 D5",
 "DQ C7 SK D3",
 "HQ H2 SJ C3",
 "SQ C4 HT S4" }
Returns: { "CHEATER!",  "CHEATER!",  "CHEATER!",  "CHEATER!" }
Some rampant cheating going on here...
{"C2 CA HA CQ",
 "C3 HK C9 C5",
 "C4 HQ C6 CJ",
 "S9 DA HJ S7",
 "H2 DT HT SK",
 "H3 DQ H4 CK",
 "S4 CT H9 S6",
 "S5 S3 C8 H8",
 "D6 D4 D5 D3",
 "D8 DK D7 H7",
 "S2 C7 H6 DJ",
 "S8 D2 H5 D9"}
Returns: { "7",  "14",  "CHEATER!",  "3" }
Player 3 holds out for awhile, but it turns out he played a heart on the first trick while he had exactly one non-scoring card.
{"C2 SQ C9 C8",
 "C6 CA CQ HA",
 "S4 SJ HK SK",
 "ST SA S9 HQ",
 "DA DK D7 DQ",
 "D2 D4 HJ DJ",
 "H4 DT H2 H3",
 "D9 D5 D3 HT",
 "S5 CK S8 H9",
 "CJ H8 D8 CT",
 "C5 H7 D6 C4",
 "S2 H6 S7 C7",
 "S6 C3 S3 H5"}
Returns: { "3",  "CHEATER!",  "21",  "2" }
You can't play the Queen of Spades in the first trick, either, if you have a non-scoring card in your hand.
{"C2 CJ C6 DK",
 "H7 S2 DQ H6",
 "H2 S6 DJ CK",
 "H3 S8 D2 S4",
 "HK ST H5 CA",
 "H8 SA D3 C7",
 "DA D4 DT C8",
 "HQ S9 D8 C4",
 "HJ S7 D9 CT",
 "HT SK SQ S5",
 "H4 SJ D5 C9",
 "HA S3 D6 C5",
 "H9 CQ D7 C3"}
Returns: { "26",  "CHEATER!",  "26",  "CHEATER!" }
Player 2 cheated to shoot the moon.

Problem url:

Problem stats url:




PabloGilberto , lbackstrom , brett1479

Problem categories:

Simulation, String Parsing