-
Notifications
You must be signed in to change notification settings - Fork 0
/
GOL_custom_init.py
62 lines (50 loc) · 1.94 KB
/
GOL_custom_init.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
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
class DrawingApp:
def __init__(self, N):
self.board = np.zeros(shape=(N, N))
self.fig, self.ax = plt.subplots()
self.ax.set_title('Click on the canvas to draw')
self.cid = self.fig.canvas.mpl_connect('button_press_event', self.on_click)
def on_click(self, event):
if event.button == 1: # Left-click to draw
x, y = event.xdata, event.ydata
if x is not None and y is not None:
_x = abs(int(x))
_y = abs(int(y))
self.board[_y,_x] = 1
print(f"Clicked at: ({x:.2f}, {y:.2f})--> {_x}, {_y}")
self.ax.plot(x, y, 'ro')
self.fig.canvas.draw_idle()
# Function to update the board based on the rules of the Game of Life
def update_board(board):
new_board = np.zeros(board.shape)
for i in range(board.shape[0]):
for j in range(board.shape[1]):
total_neighbors = np.sum(board[max(0, i-1):min(board.shape[0], i+2), max(0, j-1):min(board.shape[1], j+2)]) - board[i, j]
if board[i, j] == 1:
if total_neighbors < 2 or total_neighbors > 3:
new_board[i, j] = 0
else:
new_board[i, j] = 1
else:
if total_neighbors == 3:
new_board[i, j] = 1
return new_board
def update_plot(frameNum, img, board):
new_board = update_board(board)
img.set_data(new_board)
board[:] = new_board[:]
return img,
if __name__ == "__main__":
N = 15
drawing_app = DrawingApp(N)
plt.xlim(0, N)
plt.ylim(-N, 0)
plt.show()
fig, ax = plt.subplots()
board = drawing_app.board
img = ax.imshow(board, interpolation='nearest')
ani = animation.FuncAnimation(fig, update_plot, frames=100, fargs=(img, board), interval=100)
plt.show()