-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConwaysGameOfLife.py
300 lines (249 loc) · 7.34 KB
/
ConwaysGameOfLife.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import tkinter as tk
from random import randint
import time
CHEIGHT = 1000
CWIDTH = 1000
INCREMENT = 50
class WindowHandler():
def __init__(self):
self.canvasHeight = CHEIGHT
self.canvasWidth = CWIDTH
self.root = tk.Tk()
self.canvas = tk.Canvas(
width=self.canvasWidth,height=self.canvasHeight,
bg='black')
self.canvas.pack()
self.initButtons()
self.bindEvents()
self.drawer = CanvasDrawer(self.canvas,self.canvasWidth)
self.game = GameLogic(INCREMENT)
self.turnON = True
self.drawer.initCells(self.game.cellBoard)
def bindEvents(self):
self.root.bind("<Key>",self.keyPress)
self.root.bind("<Button-1>",self.mousePress)
self.root.bind("<B1-Motion>",self.mouseHold)
def mousePress(self,event):
x2 = self.root.winfo_pointerx() - self.root.winfo_rootx()
y2 = self.root.winfo_pointery() - self.root.winfo_rooty()
if y2 > 1000:
return
x,y = self.convertMouse(event.x,event.y)
if self.game.cellBoard[y][x].state == True:
self.turnON = False
else:
self.turnON = True
self.drawer.changeCellState(x,y,self.game.cellBoard)
def mouseHold(self,event):
x2 = self.root.winfo_pointerx() - self.root.winfo_rootx()
y2 = self.root.winfo_pointery() - self.root.winfo_rooty()
if y2 > 1000:
return
x,y = self.convertMouse(event.x,event.y)
if self.turnON == True:
if self.game.cellBoard[y][x].state == False:
self.drawer.changeCellState(x,y,self.game.cellBoard)
else:
if self.game.cellBoard[y][x].state == True:
self.drawer.changeCellState(x,y,self.game.cellBoard)
def initButtons(self):
self.stepForwardButton = tk.Button(self.root,text="ONE STEP",command=self.moveForwardOneStep,width=15)
self.stepForwardButton.pack(side='right')
self.goForwardNSteps = tk.Button(self.root,text="X STEPS",command=self.moveForwardNSteps,width=15)
self.goForwardNSteps.pack(side='left')
self.speedLabel = tk.Label(self.root,text="Speed")
self.speedInput = tk.Entry(self.root)
self.speedLabel.pack(side='left')
self.speedInput.pack(side='left')
self.numStepsLabel = tk.Label(self.root,text='# of steps')
self.numStepsInput = tk.Entry(self.root)
self.numStepsLabel.pack(side='left')
self.numStepsInput.pack(side='left')
self.resetButton = tk.Button(self.root,text="RESET",command=self.resetStuff)
self.resetButton.pack()
def resetStuff(self):
for y,row in enumerate(self.game.cellBoard):
for x,cell in enumerate(row):
if cell.state == True:
self.drawer.changeCellState(x,y,self.game.cellBoard)
cell.state = False
self.root.update()
def moveForwardNSteps(self):
try:
speed = float(self.speedInput.get())
steps = int(self.numStepsInput.get())
except:
speed = .25
steps = 10
for x in range(steps):
print(f'step: {x+1}/{steps}')
self.moveForwardOneStep()
time.sleep(speed)
self.root.update()
def convertMouse(self,x,y):
dX = self.canvasWidth/INCREMENT
realX = int(x/dX)
realY = int(y/dX)
return (realX,realY)
def keyPress(self,event):
if event.char == "q":
self.root.destroy()
def moveForwardOneStep(self):
cellBoardCopy = []
for row in self.game.cellBoard:
tempRow = []
for cell in row:
tempRow.append(cell.state)
cellBoardCopy.append(tempRow)
for y,row in enumerate(cellBoardCopy):
for x,cell in enumerate(row):
if self.judgeCell(x,y):
if cellBoardCopy[y][x] == True:
cellBoardCopy[y][x] = False
else:
cellBoardCopy[y][x] = True
else:
pass
for y,row in enumerate(cellBoardCopy):
for x,cell in enumerate(row):
if self.game.cellBoard[y][x].state != cell:
self.drawer.changeCellState(x,y,self.game.cellBoard)
def judgeCell(self,x,y):
## returns true if should change
## returns false if not
cell = self.game.cellBoard[y][x]
cells = self.game.getNeighbors(x,y)
counter = self.game.countLive(cells)
if cell.state == True:
if self.game.ruleOne(counter):
return True
elif self.game.ruleTwo(counter):
return True
elif self.game.ruleThree(counter):
return False
else:
if self.game.ruleFour(counter):
return True
else:
return False
def mainLoop(self):
while True:
try:
self.root.update()
except:
print('goodbye')
break
class CanvasDrawer():
def __init__(self,canvas,width):
self.canvas = canvas
self.dX = int(width/INCREMENT)
def drawCell(self,cell):
width = self.dX
x1 = cell.x*width
x2 = x1+width
y1 = cell.y*width
y2 = y1+width
cell.drawnImage = self.canvas.create_rectangle(x1,y1,x2,y2,fill='black',outline='white')
def initCells(self,cellBoard):
for row in cellBoard:
for cell in row:
self.drawCell(cell)
def changeCellState(self,x,y,cellBoard):
cell = cellBoard[y][x]
if cell.state == True:
self.canvas.itemconfig(cell.drawnImage,fill='black',outline='white')
else:
self.canvas.itemconfig(cell.drawnImage,fill='white',outline='black')
cell.toggleState()
class GameLogic():
def __init__(self,width):
self.width = width
self.cellBoard = [[ 0 for x in range(width)] for x in range(width)]
self.insertCells()
def insertCells(self):
for y,row in enumerate(self.cellBoard):
for x,cell in enumerate(row):
self.cellBoard[y][x] = Cell(self,x,y)
def getNeighbors(self,x,y):
boundary = len(self.cellBoard)-1
points = []
topLeft = (x-1,y-1)
top = (x,y-1)
topRight = (x+1,y-1)
right = (x+1,y)
bottomRight = (x+1,y+1)
bottom = (x,y+1)
bottomLeft = (x-1,y+1)
left = (x-1,y)
if x == 0 and y == 0:
points += [right,bottomRight,bottom]
elif x == 0 and y == boundary:
points += [top,topRight,right]
elif x == boundary and y == 0:
points += [left,bottomLeft,bottom]
elif x == boundary and y == boundary:
points += [left,topLeft,top]
elif x == boundary and y > 0 and y < boundary:
points += [top,topLeft,left,bottomLeft,bottom]
elif x == 0 and y > 0 and y < boundary:
points += [top,topRight,right,bottomRight,bottom]
elif y == boundary and x > 0 and x < boundary:
points += [left,topLeft,top,topRight,right]
elif y == 0 and x > 0 and x < boundary:
points += [left,bottomLeft,bottom,bottomRight,right]
else:
points += [topLeft,top,topRight,right,
bottomRight,bottom,bottomLeft,left]
values = []
for x,y in points:
values.append(self.cellBoard[y][x])
return values
def countLive(self,values):
counter = 0
for cell in values:
if cell.state == True:
counter += 1
return counter
def ruleOne(self,counter):
## Any live cell with fewer than two live
## neighbours dies (referred to as underpopulation or exposure)
## true = dies
if counter < 2:
return True
else:
return False
def ruleTwo(self,counter):
## Any live cell with more than three live neighbours
## dies (referred to as overpopulation or overcrowding).
## true = dies
if counter > 3:
return True
else:
return False
def ruleThree(self,counter):
## Any live cell with two or three live neighbours lives,
## unchanged, to the next generation.
## true = lives
if counter == 2 or counter == 3:
return True
else:
return False
def ruleFour(self,counter):
## Any dead cell with exactly three live neighbours will come to life.
if counter == 3:
return True
else:
return False
class Cell():
def __init__(self,game,x,y):
self.game = game
self.state = False
self.x = x
self.y = y
def toggleState(self):
if self.state == True:
self.state = False
else:
self.state = True
window = WindowHandler()
window.mainLoop()