-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemory.py
101 lines (83 loc) · 2.42 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
# The summer holiday, and then i cant't use the codeskulptor.So these are in my mind!--!
import simplegui
import random
WIDTH = 1600 # for 16 cards
HEIGHT = 150
memory = list((range(0, 8)))
memory.extend(range(0, 8))
state = 0
turns = -1
def init_exposed():
global exposed, WIDTH, paired
exposed = [False in range(0, WIDTH / 100)]
paired = [-1, -1] # for pairing!
def new_game():
global state, memory, turns
init_exposed()
random.shuffle(memory)
state = 0
turns = -1
def estimate(): # state judging!
global state
if state == 0:
state = 1
elif state == 1:
state = 2
else:
state = 1
def run_button():
global frame
init_exposed()
frame.start()
def reset_button():
new_game()
def mouseclick(pos):
global turns, paired, exposed, l
estimate()
if 0 <= pos[1] <= 150 and 0 <= pos[0] <= 1600:
clicked = pos[0] // 100
else:
return
if turns == clicked:
pass
elif state == 1 and paired[0] == -1 and paired[1] == -1:
turns = clicked
paired[0] = turns
exposed[turns] = True
elif state == 1 and paired[0] != -1 and paired[1] == -1:
turns = clicked
paired[1] = turns
exposed[turns] = True
elif state == 1 and memory[paired[0]] == memory[paired[1]]:
paired[1] = -1
turns = clicked
paired[0] = turns
exposed[turns] = True
elif state == 1 and memory[paired[0]] != memory[paired[1]]:
exposed[paired[0]] = False
exposed[paired[1]] = False
paired[1] = -1
turns = clicked
paired[0] = turns
exposed[turns] = True
elif state == 2:
turns = clicked
paired[1] = turns
exposed[turns] = True
l.set_text('Turns = ' + turns)
def draw(canvas):
i = 0
for number in memory:
assert i <= 15, 'OutOfBoundsError' # prevent i from crossing the border
if exposed[i]:
canvas.draw_text(number, [30 + 100 * i, 75], 24, 'White')
else:
canvas.draw_polygon([(100 * i, 0), (100 * (i + 1), 0), (100 * (i + 1), 150), (100 * i, 150)], 12, 'Red',
'Green')
i += 1
frame = simplegui.create_frame('Memory', WIDTH, HEIGHT)
frame.add_button('Run', run_button, 100)
frame.add_button('Reset', reset_button, 100)
l = frame.add_label('Turns = ?')
frame.set_mouseclick_handler(mouseclick)
frame.set_draw_handler(draw)