-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameOfLife.py
103 lines (92 loc) · 3.84 KB
/
GameOfLife.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
from Grid import Grid
import random
class GameOfLife:
DEAD = 0
ALIVE = 1
def __init__(self, num_columns, num_rows):
self.num_columns = num_columns
self.num_rows = num_rows
self.reset_grid()
def reset_grid(self):
self.grid = Grid(self.num_rows, self.num_columns, GameOfLife.DEAD)
def get_num_live_neighbors(self, column, row):
num_live_neighbors = 0
for neighbor_location in self.get_neighbor_locations(column, row):
try:
if self.grid[neighbor_location[0]][neighbor_location[1]] == GameOfLife.ALIVE:
num_live_neighbors += 1
except IndexError:
pass
return num_live_neighbors
def get_neighbor_locations(self, column, row):
neighbor_locations = []
neighbor_locations.append((column - 1, row - 1))
neighbor_locations.append((column - 1, row))
neighbor_locations.append((column - 1, row + 1))
neighbor_locations.append((column, row + 1))
neighbor_locations.append((column, row - 1))
neighbor_locations.append((column + 1, row - 1))
neighbor_locations.append((column + 1, row))
neighbor_locations.append((column + 1, row + 1))
return neighbor_locations
def time_step(self):
new_grid = Grid(self.grid.getHeight(), self.grid.getWidth())
for column in range(self.grid.getWidth()):
for row in range(self.grid.getHeight()):
is_alive = self.grid[column][row] == GameOfLife.ALIVE
num_living_neighbors = self.get_num_live_neighbors(column, row)
if is_alive and num_living_neighbors < 2:
new_grid[column][row] = GameOfLife.DEAD
elif is_alive and (num_living_neighbors == 2 or num_living_neighbors == 3):
new_grid[column][row] = GameOfLife.ALIVE
elif is_alive and num_living_neighbors > 3:
new_grid[column][row] = GameOfLife.DEAD
elif not is_alive and num_living_neighbors == 3:
new_grid[column][row] = GameOfLife.ALIVE
else:
new_grid[column][row] = self.grid[column][row]
self.grid = new_grid
def random_grid_init(self):
for column in range(self.grid.getWidth()):
for row in range(self.grid.getHeight()):
rand_state = random.choice([GameOfLife.ALIVE, GameOfLife.DEAD])
self.grid[column][row] = rand_state
def insert_pulsar(self, location):
points = []
for col in [2, 7, 9, 14]:
for row in [4, 5, 6, 10, 11, 12]:
points.append((col + location[0], row + location[1]))
for row in [2, 7, 9, 14]:
for col in [4, 5, 6, 10, 11, 12]:
points.append((col + location[0], row + location[1]))
for point in points:
try:
self.grid[point[0]][point[1]] = GameOfLife.ALIVE
except IndexError:
pass
def insert_glider(self, location):
alive_points = [(1,0), (2,1), (0,2), (1,2), (2,2)]
for col in range(3):
for row in range(3):
try:
self.grid[col + location[0]][row + location[1]] = GameOfLife.DEAD
except IndexError:
pass
for alive_point in alive_points:
try:
self.grid[alive_point[0] + location[0]][alive_point[1] + location[1]] = GameOfLife.ALIVE
except IndexError:
pass
if __name__ == "__main__":
g = GameOfLife(15, 15)
g.grid[1][1] = GameOfLife.ALIVE
g.grid[1][2] = GameOfLife.ALIVE
g.grid[2][2] = GameOfLife.ALIVE
g.grid[3][3] = GameOfLife.ALIVE
print(g.grid)
g.time_step()
print(g.grid)
g.time_step()
print(g.grid)
g.time_step()
print(g.grid)