Solitaire: Idiot's Delight
source: Katrin Becker, 1999
Write a program that will allow you to play solitaire. Since we are not yet ready to deal with animation or fancy graphics the "display" will be entirely text based and each play will require that the "screen" be re-drawn.
Cards may be represented as follows:
C = Clubs; S = Spades; D = Diamonds; H = Hearts
A = Ace; 2-9 as themselves; T= 10; J = Jack; Q = Queen; K = King
This way every card can be represented in 2 characters. The most straight-forward way to represent a deck internally is using the integers 0-51. Each number can easily be mapped onto a suit { C, S, D, H } and a rank { A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K }. Each Pile of cards is to be implemented as a proper stack.
A minimal acceptable solution will NOT check whether requested moves are legal.
The variation of Solitaire you are to implement is one of the simplest.
Layout: Deal 4 cards to form the tableau. The rest remain in the hand. There is also a Wastepile for discarded cards.
Goal: Aces are high (i.e. have the greatest value), and the goal is to end up with all the aces on the tops of the tableaus and no cards left in the hand.
Play: Cards are always dealt from the Hand in sets of 4 cards (one for each pile in the Tableau). After each 'deal' examine the top cards of each stack. If there are 2 cards of the same suit available, the lower of the two may be discarded. This can be repeated until no more cards can be discarded. If one of the stacks becomes empty, it can be filled with the top card of any of the other stacks. Since the user may choose which card to move in this case and some strategy may be involved, all cards must be visible on the stacks (even though only the top card of any given stack is accessible).
Implementation Hints:
1. Each 'location' (stack) must be identifiable.
2. Need to be able to detect an empty stack.
3. Only one card at a time may be moved.
4. The Wastepile need not be represented as cards since cards can never be removed from this pile.
5. The Hand must be represented internally but need not be displayed - we can't look ahead in the hand pile anyway.
6. The Hand can only be used for dealing (always 4 cards at a time).
7. The Wastepile can only be used for discarding (one card at a time)
8. Tableaus can serve as both sources and destinations.
Display:
The display consists of the 4 stacks in the tableau. All cards should be visible but only the top card is available for play.
User Options:
Play (i.e. start again); command: p
Move a card from source to destination; command: m <s> <d>
Cast Off (i.e. discard) from source (always to wastepile); command: c <s>
Deal (deal the next 4 cards; command: d
Help (display the rules); command: h
Quit (end game); command: q
Testing: run at least one game to completion (including invalid commands and bad arguments to commands)
There is a working version of this Solitaire Game in the course directory (/home/233). You are free to play with it.
BONUSES:
1. [2 points] check for and deny requests for invalid moves (invalid source/destination)
2. [3 points] check for and deny requests for illegal moves (attempt to move to stack that's not empty; cast off a card for which there is no outranking card on another stack, etc)
3. [5 points] use classes for stacks, cards, decks
4. [4 points] have the game detect a winning state and print out an appropriate message