This page is still under construction.

Parts of this page are still being built. What you see may change.

Hearts

Time limit1sMemory limit128 MB

Summary
Simulate a simplified Hearts game with fixed strategies and report the heart points won by each of the five players, starting with the dealer.
Level

Medium6 of 10

Topics
Simulation, Implementation, Sorting, Array
Solved
No attempts yet

Problem

A pack of playing cards has 52 cards, divided into suits and, within each suit, into denominations. The suits are, from lowest to highest, Clubs, Diamonds, Hearts and Spades, abbreviated C, D, H and S. The 13 denominations (face values) are, from lowest to highest: 2, 3, 4, 5, 6, 7, 8, 9, 10 (written T), Jack (J), Queen (Q), King (K) and Ace (A).

A higher card beats a lower card of the same suit, but normally does not beat any card of a different suit. The exception is the trump suit: if a suit is designated as trump, then any card of that suit beats any card of any other suit.

A simplified version of the old card game Hearts is played as follows.

The dealer deals the cards clockwise, one at a time and face down, to four other players and to himself. Dealing starts with the player on the dealer's left, who therefore receives the 1st card, then the 6th, and so on; the dealer receives the 5th card, then the 10th, and so on. Once every player holds 10 cards, two cards remain. These two are turned face up, and the suit of the card with the higher denomination becomes the trump suit. If the two cards have equal denomination, the higher-ranking suit becomes trump.

A game consists of 10 tricks, each made up of 5 cards, one from every player. In each trick one player leads, i.e. plays a card face up on the table; the other players then follow in clockwise order. The player on the dealer's left leads the first trick; thereafter the winner of each trick leads the next one.

A player must follow suit if possible, i.e. play a card of the same suit as the card led. If he cannot follow suit, he must trump (play a card of the trump suit). If he cannot trump either (he holds no trump), he discards a card. If any trump was played in a trick, the highest trump wins it; otherwise the highest card of the led suit wins it. The winner takes all five cards of the trick.

The players use the following fixed strategies:

  • Leader: always plays the highest card in hand. If several cards tie for highest and one of them is a trump, the trump is led; otherwise the card of the highest-ranking suit is led.
  • Follower: must play the highest card of the led suit if he holds any. If he holds no card of the led suit, he plays his highest trump. If he cannot trump, he plays the highest card in his hand, breaking ties by the same rule as the leader.

After all tricks have been played, each player looks at the tricks he has won and scores the face value of every Heart in them (Jack counts 11, Queen 12, King 13 and Ace 14). Simulate the game and report each player's score.

Input

The input consists of a series of decks. Each deck is given on four lines that hold its 52 cards in dealing order (13 cards per line, separated by single spaces). The input is terminated by a line containing a single #.

Output

For each deck, output one line with 5 numbers: the players' scores, starting with the dealer and continuing clockwise through the other four players. Each score is right-justified in a field of width 3.

Examples2

  1. Example 1

    Input
    TS QC 8S 8D QH 2D 3H KH 9H 2H TH KS KC
    9D JH 7H JD 2S QS TD 2C 4H 5H AD 4D 5D
    6D 4S 9S 5S 7S JS 8H 3D 8C 3S 4C 6S 9C
    AS 7C AH 6H KD JC 7D AC 5C TC QD 6C 3C
    #
    
    Expected output
     22  0 68  0 14
    
  2. Example 2

    Input
    TS QC 8S 8D QH 2D 3H KH 9H 2H TH KS KC
    9D JH 7H JD 2S QS TD 2C 4H 5H AD 4D 5D
    6D 4S 9S 5S 7S JS 8H 3D 8C 3S 4C 6S 9C
    AS 7C AH 6H KD JC 7D AC 5C TC QD 6C 3C
    QS JC KH JD 4C AH 8D KC JH 7C 5H AS 6S
    4D QD 9S TS TD 3S 2S AC KS 8H 9D AD 2H
    TH QC 9H 5C 7D 3D 6D 7S 2C 3H 5S 3C 8S
    8C 2D KD 4S 6H 4H 7H 9C 5D 6C JS QH TC
    #
    
    Expected output
     22  0 68  0 14
      0 57  0 19 16