Under construction

Still being built. Things may change

Bridge Hands

Time limit1sMemory limit128 MB

Summary
Deal a 52-card deck to four bridge players from a given dealer, sort each 13-card hand by suit and rank, and print the hands in a fixed card-table format.
Level

Easy3 of 10

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

Problem

Bridge is a card game played with a standard deck of 52 cards dealt to 4 players, so that each player receives 13 cards. Most players like to sort their hand, first by suit and then by rank within each suit.

There is no fixed ranking of the suits, but alternating the colors is convenient, so we use the ordering

♣\clubsuit (C) < ♢\diamondsuit (D) < ♠\spadesuit (S) < ♡\heartsuit (H)

Because most character sets cannot display the suit symbols, we write the suits as the letters C (clubs), D (diamonds), S (spades), and H (hearts).

Within a suit the Ace is high, so the ranks are ordered

2<3<4<5<6<7<8<9<T<J<Q<K<A2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < T < J < Q < K < A

The four players are called North, South, East, and West, seated at the points of the compass. One player is the dealer. She deals one card at a time, starting with the player on her left and proceeding clockwise, until the last card is dealt to herself. The clockwise order of the seats is North → East → South → West.

Write a program that reads a deck of cards, deals it, sorts each of the four hands, and prints them in the format described below.

Input

The input contains several deals. Each deal is given as three lines:

  • The first line is a single letter — N, E, S, or W — naming the dealer.
  • The next two lines list the 52 cards of the deck in dealing order (the first card listed is dealt first). Each line contains 26 cards, and each card is written as two characters: the suit (C, D, S, H) followed by the rank (2–9, T, J, Q, K, A).

The input ends with a line containing a single #.

Output

For each deal print the four sorted hands, using 24 lines per deal. Print the hands in the order South, West, North, East. Separate consecutive deals with a single blank line.

Each hand is printed as six lines:

  1. A header line <Name> player: where <Name> is South, West, North, or East.
  2. A top border made of 13 cells: + followed by ---+ thirteen times, i.e. +---+ ....
  3. A line showing each card's rank twice: for every card a cell |R R (rank, space, rank), ending with a final |.
  4. A line showing each card's suit: for every card a cell | S (space, suit, space), ending with a final |.
  5. The same rank line as line 3.
  6. The same border line as line 2.

The 13 cards of a hand are printed left to right in sorted order (by suit, then by rank).

Examples1

  1. Example 1

    Input
    N
    CQDTC4D8S7HTDAH7D2S3D6C6S6D9S4SAD7H2CKH5D3CTS8C9H3C3
    DQS9SQDJH8HAS2SKD4H4S5C7SJC8DKC5C2CAHQCJSTH6HKH9D5HJ
    #
    
    Expected output
    South player:
    +---+---+---+---+---+---+---+---+---+---+---+---+---+
    |3 3|5 5|7 7|T T|J J|9 9|T T|J J|3 3|K K|2 2|9 9|T T|
    | C | C | C | C | C | D | D | D | S | S | H | H | H |
    |3 3|5 5|7 7|T T|J J|9 9|T T|J J|3 3|K K|2 2|9 9|T T|
    +---+---+---+---+---+---+---+---+---+---+---+---+---+
    West player:
    +---+---+---+---+---+---+---+---+---+---+---+---+---+
    |2 2|4 4|K K|4 4|5 5|6 6|Q Q|A A|4 4|8 8|T T|J J|8 8|
    | C | C | C | D | D | D | D | D | S | S | S | S | H |
    |2 2|4 4|K K|4 4|5 5|6 6|Q Q|A A|4 4|8 8|T T|J J|8 8|
    +---+---+---+---+---+---+---+---+---+---+---+---+---+
    North player:
    +---+---+---+---+---+---+---+---+---+---+---+---+---+
    |6 6|8 8|9 9|A A|8 8|9 9|A A|4 4|5 5|6 6|7 7|J J|A A|
    | C | C | C | C | D | S | S | H | H | H | H | H | H |
    |6 6|8 8|9 9|A A|8 8|9 9|A A|4 4|5 5|6 6|7 7|J J|A A|
    +---+---+---+---+---+---+---+---+---+---+---+---+---+
    East player:
    +---+---+---+---+---+---+---+---+---+---+---+---+---+
    |Q Q|2 2|3 3|7 7|K K|2 2|5 5|6 6|7 7|Q Q|3 3|Q Q|K K|
    | C | D | D | D | D | S | S | S | S | S | H | H | H |
    |Q Q|2 2|3 3|7 7|K K|2 2|5 5|6 6|7 7|Q Q|3 3|Q Q|K K|
    +---+---+---+---+---+---+---+---+---+---+---+---+---+