-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmagic_square.py
153 lines (135 loc) · 4.2 KB
/
magic_square.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# 0-8 used to draw a square
# extra buttons NEW GAME, SAME GAME
# goal is [1,1,1,1,0,1,1,1,1]
# 0 1 2
# 3 4 5
# 6 7 8
# corner buttons affect the three adjacent lights
# edge buttons affect the two corners on that edge
# center button affects all edge buttons
# 0 flips 0,1,3,4
# 1: 0,1,2
# 2: 1,2,4,5
# 3: 0,3,6
# 4: 1,3,4,5,7
# 5: 2,5,8
# 6: 3,4,6,7
# 7: 6,7,8
# 8: 4,5,6,7
from random import randint
import time
# init
# flash the square
# choose a random config
class magic_square():
def __init__(self, macropad, tones):
# shows how the kets affect others
self.keys = [
0b110110000,
0b111000000,
0b011011000,
0b100100100,
0b010111010,
0b001001001,
0b000110110,
0b000000111,
0b000011011]
self.state=0b0
self.start = 0b0
self.tones = tones
self.color = 0xff0000
self.macropad = macropad
print ("Magic Square is initialized")
#self.new_game()
def new_game(self):
print ("new Magic Square game")
# show a square for fun
self.macropad.pixels.fill((0,0,0))
self.color = 0x0009ff
self.state = 0b111101111
self.show_leds()
self.macropad.play_tone(self.tones[0], 0.5)
self.macropad.play_tone(self.tones[2], 0.5)
self.macropad.play_tone(self.tones[4], 0.5)
self.color = 0xff0000
#generate a new matrix
for x in range(8):
self.state=bin(randint(0, 511))
if (self.state == 0b111101111):
self.state = self.state + 1
print ("starting",self.state)
self.start = self.state
self.show_leds()
#need to change labels
self.macropad.pixels[9]=0xff9900
self.macropad.pixels[11]=0x00ff00
def same_game(self):
# show a square for fun
self.color = 0xff9900
self.state = 0b111101111
self.show_leds()
self.macropad.play_tone(self.tones[4], 0.5)
self.macropad.play_tone(self.tones[2], 0.5)
self.color = 0xff0000
self.state = self.start
self.show_leds()
def show_off(self):
# show a square for fun
self.color = 0x0009ff
self.state = 0b111101111
self.show_leds()
self.macropad.play_tone(self.tones[0], 0.5)
self.macropad.play_tone(self.tones[2], 0.5)
self.macropad.play_tone(self.tones[4], 0.5)
self.color = 0xff0000
#time.sleep(1)
def winner(self):
# do a winning thing
self.color = 0x00ff00
self.show_leds()
self.macropad.play_tone(self.tones[0], 0.2)
self.macropad.play_tone(self.tones[2], 0.2)
self.macropad.play_tone(self.tones[4], 0.2)
self.macropad.play_tone(self.tones[6], 0.2)
self.macropad.play_tone(self.tones[4], 0.2)
self.macropad.play_tone(self.tones[6], 0.5)
print ("you are a weiner")
def bits(self,n):
#print (bin(int(n)))
arr = [int(x) for x in bin(int(n))[2:]]
for x in range (0,(9-len(arr))):
arr.insert(0,0)
print (arr)
return arr
def show_leds(self):
# show the results
matrix = self.bits(self.state)
#print (matrix)
for x in range (len(matrix)):
if matrix[x]:
self.macropad.pixels[x] = self.color
else:
self.macropad.pixels[x] = 0x000000
# make boop
#self.macropad.play_tone(self.tones[7], 0.5)
def button(self,key):
if key==9:
self.same_game()
elif key ==11:
self.new_game()
elif key <9:
#print ("is",bin(self.state),", affects",bin(self.keys[key]))
self.macropad.play_tone(self.tones[key], 0.2)
self.state = int(self.state)^int(self.keys[key])
if (self.state == 0b111101111):
self.winner()
else:
self.show_leds()
else: # it's another button, weirdo
pass
def encoderChange(self,newPosition, oldPosition):
pass
# keypress
# take the key number, pull the modifier array, apply
# check for win
# show result