Model the deck of cards used in Memory as a list consisting of 16 numbers with each number lying in the range [0,8) and appearing twice. I suggest that you create this list using a list comprehension of the form [... for i in range(16)] where the expression ... should be replaced by a Python expression depending on i that yields numbers in the range [0,8).
Write a draw handler that iterates through the Memory deck and uses draw_text to draw the number associated with each card on the canvas. The result should be a horizontal sequence of evenly-spaced numbers drawn on the canvas.
Shuffle the deck using random.shuffle(). Remember to debug your canvas drawing code before shuffling to make debugging easier.
Next, modify the draw handler to either draw a blank rectangle or the card's value. To implement this behavior, we suggest that you create a second list called exposed whose ith entry is True if the card is flipped over and its value is visible and False if the card's value is hidden. We suggest that you initialize exposed to some known values while testing your drawing code with this modification.
Now, add functionality to determine which card you have clicked on with your mouse. Add an event handler for mouseclick that takes the position of the mouseclick and prints the index of the card that you have clicked on to the console. To make determining which card you have clicked on easy, we suggest sizing the canvas so that the sequence of cards entirely fills the canvas.
Modify the event handler for mouseclick to flip cards based on mouseclicks. If the player clicked on the ith card, you can change the value of exposed[i] from False to True. If the card is already exposed, you should ignore the mouseclick. At this point, the basic infrastructure for Memory is done.
You now need to add game logic for selecting two cards and determining if they match. We suggest following the structure in the program template, which explicitly keeps track of the state of the game. State 0 corresponds to the start of the game. In state 0, if you click on a card, that card is exposed, and you switch to state 1. State 1 corresponds to a single exposed unpaired card. In state 1, if you click on an unexposed card, that card is exposed and you switch to state 2. State 2 corresponds to the end of a turn. In state 2, if you click on an unexposed card, that card is exposed and you switch to state 1.
Note that in state 2, you also have to determine if the previous two cards are paired or unpaired. If they are unpaired, you have to flip them back over so that they are hidden before moving to state 1. We suggest that you use two global variables to store the index of each of the two cards that were clicked in the previous turn.
Add a counter that keeps track of the number of turns and uses
set_text to update this counter as a label in the control panel. (
BTW, Joe's record is 12 turns.)
Finally, add a “Reset” button that reshuffles the cards, resets the turn counter and restarts the game. All cards should start the game hidden.
(Optional) You may replace the draw_text for each card by a draw_image that uses one of eight different images.