-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame of Life.py
More file actions
42 lines (32 loc) · 1.32 KB
/
Game of Life.py
File metadata and controls
42 lines (32 loc) · 1.32 KB
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
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Set the dimensions of the grid
width, height = 50, 50
# Initialize the grid with random initial states
grid = np.random.choice([0, 1], size=(width, height))
# Function to update the grid for each generation
def update(frameNum, img, grid, width, height):
new_grid = grid.copy()
for x in range(width):
for y in range(height):
# Count the live neighbors
neighbors = [(x - 1, y - 1), (x - 1, y), (x - 1, y + 1),
(x, y - 1), (x, y + 1),
(x + 1, y - 1), (x + 1, y), (x + 1, y + 1)]
live_neighbors = sum(grid[i % width, j % height] for i, j in neighbors)
# Apply the rules of the Game of Life
if grid[x, y] == 1:
if live_neighbors < 2 or live_neighbors > 3:
new_grid[x, y] = 0
else:
if live_neighbors == 3:
new_grid[x, y] = 1
img.set_data(new_grid)
grid[:] = new_grid[:]
return img
# Create a figure and axis for visualization
fig, ax = plt.subplots()
img = ax.imshow(grid, interpolation='nearest', cmap='binary')
ani = animation.FuncAnimation(fig, update, fargs=(img, grid, width, height), frames=10, interval=200)
plt.show()