-
Notifications
You must be signed in to change notification settings - Fork 0
/
life.py
216 lines (163 loc) · 5.13 KB
/
life.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import random
import time
import mido
import re
#constants for epic gaming
GRIDSIZE = 8
SLEEPTIME = 0.4
CellOnColour = 3
CellOffColour = 0
print (mido.get_output_names())
print (mido.get_input_names())
#take note and return coord
def coordToNote(x,y):
#0,0 > 81
x = x + 1
y = GRIDSIZE - y
note = int(f"{y}{x}")
return note
def noteToCoord(note):
y = int(str(note)[0])
x = int(str(note)[1])
#the number 8 should become 0, the number 7 should become 1, etc.
y = GRIDSIZE - y
x = x - 1
return x,y
def midiSend(note, state):
if state == 1:
velocity = CellOnColour
else:
velocity = CellOffColour
if note >= 11 and note <= 88:
msg = mido.Message('note_on', note=note, velocity=velocity)
outPort.send(msg)
#set the grid to all 0s
def clearGrid():
for i in range(GRIDSIZE):
for j in range(GRIDSIZE):
setCoord(i,j,0)
midiSend(coordToNote(i,j), 0)
velocity = CellOffColour
for i in range(19,90):
msg = mido.Message("note_on", note=i, velocity=velocity)
outPort.send(msg)
for i in range(104,112):
msg = mido.Message("control_change", control=i, value=velocity)
outPort.send(msg)
#set a given coordinate to a given value
def setCoord(x,y,val):
grid[x][y] = val
#get the value of a given coordinate
def getCoord(x,y):
return grid[x][y]
#game of life rules
def gameOfLife(x,y):
#count the number of neighbours without counting cells outside the grid
def check_neighbour(x,y):
count = 0
for i in range(x-1,x+2):
for j in range(y-1,y+2):
if i >= 0 and i < GRIDSIZE and j >= 0 and j < GRIDSIZE and not(i==x and j==y):
count += getCoord(i,j)
return count
neighbours = check_neighbour(x,y)
#if the cell is alive and has 2 or 3 neighbours, it stays alive
if getCoord(x,y) == 1 and neighbours == 2 or neighbours == 3:
return 1
#if the cell is alive and has less than 2 neighbours, it dies
elif getCoord(x,y) == 1 and neighbours < 2:
return 0
#if the cell is alive and has more than 3 neighbours, it dies
elif getCoord(x,y) == 1 and neighbours > 3:
return 0
#if the cell is dead and has 3 neighbours, it comes to life
elif getCoord(x,y) == 0 and neighbours == 3:
return 1
#if the cell is dead and has less than 3 neighbours, it stays dead
elif getCoord(x,y) == 0 and neighbours < 3:
return 0
#if the cell is dead and has more than 3 neighbours, it stays dead
elif getCoord(x,y) == 0 and neighbours > 3:
return 0
else:
return 0
#update the grid
def updateGrid():
def setNewCoord(x,y,val):
newGrid[x][y] = val
def getNewCoord(x,y):
return newGrid[x][y]
newGrid = [[0 for x in range(GRIDSIZE)] for y in range(GRIDSIZE)]
for i in range(GRIDSIZE):
for j in range(GRIDSIZE):
setNewCoord(i,j,gameOfLife(i,j))
if newGrid == grid:
print ("no change")
return False
if newGrid != grid:
for i in range(GRIDSIZE):
for j in range(GRIDSIZE):
setCoord(i,j,getNewCoord(i,j))
return True
#print the grid
def printGrid():
for j in range(GRIDSIZE):
for i in range(GRIDSIZE):
print(getCoord(i,j), end = " ")
print()
print()
def printLaunchpad():
for i in range(GRIDSIZE):
for j in range(GRIDSIZE):
midiSend(coordToNote(i,j), getCoord(i,j))
def randomizeGrid():
for i in range(GRIDSIZE):
for j in range(GRIDSIZE):
setCoord(i,j,random.randint(0,1))
#play the game
def play():
while updateGrid():
printGrid()
printLaunchpad()
time.sleep(SLEEPTIME)
#create an 8x8 grid
grid = [[0 for x in range(GRIDSIZE)] for y in range(GRIDSIZE)]
outPort = mido.open_output('6- Launchpad MK2 1')
inPort = mido.open_input('6- Launchpad MK2 0')
def extractNoteFromData(inputData):
inputData = str(inputData)
if (re.search(' velocity=0 ', inputData)) != None:
return None
else:
m = re.search(' note=(.+?) ', inputData)
if m:
note = m.group(1)
return note
def drawingMode():
clearGrid()
while True:
inputData = inPort.receive()
note = extractNoteFromData(inputData)
if note == None:
continue
if note == "89":
break
if note == "79":
randomizeGrid()
break
else:
#print (getCoord(noteToCoord(note)[0], noteToCoord(note)[1]))
if getCoord(noteToCoord(note)[0], noteToCoord(note)[1]) == 0:
setCoord(noteToCoord(note)[0], noteToCoord(note)[1], 1)
printGrid()
printLaunchpad()
else:
setCoord(noteToCoord(note)[0], noteToCoord(note)[1], 0)
printGrid()
printLaunchpad()
printLaunchpad()
time.sleep(SLEEPTIME)
play()
if __name__ == "__main__":
while True:
drawingMode()