Bridge Hands
Time limit1sMemory limit128 MB
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
(C) < (D) < (S) < (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
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, orW— 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:
- A header line
<Name> player:where<Name>isSouth,West,North, orEast. - A top border made of 13 cells:
+followed by---+thirteen times, i.e.+---+ .... - A line showing each card's rank twice: for every card a cell
|R R(rank, space, rank), ending with a final|. - A line showing each card's suit: for every card a cell
| S(space, suit, space), ending with a final|. - The same rank line as line 3.
- 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).