Skip to content

Instantly share code, notes, and snippets.

@gnagel
Last active August 29, 2015 14:25
Show Gist options
  • Save gnagel/f6aa6f9126f9982c2505 to your computer and use it in GitHub Desktop.
Save gnagel/f6aa6f9126f9982c2505 to your computer and use it in GitHub Desktop.
CodeSkulptor is unable to save at this time :-(
#
# CodeSkuptor URL:
# http://www.codeskulptor.org/#user40_0zoDGCu9x5_0.py
#
# memory.py
# 2015-07-25
# github.com/gnagel
#
#
# Mini-project description - Memory (late version - half credit)
#
# 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.
#
# Mini-project development process
#
# As usual, we suggest that you start from the program template for this mini-project.
# 1. 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.
#
# 2. Write a draw handler that iterates through the Memory deck using a 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.
#
# 3. Shuffle the deck using random.shuffle().
# Remember to debug your canvas drawing code before shuffling to make debugging easier.
#
# 4. 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 the exposed list, the ith entry should be True if the ith card is face up and its value is visible or False if the ith 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.
#
# 5. 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.
#
# 6. Modify the event handler for mouse clicks to flip cards based on the location of the mouse click.
# 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.
#
# 7. 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 the example code discussed 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.
#
# 8. 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.
#
# 9. 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.
#
# 10. Finally, implement the 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.
#
# 11. (Optional) You may replace the draw_text for each card by a draw_image that uses one of eight different images.
#
import simplegui
import random
# 1. 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.
num = []
num.extend(range(0,8))
num.extend(range(0,8))
#
exposed = []
for i in range(len(num)):
exposed.append(False)
#
turn=0
#
# 3. Shuffle the deck using random.shuffle().
# Remember to debug your canvas drawing code before shuffling to make debugging easier.
#
random.shuffle(num)
# 5. 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.
#
# 6. Modify the event handler for mouse clicks to flip cards based on the location of the mouse click.
# 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.
#
# 7. 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 the example code discussed 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.
#
# 8. 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.
#
# 9. 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.
#
# Globals used in handle_mouseclick:
state = 0
left_index = 0
right_index = 0
#
def handle_mouseclick(pos):
global exposed
global state
global left_index
global right_index
global turn
# Starting point is the user's click position
index = pos[0] // 50
# If the user clicked on a visible item, do nothing
if exposed[index]:
return None
# Otherwise, iterate the cards and do some magic:
exposed[index] = True
# Set the left/right indexes
if state == 0:
left_index = index
elif state == 1:
right_index = index
elif state == 2:
# Flip the cards face down
if num[left_index] != num[right_index]:
exposed[right_index] = False
exposed[left_index] = False
#
left_index = index
# Increment the state counter
# 0 -> 1
# 1 -> 2
# 2 -> 1
state = state % 2 + 1
# Increment the turn counter
turn += 1
# 2. Write a draw handler that iterates through the Memory deck using a 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.
#
# 4. 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 the exposed list, the ith entry should be True if the ith card is face up and its value is visible or False if the ith 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.
#
def handle_draw(canvas):
global num
global label
# Update the label with the current turn
label.set_text("Moves = "+str(turn))
# Iterate the cards and update the visible states
for i in range(len(num)):
# Current card and x position
number = num[i]
x_offset = i * 50
x_center = (x_offset, 100)
if exposed[i]:
# The current card is visible
canvas.draw_text(str(number), x_center, 100, "White")
else:
# The current card is hidden
canvas.draw_polygon( [ (x_offset, 0), x_center, (x_offset + 50, 100), (x_offset + 50, 0) ], 1, "Green", "Green")
# 10. Finally, implement the 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.
#
def new_game():
# Re-shuffle the cards
random.shuffle(num)
# Re-set the visible states
for i in range(len(num)):
exposed[i] = False
# Back to turn 0x
turn = 0
#
# Setup the GUI
#
frame = simplegui.create_frame("Memory", 800, 100)
frame.set_mouseclick_handler(handle_mouseclick)
frame.set_draw_handler(handle_draw)
#
frame.add_button("Restart", new_game)
#
label = frame.add_label("Moves = 0")
#
frame.start()
# Always remember to review the grading rubric
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment