Run update21, if you haven't already, to create the cs21/labs/10 directory. Then cd into your cs21/labs/10 directory and create the python programs for lab 10 in this directory.
This week you will implement a card game called Triad. The game Triad is quite similar to a card game called Set. You will get practice writing your own classes and becoming more familiar with object-oriented programming.
We will provide you with two supporting classes that will create the graphical display, draw the cards on the screen, highlight the cards when you select them with the mouse, remove cards from the screen when they form an appropriate triad, deal more cards onto the screen when necessary, and end the game when the game is over. Your assignment is to write three other classes that will work with the classes we provide you. When your three classes are implemented, you will have completed the lab and you will have a playable version of the game Triad.
First, we will explain the rules of the game. We will then describe the Card class and the Deck class that you will need to write. Finally, we will describe the TriadGame class that you will write which implements the logic of the game. The primary responsibility for testing the code will be yours.
We have written a very small tester for you which will work once you have written the Card and Deck classes. When you run update21, if you don't get Tester.py in your labs/10 directory, then type:
cp /home/knerr/Tester.py ~/cs21/labs/10/
Triad is a solitaire card game played with a deck of 81 cards. The face of each card contains a simple symbol which may be repeated. Each card can be described in four ways: by the number of symbols which appear (1, 2, or 3), by the shape of those symbols ("triangle", "oval", or "diamond"), by the color of those symbols ("orange", "purple", or "green"), and by the shading of those symbols ("none", "light", or "dark"). For example, consider the three cards presented below:
|Three green triangles with no shading||One purple oval with dark shading||Two orange diamonds with light shading|
|Two green diamonds with light shading||Two orange diamonds with no shading||Two purple diamonds with dark shading|
|Three green diamonds with dark shading||Two green ovals with dark shading||One green diamond with dark shading|
After the initial twelve cards are dealt, play proceeds with the player picking triads of three cards. Each time the player finds a triad of three cards, those three cards are removed from the playing field. Then, three new cards are dealt from the deck onto the tableau to replace them. Play continues until the deck is empty; at this point, the game is over when there are no more triads to find.
It may occasionally happen that, although there are cards left in the deck, none of the cards on the tableau form a triad. In this case (and only in this case!), all of the cards on the tableau are discarded -- you do not put them back in the deck. Then, twelve new cards are dealt from deck and those new cards are placed on the tableau.
The player wins and the game ends when there are no cards left in the deck. There is no loss condition; the only way to lose this game is by quitting.
Your Card object will store the shape, color, count and fill of the Card. Each of the data values store the following:
Your Card object will have six methods:
if __name__ == "__main__": main()Here is a sample run of the Card class to demonstrate how it should work:
$ python Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from Card import Card >>> c1 = Card("diamond", "green", 3, "dark") >>> print(c1) diamond,green,3,dark >>> c1.get_shape() 'diamond' >>> c1.get_color() 'green' >>> c1.get_count() 3 >>> c1.get_fill() 'dark' >>>When you have finished with the Card class and after you have thoroughly tested it, you can move on to the Deck class. For this lab, you can assume that the Card constructor is only ever called with legal values for the shape, color, fill and count.
You Deck object will have three methods:
if __name__ == "__main__": main()Here is a sample run of the Deck class to demonstrate how it should work:
$ python Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from Deck import * >>> deck = Deck() >>> print(deck.cards_left()) 81 >>> card = deck.deal() >>> print(card) triangle,green,1,none >>> card = deck.deal() >>> print(card) triangle,purple,1,dark >>> print(deck.cards_left()) 79 >>> for i in range(5): ... print(deck.deal()) ... diamond,orange,1,none diamond,purple,2,light triangle,purple,1,light oval,orange,1,light diamond,green,3,dark >>> print(deck.cards_left()) 74 >>>Note that because the Deck is randomly shuffled, you will almost certainly get different Cards printed out than in this example.
It is absolutely critical that you do not proceed to the next section unless you are confident that your Deck and Card classes work without any errors. If anything is wrong with your Deck and/or Card classes, debugging the next class will be very difficult.
The final class you will write is the TriadGame class, saved in the file TriadGame.py. The TriadGame class will manage the current state of the game and implement methods that allow the game to be played.
The TriadGame class will have two pieces of data. First, the class will maintain a Deck of cards (constructed using your Deck class). At the start of the game this Deck will contain 81 cards, but as the game progresses the Deck will get smaller. The TriadGame class will also store a list of the cards that are currently on the tableau. The graphical portion of the game will lay out the cards by their indices like so:
0 3 6 9 1 4 7 10 2 5 8 11
The organization of the cards on the tableau shouldn't affect how you write your TriadGame class, but it's helpful to know the indices of the displayed cards when you are debugging.
The TriadGame class will implement four methods:
You are not required to write the above methods, but you should.
Like the Card and Deck classes, you should write a main function that tests your TriadGame class. Be sure to put the call to main at the end of your program using the special __name__ == "__main__" syntax shown above.
Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.