-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconways-game-of-life-forced
69 lines (62 loc) · 2.45 KB
/
conways-game-of-life-forced
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
#alive = 1, dead = 0
cell_history = {}
for each in range(100):
cell_history.update({each:[0]})
cell_history[44] =[1]
cell_history[55] =[1]
cell_history[56] =[1]
cell_history[64] =[1]
cell_history[65] =[1]
grid = [tuple([a,b]) for a in range(10) for b in range(10)]
def get_neighbours(cell):
list_of_neighbours = []
relative_positions = [tuple([a,b]) for a in [-1,0,1] for b in [-1,0,1]]
relative_positions.remove((0,0))
for each in relative_positions:
x = int(each[0]+cell[0])
y = int(each[1]+cell[1])
#below checks new x.y coordinates within the board
if x in (-1,-2, 10,11) or y in (-1,-2, 10,11):
pass
else:
list_of_neighbours.append((x, y))
return(list_of_neighbours)
def neighbours_to_n(cell):
stringed = str(cell)[1:-1].replace(",", "").replace(" ","")
return int(stringed)
for time_steps in range(100):
for position_index in range(100):
no_of_neighbours = 0
has_been_alive = 'no'
if cell_history[position_index][time_steps] == 1:
# if the 'current' cell is alive, otherwise makes investigation of its neighbours relevant
for each_neighbour in get_neighbours(grid[position_index]):
# Going through each neigbour to currently 'selected' cell
neighbour_cell = neighbours_to_n(each_neighbour)
if cell_history[neighbour_cell][time_steps] == 1:
no_of_neighbours+=1
else:
# neighbour is dead
pass
if no_of_neighbours in (2,3):
# will live on
cell_history[position_index].append(1)
else:
# will die
cell_history[position_index].append(0)
else:
# if dead/0 but has 3 neighbours
for each_neighbour in get_neighbours(grid[position_index]):
# Going through each neigbour to currently 'selected' cell
neighbour_cell = neighbours_to_n(each_neighbour)
if cell_history[neighbour_cell][time_steps] == 1:
no_of_neighbours+=1
else:
#neighbour is dead
pass
if no_of_neighbours == 3:
cell_history[position_index].append(1)
else:
cell_history[position_index].append(0)
#return end state for each cell
[cell_history[i][-1] for i in range(100)]