-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_of_life.py
86 lines (63 loc) · 2.7 KB
/
game_of_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
import pygame
import numpy as np
class GameOfLife:
def __init__(self, surface, width=1920, height=1080, scale=10, offset=1, active_color=(255, 255, 255), inactive_color=(50, 50, 50)):
self.surface = surface
self.width = width
self.height = height
self.scale = scale
self.offset = offset
self.active_color = active_color
self.inactive_color = inactive_color
self.columns = int(height / scale)
self.rows = int(width / scale)
self.grid = np.random.randint(0, 2, size=(self.rows, self.columns), dtype=bool)
def run(self):
"""Update and redraw the current grid state"""
self.draw_grid()
self.update_grid()
def draw_grid(self):
"""Drawing the grid"""
for row in range(self.rows):
for col in range(self.columns):
if self.grid[row, col]:
pygame.draw.rect(self.surface, self.active_color, [row * self.scale, col * self.scale, self.scale - self.offset, self.scale - self.offset])
else:
pygame.draw.rect(self.surface, self.inactive_color, [row * self.scale, col * self.scale, self.scale - self.offset, self.scale - self.offset])
def update_grid(self):
"""Updating the gid according to Conway's Game of Life rules"""
updated_grid = self.grid.copy()
for row in range(updated_grid.shape[0]):
for col in range(updated_grid.shape[1]):
updated_grid[row, col] = self.update_cell(row, col)
self.grid = updated_grid
def update_cell(self, x, y):
"""Update single cell based on Conway's Game of Life rules"""
current_state = self.grid[x, y]
alive_neighbors = 0
# Get number of alive neighbors
for i in range(-1, 2):
for j in range(-1, 2):
try:
if i == 0 and j == 0:
continue
elif self.grid[x + i, y + j]:
alive_neighbors += 1
except:
continue
# Update cell's state
# Dies by underpopulation
if current_state and alive_neighbors < 2:
return False
# Live to next generation
elif current_state and (alive_neighbors == 2 or alive_neighbors == 3):
return True
# Dies by overpopulation
elif current_state and alive_neighbors > 3:
return False
# Alive by reproduction
elif ~current_state and alive_neighbors == 3:
return True
# Otherwise no change
else:
return current_state