An Introduction to Interactive Programming in Python
Mini-project description - Memory
Memory is a card game in which the player deals out a set of cards face down. In Memory, aturn(or a move) consists of the player flipping over two cards. If they match, the player leaves them face up. If they don't match, the player flips the cards back face down. The goal of Memory is to end up with all of the cards flipped face up in the minimum number of turns. For this project, we will keep our model for Memory fairly simple. A Memory deck consists of eight pairs of matching cards.
Mini-project development process
As usual, we suggest that you start from the program template for this mini-project.- 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. We suggest that you create this list by concatenating two list with range [0,8) together. Use the Docs to locate the list concatenation operator.
- Write a draw handler that iterates through the Memory deck using a
for
loop and usesdraw_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 green rectangle or the card's value. To implement this behavior, we suggest that you create a second list called
exposed
. In theexposed
list, the<nobr style="border:0px; padding:0px; margin:0px; max-width:none; max-height:none; vertical-align:0px; line-height:normal; text-decoration:none; white-space:nowrap!important"><span class="math" id="MathJax-Span-1" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span style="display:inline-block; position:relative; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; width:20px; height:0px; font-size:18px"><span style="display:inline; position:absolute; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; top:-41px; left:0px"><span class="mrow" id="MathJax-Span-2" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span class="msubsup" id="MathJax-Span-3" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span style="display:inline-block; position:relative; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; width:19.4px; height:0px"><span style="display:inline; position:absolute; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; top:-41px; left:0px"><span class="mi" id="MathJax-Span-4" style="font-family:MathJax_Math; display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; font-style:italic">i</span></span><span style="display:inline; position:absolute; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; top:-48.2px; left:6px"><span class="texatom" id="MathJax-Span-5" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span class="mrow" id="MathJax-Span-6" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span class="mi" id="MathJax-Span-7" style="font-family:MathJax_Math; display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; font-size:13px; font-style:italic">t</span><span class="mi" id="MathJax-Span-8" style="font-family:MathJax_Math; display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; font-size:13px; font-style:italic">h</span></span></span></span></span></span></span></span></span></span></nobr>entry should beTrue
if the<nobr style="border:0px; padding:0px; margin:0px; max-width:none; max-height:none; vertical-align:0px; line-height:normal; text-decoration:none; white-space:nowrap!important"><span class="math" id="MathJax-Span-9" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span style="display:inline-block; position:relative; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; width:20px; height:0px; font-size:18px"><span style="display:inline; position:absolute; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; top:-41px; left:0px"><span class="mrow" id="MathJax-Span-10" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span class="msubsup" id="MathJax-Span-11" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span style="display:inline-block; position:relative; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; width:19.4px; height:0px"><span style="display:inline; position:absolute; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; top:-41px; left:0px"><span class="mi" id="MathJax-Span-12" style="font-family:MathJax_Math; display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; font-style:italic">i</span></span><span style="display:inline; position:absolute; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; top:-48.2px; left:6px"><span class="texatom" id="MathJax-Span-13" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span class="mrow" id="MathJax-Span-14" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span class="mi" id="MathJax-Span-15" style="font-family:MathJax_Math; display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; font-size:13px; font-style:italic">t</span><span class="mi" id="MathJax-Span-16" style="font-family:MathJax_Math; display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; font-size:13px; font-style:italic">h</span></span></span></span></span></span></span></span></span></span></nobr>card is face up and its value is visible orFalse
if the<nobr style="border:0px; padding:0px; margin:0px; max-width:none; max-height:none; vertical-align:0px; line-height:normal; text-decoration:none; white-space:nowrap!important"><span class="math" id="MathJax-Span-17" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span style="display:inline-block; position:relative; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; width:20px; height:0px; font-size:18px"><span style="display:inline; position:absolute; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; top:-41px; left:0px"><span class="mrow" id="MathJax-Span-18" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span class="msubsup" id="MathJax-Span-19" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span style="display:inline-block; position:relative; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; width:19.4px; height:0px"><span style="display:inline; position:absolute; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; top:-41px; left:0px"><span class="mi" id="MathJax-Span-20" style="font-family:MathJax_Math; display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; font-style:italic">i</span></span><span style="display:inline; position:absolute; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; top:-48.2px; left:6px"><span class="texatom" id="MathJax-Span-21" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span class="mrow" id="MathJax-Span-22" style="display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none"><span class="mi" id="MathJax-Span-23" style="font-family:MathJax_Math; display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; font-size:13px; font-style:italic">t</span><span class="mi" id="MathJax-Span-24" style="font-family:MathJax_Math; display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px; line-height:normal; text-decoration:none; font-size:13px; font-style:italic">h</span></span></span></span></span></span></span></span></span></span></nobr>card is face down and it's value is hidden. We suggest that you initializeexposed
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 mouse clicks that takes the position of the mouse click 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 mouse clicks to flip cards based on the location of the mouse click. If the player clicked on the
i
th card, you can change the value ofexposed[i]
fromFalse
toTrue
.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 to the mouse click handler for selecting two cards and determining if they match. We suggest following the game logic in theexample codediscussed in the Memory video. 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.) This counter should be incremented after either the first or second card is flipped during a turn. - Finally, implement the
init()
function (if you have not already) so that the "Reset" button 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 adraw_image
that uses one of eight different images.
While this project may seem daunting at first glance, our full implementation took well under 100 lines with comments and spaces. If you feel a little bit intimidated, focus on developing your project to step six. Our experience is that, at this point, you will begin to see your game come together and the going will get much easier.
代码如下:
# implementation of card game - Memory
import simplegui
import random
num=[]
state=0
preindex=0
aftindex=0
turn=0
# helper function to initialize globals
def init():
global num,exposed,turn
num=range(0,8)
num.extend(range(0,8))
random.shuffle(num)
exposed=[False,False,False,False,False,False,False,False,
False,False,False,False,False,False,False,False]
turn=0
#print num
# define event handlers
def mouseclick(pos):
# add game state logic here
global exposed,state,preindex,aftindex,turn
index=pos[0]//50
if not exposed[index]:
exposed[index]=True
#state=(state+1)%3
if state==0:
state=1
preindex=index
elif state==1:
aftindex=index
state=2
elif state==2:
if num[preindex]!=num[aftindex]:
exposed[aftindex]=False
exposed[preindex]=False
preindex=index
state=1
turn+=1
# cards are logically 50x100 pixels in size
def draw(canvas):
global num
horzoffset=0
index=0
label.set_text("Moves = "+str(turn))
for tmp in num:
if exposed[index]:
canvas.draw_text(str(tmp), (horzoffset, 100), 100, "White")
else:
canvas.draw_polygon([(horzoffset, 0), (horzoffset, 100),
(horzoffset+50, 100),(horzoffset+50, 0)],
1, "Green", "Green")
horzoffset+=50
index+=1
# create frame and add a button and labels
frame = simplegui.create_frame("Memory", 800, 100)
frame.add_button("Restart", init)
label = frame.add_label("Moves = 0")
# initialize global variables
init()
# register event handlers
frame.set_mouseclick_handler(mouseclick)
frame.set_draw_handler(draw)
# get things rolling
frame.start()
# Always remember to review the grading rubric