-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemory.py
111 lines (77 loc) · 2.53 KB
/
Memory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# implementation of card game - Memory
import simplegui
import random
# globals
numbers = []
cards_to_points = {}
closed = False
isopen = True
turns = 0
opened = []
# helper function to initialize globals
def new_game():
global numbers, turns, label
turns = 0
numbers = range(1, 9)
numbers = numbers * 2
random.shuffle(numbers)
x = 0
y = 100
for i in range(0,16):
cards_to_points[i] = [numbers[i], [[x, y-100], [x, y], [x+50, y], [x+50, y-100]], closed]
x += 50
label.set_text("Turns = %d" % turns)
# define event handlers
def mouseclick(pos):
global cards_to_points, opened, label, turns
x, y = pos
position_number = int(x)/50
card = cards_to_points[position_number]
if card[2] == isopen:
# do nothing
return
number = card[0]
card[2] = isopen
opened.append((position_number, number))
if len(opened) == 2:
# Set the turns every 2nd click
turns += 1
label.set_text("Turns = %d" % turns)
if len(opened) == 3:
# On the third exposed, check for pairing.
first = opened[0]
second = opened[1]
first_position_number = first[0]
first_number = first[1]
second_position_number = second[0]
second_number = second[1]
if first_number == second_number:
cards_to_points[first_position_number][2] = isopen
cards_to_points[second_position_number][2] = isopen
else:
cards_to_points[first_position_number][2] = closed
cards_to_points[second_position_number][2] = closed
# Remove the first two.
opened = opened[2:]
# cards are logically 50x100 pixels in size
def draw(canvas):
for value in cards_to_points.values():
num = value[0]
position = value[1]
is_open = value[2]
canvas.draw_text(str(num), position[1], 100, "White")
if is_open:
canvas.draw_polygon(position, 1, "White")
else:
canvas.draw_polygon(position, 1, "White", "Green")
# create frame and add a button and labels
frame = simplegui.create_frame("Memory", 800, 100)
frame.add_button("Restart", new_game)
label = frame.add_label("Turns = %d" % turns)
# 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