This repository was archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.py
More file actions
128 lines (100 loc) · 3.5 KB
/
simulation.py
File metadata and controls
128 lines (100 loc) · 3.5 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import numpy as np
def survival(coordinates,universe):
'''
Checks surrounding cells to determine if the cell will be alive or dead afterwards
Parameters
-------
coordinates
Tuple (y,x) of selected cell
universe
Numpy array of initial universe containing selected cell
Returns
-------
final state of cell
1 for alive and 0 for dead
'''
# Count the number of live neighbours
count = 0
y = coordinates[0]
x = coordinates[1]
num_rows_univ,num_cols_univ = universe.shape
for i in [-1,0,1]:
for j in [-1,0,1]:
if i == 0 and j == 0:
continue # Do not include cell whose neighbours are to be checked
modified_y = (y + i) % num_rows_univ # Implement torroidal universe
modified_x = (x + j) % num_cols_univ # Implement torroidal universe
count += universe[modified_y,modified_x]
# Judge survivability
if universe[y,x] and (count == 2 or count == 3): # Live cell stays alive
return 1
elif not universe[y,x] and count == 3: # Dead cell becomes alive
return 1
else: # All other conditions that lead to a dead cell afterwards
return 0
def generation(universe):
'''
Generates the next iteration of the universe
Parameters
-------
universe
Numpy array of initial universe on which the next iteration is to be generated
Returns
--------
new_universe
Numpy array of the new universe obtained
'''
# Identify size of given universe
num_rows_univ,num_cols_univ = universe.shape
# Create a new universe on which values derived from the survival function on given universe are later added
new_universe = np.zeros([num_rows_univ,num_cols_univ])
for i in range(0,num_rows_univ):
for j in range(0,num_cols_univ):
new_universe[i,j] = survival((i,j),universe)
return new_universe
def game_life_simulate(universe, iterations, universe_dict):
'''
Simulation of the game over given number of iterations
Parameters
-------
universe
Numpy array of initial universe
iterations
Integer of the number of iterations to be generated
universe_dict
Dictionary containing the number of iterations as key and generated universe as value
Returns
--------
latest_universe
Numpy array of final universe
'''
# Return given universe if no iteration is requested
if iterations == 0:
return universe
# # Loop over the given number of iterations to generate latest universe.
# latest_universe = None
# for i in range(0,iterations):
# latest_universe = generation(universe)
# universe = latest_universe
# return latest_universe
# Return generated universe if it already exists in universe_dict.
elif iterations in universe_dict:
return universe_dict[iterations]
# Use of recursion and memoisation to generate latest universe.
else:
universe_dict[iterations] = generation(game_life_simulate(universe,iterations - 1,universe_dict))
return universe_dict[iterations]
def reset_universe_dict(universe_dict):
'''
Resets universe_dict for correct usage of game_life_simulate
Parameters
-------
universe_dict
universe_dict generated by previous usage of game_life_simulate
Returns
-------
universe_dict
universe_dict reset
'''
universe_dict.clear()
universe_dict = {}