Got Questions? Try office hours, email or the newsgroup git.cc.class.3411.
"If you want it done right, do it yourself..."
For your second and final programming assignment this quarter, you will implement the beloved Solitaire game so familiar to millions of Windows users. Solitaire is a non-productivity tool :-) Our game will use a simple textual interface that could be easily replaced by a graphical user interface. We will focus on the basic game "engine" and use the C++ inheritance mechanism to help structure our code. Inherited classes will occasionally override member functions, allowing polymorphic invocations.
But first we all need to become familiar with the terminology and rules of Solitaire. The full assignment will be handed out Thursday. This quick prelude will summarize some important terms and the rules of the game and introduce you to the textual representation we will use.
The Game
Solitaire is a one-player card game involving several "piles" of cards and rules for moving cards between the piles. Cards have rank and suit. There are four suits: clubs, diamonds, hearts and spades. There are 13 cards of each suit identified by their rank: ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king. Clubs and spades are black and diamonds and hearts are red. The objective is to end up with four "suit piles" (one for each suit: clubs, diamonds, hearts and spades) with all of the cards of each suit stacked in ascending order (ace, 2, 3, ... king).
The game begins with all 52 cards divided among 13 distinct piles of cards layed out in two rows. Some cards are face down and some cards are face up. The "upper row" begins with 24 cards facedown in the far left. This is the deck pile. Immediately to the right is the discard pile (initially empty). We draw cards from the deck pile one at a time, turn them over and place them in the discard pile. Next we skip a space and the four piles to the far right are the suit piles, all initially empty. We will place cards faceup in ascending order in these piles.
The lower row has 7 table piles. All table piles begin with one faceup card on top. Underneath are facedown cards. The leftmost table pile has 0 cards under it. The next one has 1, the next 2 and so forth.
deck discard
suit piles
pile pile
| | |
|
|
| |
| | |
V
V V
V V V
24--- empty
empty empty empty empty
0---- 1---- 2---- 3----
4---- 5---- 6----
b-4S b-8C r-5D
b-9S b-3S b-JC r-3H
^
^ ^ ^
^ ^ ^
these are all the table piles
The Rules
At each step of the game we move one or more cards between piles. The objective is to complete the suit piles so you first look for any faceup aces and move them to the suit piles (in any order). Once you have an ace you can put a 2 of the same suit on top. When you expose a facedown card in a table pile you can turn it over. You can also move cards from one table pile to another. Here the cards must be in descending order and you must alternate color. So if you have the 5 of spades (5S) and the 4 of hearts (4H) on top of two table piles, you can move 4H to 5S because spades are black and hearts are red and the two cards have consecutive (descending) ranks. (We try to keep the faceup cards exposed in the table piles by moving each new card down just a bit.)
Once you have moved all the cards you can between table piles and suit piles, you can turnover a new card from the deck, place it faceup in the discard pile and try to play it. If not, turnover another card. When you get to the end of the deck, you can "recycle" the discard pile and start using it as the deck again.
If a table pile become empty you can only place a king in that spot. The king can come from another table pile (possibly exposing new cards to turnover) or from the deck. It is occasionally useful to move the top card from a suit pile temporarily onto a table pile.
Finally, it is also possible to move several cards at once between two table piles. You can take all the faceup cards or just a few from the top and place them somewhere else. This is called "moving a build."
A Textual Representation
Here is a simple textual representation for playing Solitaire. (It's more difficult to play without the card images and color cues!)
> newgameThe notation "5----" means there are 5 facedown cards. Cards are represented by their rank and suit (4S, 5D, 10C, JC). I've added "r-" and "b-" prefixes to make it easier to see at a glance which are red and which are black.
24--- empty empty empty empty empty0---- 1---- 2---- 3---- 4---- 5---- 6----
b-4S b-8C r-5D b-9S b-3S b-JC r-3H
> move 3h to 4sTo move, just name a card and describe its destination. Your program should verify that both cards are faceup. The destination must be the top of a stack. If the first card isn't on the top of a stack, then move the build starting with that card. When a facedown card is exposed in a table pile, go ahead and turn it over automatically.
24--- empty empty empty empty empty0---- 1---- 2---- 3---- 4---- 5---- 5----
b-4S b-8C r-5D b-9S b-3S b-JC r-QH
r-3H
> move 4s to 5d
24--- empty empty empty empty empty0---- 1---- 2---- 3---- 4---- 5---- 5----
empty b-8C r-5D b-9S b-3S b-JC r-QH
b-4S
r-3H
To get a new card from the deck, just say newcard:
> newcardThese three simple commands cover almost everything you need to do in Solitaire but there are two situations which require additional notation. If you want to move a king or a "king build" to an empty table pile, just leave off the destination. If there are more than two empty spots, just choose the first. Similarly, if you want to move an ace to an empty suit pile, just leave off the destination.
23--- r-AH empty empty empty empty0---- 1---- 2---- 3---- 4---- 5---- 5----
empty b-8C r-5D b-9S b-3S b-JC r-QH
b-4S
r-3H
> move ah
23--- empty r-ah empty empty empty0---- 1---- 2---- 3---- 4---- 5---- 5----
empty b-8C r-5D b-9S b-3S b-JC r-KH
b-4S
r-3H> move kh
23--- empty r-ah empty empty empty0---- 1---- 2---- 3---- 4---- 5---- 4----
r-KH b-8C r-5D b-9S b-3S b-JC r-3D
b-4S
r-3H
All other cards must have destinations.
What Do I Need To Do?
By Thursday of this week you are required to spend 30 minutes playing Solitaire :-) Make sure you understand the rules of the game, the terminology I've introduced above and the textual notation and command language I have introduced. Thursday I will give you a handout describing how to break the game up into classes.