CSC 221: Introduction to Programming
Fall 2023
HW7: Classes and Skip-3 Solitaire
Note: late submissions will not be accepted for this assignment.
For this assignment, you are to complete a Python project for playing
Skip-3 Solitaire.
In Skip-3 Solitaire, cards are dealt one at a time
from a
standard deck and placed in a row. If the rank or suit of a card matches the rank or
suit either one or three cards
to its left, then that card (and any cards in the pile beneath it) can be moved on top of
the matching card. For
example, suppose the three of clubs, five of hearts, nine of spades, and jack of hearts
are initially dealt. They
would be placed in a row, in left-to-right order, as shown below.
3C 5H 9S JH
If the next card dealt is the five of spades, then that card is placed to the right and
is found to match the
five of hearts three spots to its left. Thus, the player can skip the five of spades on top of
the five of hearts,
shortening the row of cards. The consolidation of these piles then allows the nine of
spades to match with the five
of spades that is immediately to its left. Thus, the player can move the pile topped by the nine of
spades on top of the five
of spades, shortening the row even further. Pictorially, the consolidation steps are:
3C 5H 9S JH 5S
3C 5S 9S JH
3C 9S JH
Note that the addition of a single card can start a chain reaction in which many piles of cards,
both to the left and right, are
moved/skipped. The goal of
the game is to end with as few piles of cards as possible. Theoretically, the game could
end with a single pile of 52
cards, although that outcome is extremely unlikely.
Also, note that only the top card in a pile needs to be stored, since the cards below it
are never seen again.
Download the cards.py module that contains the Card and DeckOfCards classes described in lectures. Similarly, download the skip3.py module that contains a starter version of the game. As is, the starter version creates and shuffles a deck of cards and responds to some of the game commands: the user can deal cards by entering a command starting with 'D' or 'd', and can quit the game by entering a command starting with 'Q' or 'q'.
Once you have tested the starter version, implement the full version by completing the following tasks.
- Modify the game code so that the command prompt includes the number of cards remaining in the deck. For example, instead of just asking "Action?", it could display "(52 cards remaining) Action?". This will allow the user to see how close they are to the end of the game.
- Currently, the game displays "Thanks for playing." when the player enters the quit command.
Modify the game code so that the final number of cards in the row is also printed at the end of the game.
- Implement the Help operation, which displays a list of commands when the user enters '?' or 'h'. For example:
The possible commands are:
D - deals a card and adds it to the row
M RS - moves card with rank R and suit S one spot
S RS - skips card with rank R and suit S three spots
Q - quit the game
? - help menu
- Implement the Move operation that moves a specified card one place to the left in the row. For example, if the row contained the cards "3C 9S JH JS", then the command "M JS" would specify moving the jack of spades one place to the left, resulting in "3C 9S JS". Note that your code should not crash, regardless of the user's input. If they specify a card that doesn't exist in the board, a warning message to that effect should be displayed and the row left unchanged. Similarly, a warning message should be displayed and no changes made if the card to the left does not match (i.e., have the same rank or suit) or if the specified card is first in the row.
- Implement the Skip operation that skips a specified card three places to the left in the row. For example, if the row contained the cards "3C 9S JS 8C", then the command "S 8C" would specify skipping the eight of clubs three places to the left, resulting in "8C 9S JS". As before, any illegal skip should result in a warning message and no changes to the row of cards.
- According to the rules of Skip3 Solitaire, the player cannot deal a new card if there is a possible move or skip available. This precludes them from gaining an unfair advantage by laying out all of the cards and then moving/skipping with complete knowledge. Modify the game code so that it disallows dealing a new card (and warns the user) if there is a valid move or skip that could be made. It should also display a warning if the user attempts to deal a card when the deck is empty.
- Modify the Quit operation so that it warns the player if the deck is not empty or if there are moves/skips that could be made. In either case, the user would be prompted to confirm that they really want to quit. If they change their mind, then the game should continue. For example:
(33 cards left) Action? q
The game is not over. Are you sure? (Y/N) N
Good for you - play on!
QD 5C TD JH QS AH 4S
.
.
.
(15 cards left) Action? q
The game is not over. Are you sure? (Y/N) Y
Quitters never win.
QD 5C 2D 8H
GAME OVER: row length = 4
Save your functions in a file named lastnameSkip3.py, where lastname is your last name. Your file should have a comment block at the top that includes your name and a brief description of the file, and each function should have a doc string that describes its behavior. Feel free to implement additional helper functions as needed.