-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
188 lines (151 loc) · 5.11 KB
/
main.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
# Conway's Game of Life
import tkinter as tk
from time import sleep
from threading import Thread
# Logic part
class Board:
def __init__(self, rows, columns):
self.rows = rows
self.columns = columns
self.board = [[0 for _ in range(columns)]for _ in range(rows)]
def countLiveNeighbours(self, row, column):
fields = [(-1, -1), (-1, 0), (-1, 1), (0, -1),
(0, 1), (1, -1), (1, 0), (1, 1)]
alive = 0
for i, j in fields:
if 0 <= row+i < self.rows and 0 <= column+j < self.columns:
if self.board[row+i][column+j] == 1:
alive += 1
return alive
def countLiveCells(self):
alive = 0
for row in self.board:
for cell in row:
if cell:
alive += 1
return alive
def update(self):
board = [[0 for _ in range(self.columns)]for _ in range(self.rows)]
for row in range(self.rows):
for column in range(self.columns):
live_cells = self.countLiveNeighbours(row, column)
if self.board[row][column]:
if live_cells == 3:
board[row][column] = 1
else:
if live_cells == 2 or live_cells == 3:
board[row][column] = 1
self.board = board
def show(self):
for row in self.board:
print(row)
print()
# Gui stuff
class Tile:
def __init__(self, root):
self.state = 0
self.button = tk.Button(root, bg="slate blue", command=self.onClick)
# strange, this is a square...
self.button.configure(height=2, width=4)
def onClick(self):
if self.state:
self.button.configure(bg="slate blue")
self.state = 0
else:
self.button.configure(bg="snow")
self.state = 1
# info = self.button.grid_info()
# print(info.get("row"), info.get("column"))
# Putting it all togheter
class App:
def __init__(self):
# TODO - parametrize this
rows = 9
columns = 9
self.game = Board(rows, columns)
self.root = tk.Tk()
self.root.title("Conway's Game of Life")
self.centerWindow()
self.gamePanel = tk.Frame(self.root)
self.gamePanel.pack()
self.gui = self.makeGrid(self.gamePanel)
self.cmdPanel = tk.Frame(self.root)
self.cmdPanel.pack()
self.start = tk.Button(self.cmdPanel, text="Start", command=self.run)
self.start.pack()
self.stopButton = tk.Button(
self.cmdPanel, text="Stop", command=self.stop)
self.stopButton.pack()
self.resetButton = tk.Button(
self.cmdPanel, text="Reset", command=self.reset)
self.resetButton.pack()
self.speedLabel = tk.Label(self.cmdPanel, text="Speed")
self.speedLabel.pack()
self.slider = tk.Scale(self.cmdPanel, from_=1,
to=30, orient="horizontal")
self.slider.pack()
self.isRunning = False
def _run(self):
self.isRunning = True
row = 0
for A, B in zip(self.gui, self.game.board):
column = 0
for a, b in zip(A, B):
if a.state == b:
pass
else:
self.game.board[row][column] = a.state
column += 1
row += 1
# self.game.show()
while self.isRunning:
self.game.update()
for A, B in zip(self.gui, self.game.board):
for a, b in zip(A, B):
if a.state == b:
pass
else:
a.onClick()
# self.game.show()
self.root.update_idletasks()
sleep(1/self.slider.get())
def run(self):
if not self.isRunning:
thread = Thread(target=self._run)
thread.start()
def stop(self):
self.isRunning = False
def reset(self):
self.isRunning = False
row = 0
for A, B in zip(self.gui, self.game.board):
column = 0
for a, b in zip(A, B):
if a.state:
a.onClick()
self.game.board[row][column] = 0
column += 1
row += 1
def centerWindow(self):
width = 500
height = 600
x = (self.root.winfo_screenwidth() // 2) - (width // 2)
y = (self.root.winfo_screenheight() // 2) - (height // 2)
self.root.geometry('{}x{}+{}+{}'.format(width, height, x, y))
def makeGrid(self, root):
Tiles = []
for row in range(self.game.rows):
tiles = []
for column in range(self.game.columns):
tile = Tile(root)
tile.button.grid(row=row, column=column)
tiles.append(tile)
Tiles.append(tiles)
return Tiles
def mainloop(self):
self.root.mainloop()
# Main
if __name__ == "__main__":
# TODO - auto adjust window size
app = App()
app.mainloop()