CSE 231 Fall 2012 Programming Project 8 Assignment Overview This assignment will give you more experience on the use of classes. It is worth 50 points (5.0% of the course grade) and must be completed and turned in before 11:59 on November 12 (that is 2 weeks because of the exam). Background You will create a program that allows a user to play the card game 2-Suit Spider Solitaire. This is a popular version of the solitaire class of card games, and your job is to enforce the rules of the game. A playable example of this game and the rule can be found at http://www.worldofsolitaire.com (click Solitaire at the left-upper corner, click Select game, and choose Spider (2 Suits) ). If you are not familiar with the game, play it first to get a better understanding of the rules. (It is really hard to win this game!) Game setup: At the beginning of the game, two suits are selected (for example, hearts and spades). Four decks of cards are combined, but only using the cards from those two suits. This means that a total of 104 cards are used in this game (of the 52 heart cards, there will be 4 instances of each rank, and the same for the spades cards). The main playing area is called the tableau. This consists of 10 piles of cards. At the beginning of the game, 54 of the cards are dealt to these piles (4 piles get 6 cards, while the rest get 5 cards each). All cards except for the top card are hidden (look at the included cards.py and cardsdemo.py files for examples). There is a single pile for the foundation. This is where completed runs of cards will be placed (we ll talk more about this). The remaining cards are called the stock. On a deal command, each tableau row is dealt a single card from the stock. Goal: The goal is to sort all the cards into runs of cards. A completed run has cards from the same suit and are in descending order from King down to Ace. Once a run has been completed, it should automatically be placed in the foundation. Once the foundation contains all the cards, the game has been won. Rules: 1. A card moved in the tableau must be placed on a card that is ranked one higher than itself. For example, a 3 can be placed on a 4, but not the other way around. They do not have to be the same suit. 2. A sequence of cards with the same suit and descending rank (i.e. a run ) can be moved in the tableau according to the following restriction: the connecting card must follow the same rank rule, i.e. the highest card in the run must be ranked one lower than the card it is placed on, but need not be the same suit. For example, a sequence with a 4, 3, and 2 of hearts can be place on a 5 card of any suit 3. Cards can only be dealt from the stock if none of the tableau rows are empty. Requirements
You must use both of the provided Card and Deck classes, found in the cards.py file in the project directory. Do not modify the cards.py program as you will only turn in proj09.py. Just import cards.py into your proj08.py. You will not need to use every method in those classes, but look at the example code in cardsdemo.py to get an idea of what you might need. Also, in the project directory is proj08skel.py. This Python file has the skeleton code for one way to solve this project. The pass statement is simply a filler because a function requires something in its suite the pass statement does nothing except fill a void. It is highly suggested to use this format, as it will make your life easier. Regardless of whether you use that format, you must use at least two functions in your program. Perform error checking on all user input. This includes checking for typos as well as incorrect values (such as an invalid row number). If a command cannot be followed, print a specific error message to let the user know what went wrong. You must include the following commands: d - deals more cards (or prints error message if cannot be completed) q - quits the game h - prints help information m (# of cards) (source row #) (dest row #) - moves the number of cards from the source row to the destination row, if the move is allowed When a run of cards from King to Ace has been completed, your program must automatically put that run of cards into the foundation. Do not allow dealing if there are empty tableau rows. Print the game after every move. Look at the example interaction below and follow that general format. Do not simply print a list: make it readable and clean. You may use Hearts and Spades as the two suits for the game. Deliverables You must use handin to turn in the file proj08.py this is your source code solution; include your section, the date, the project number and comments describing your code. Be sure to use the specified file name, and save a copy of your proj08.py file to the CSE server (your H drive) as a backup. Notes and Hints 1. Play with the online game and get a feel for it. 2. Look carefully at the example cardsdemo.py program. It imports the cards module and uses the two classes. Understanding what it does will give you a better idea how you can use these classes. These classes provide more methods than you will probably use, but they should provide almost any method you need. 3. When using class methods remember the parenthesis no error is generated for missing parenthesis, but the results will not be what you expect. 4. There are multiple parts to the game (setup, printing, game play, starting). Address each one individually and then put them together. Try starting with the setup, and then work on printing the game.
5. For playing the game, begin by assuming perfect input. Get that working and add error checking later. 6. For the setup you will need to create the special deck required for this game. Start with an empty deck (create a regular deck and then discard all 52 cards) and the fill the deck with Hearts and Spades from four decks. 7. The move_in_tableau is the hardest function. A good way to simplify this function for starting is to have it move only one card at a time. Once you can do that, work on moving runs. If you can get the whole program working with this function only moving one card, you will get most of the points in the project. 8. The hardest part of the play function is checking for errors so begin by assuming no errors. Sample Interactions Rules of Spider Solitaire: The goal is to move all cards to the foundation. Cards can only be moved to the foundation if in a completed run of cards (King, Queen,..., Ace). A single card in the tableau can be moved to another row if the destination card is one rank higher than the moving card. Multiple cards can be moved at once, but all cards within the stack being moved must be in descending order, and they must all be the same suit. The destination card must also be one rank higher than the top card of the stack being moved. Acceptable commands: XX XX XX XX XX 10S XX XX XX XX XX 5S XX XX XX XX JS XX XX XX XX 4S What is your move? --> m 1 10 4 XX XX XX XX XX 10S XX XX XX XX XX 5S 4S XX XX XX XX JS
XX XX XX 6H What is your move? --> m 2 4 10 XX XX XX XX XX 10S XX XX XX XX QH XX XX XX XX JS XX XX XX 6H 5S 4S What is your move? --> m 1 1 6 XX XX XX XX 3S XX XX XX XX QH XX XX XX XX JS 10S XX XX XX 6H 5S 4S What is your move? --> m 1 1 2 The source card must be one rank lower than the destination card. XX XX XX XX 3S XX XX XX XX QH XX XX XX XX JS 10S XX XX XX 6H 5S 4S (skipping ahead) XX XX XX 5S
XX XX XX XX 9S 8H XX XX XX XX AS XX XX XX XX QH JS 10S AH XX XX 6S 5S 4S 3S XX XX XX AH AS XX XX XX 6H 5H What is your move? --> m 2 2 4 The cards which you are trying to move are not correctly ordered (skipping ahead) XX XX XX 5S XX XX XX XX 9S 8H XX XX XX XX AS XX XX XX XX QH JS 10S XX XX XX JS XX XX 6S 5S 4S 3S 2H AH AH XX XX KH AS XX XX XX 6H 5H What is your move? --> d AH 6S AS 5H What is your move? --> md 1 9 10 Incorrect command. Acceptable commands:
AH 6S AS 5H What is your move? --> m 1? 10 Incorrect command. Move command must be followed by three numbers. Acceptable commands: AH 6S AS 5H What is your move? --> h Rules of Spider Solitaire: The goal is to move all cards to the foundation. Cards can only be moved to the foundation if in a completed run of cards (King, Queen,..., Ace). A single card in the tableau can be moved to another row if the destination card is one rank higher than the moving card. Multiple cards can be moved at once, but all cards within the stack being moved must be in descending order, and they must all be the same suit. The destination card must also be one rank higher than the top card of the stack being moved. Acceptable commands: AH 6S AS 5H
What is your move? --> q Thank you for playing!