Memory is a card game in which the player deals out a set of cards face down. In Memory, a turn (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.
As usual, we suggest that you start from the program template for this mini-project.
for
loop 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.random.shuffle()
. Remember to debug your canvas drawing code before shuffling to make debugging easier.exposed
. In the exposed
list, the \(i^{th}\) entry should be True
if the \(i^{th}\) card is face up and its value is visible or False
if the \(i^{th}\) card is face down and it’s value is hidden. We suggest that you initialize exposed
to some known values while testing your drawing code with this modification.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.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.new_game()
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.draw_text
for each card by a draw_image
that uses one of eight different images.Once the run button is clicked in CodeSkulptor, the game should start. You should not have to hit the “Reset” button to start. Once the game is over, you should hit the “Reset” button to restart the game.
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
# helper function to initialize globals
def new_game():
global deck, exposed, nTurns, state, cInd1, cInd2
deck = [x for x in range(8)] * 2
random.shuffle(deck)
exposed = [False] * 16
nTurns = 0
state = 0
cInd1, cInd2 = -1, -1
# define event handlers
def mouseclick(pos):
# add game state logic here
global nTurns, state, cInd1, cInd2
cardInd = list(pos)[0] // 50
if not exposed[cardInd]:
if state == 0:
cInd1 = cardInd
exposed[cInd1] = True
state = 1
elif state == 1:
cInd2 = cardInd
exposed[cInd2] = True
state = 2
nTurns += 1
label.set_text("Turns = " + str(nTurns))
else:
if deck[cInd1] != deck[cInd2]:
exposed[cInd1], exposed[cInd2] = False, False
cInd1, cInd2 = -1, -1
cInd1 = cardInd
exposed[cInd1] = True
state = 1
# cards are logically 50x100 pixels in size
def draw(canvas):
for i in range(16):
if exposed[i]:
canvas.draw_polygon([[i*50, 0], [(i+1)*50, 0], [(i+1)*50, 100], [i*50, 100]], 1, "Black", "White")
canvas.draw_text(str(deck[i]), (i*50+15, 65), 50, "Black")
else:
canvas.draw_polygon([[i*50, 0], [(i+1)*50, 0], [(i+1)*50, 100], [i*50, 100]], 1, "Black", "Teal")
label.set_text("Turns = " + str(nTurns))
# create frame and add a button and labels
frame = simplegui.create_frame("Memory", 800, 100)
frame.add_button("Reset", new_game)
label = frame.add_label("Turns = 0")
# register event handlers
frame.set_mouseclick_handler(mouseclick)
frame.set_draw_handler(draw)
# get things rolling
new_game()
frame.start()
# Always remember to review the grading rubric