-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdifferent_world_physics.py
306 lines (268 loc) · 9.9 KB
/
different_world_physics.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# Noah Yi, GitHub: NoahTheCorgi
# future task: apply Hashlife algorithm
import time
import random
n = 100
N = n * n # total number of cells
# randomized game of life world initial state generation
def determine_new_world():
wworld = []
i = 0
while i < N:
# wworld.append(" ")
# random.choice([" ","*"]))
wworld.append(" ")
i += 1
return wworld
def show_world_terminal(world):
# this is to display on the terminal
j = 0
L = []
the_word = ""
while j < N:
L.append(world[j : n + j])
j += n
k = 0
while k < n:
l = 0
while l < n:
the_word = the_word + L[k][l] + " "
l += 1
k += 1
the_word += "\n"
print(the_word)
# world/realm physics: original laws by Conway
def create_next_state_original(previous):
nbhr_count = [0] * N # neighborhood count
# addressing the corners first:
# top left corner
if previous[0] == "*":
nbhr_count[(0) + (0) * n + 1] += 1
nbhr_count[(0) + (0) * n + n] += 1
nbhr_count[(0) + (0) * n + 1 + n] += 1
# top right corner
if previous[n - 1] == "*":
nbhr_count[(n - 1) + (0) * n - 1] += 1
nbhr_count[(n - 1) + (0) * n + n] += 1
nbhr_count[(n - 1) + (0) * n - 1 + n] += 1
# bottom left corner
if previous[(n - 1) * n] == "*":
nbhr_count[(0) + (n - 1) * n + 1] += 1
nbhr_count[(0) + (n - 1) * n - n] += 1
nbhr_count[(0) + (n - 1) * n + 1 - n] += 1
# bottom right corner
if previous[(n - 1) * n + n - 1] == "*":
nbhr_count[(n - 1) + (n - 1) * n - 1] += 1
nbhr_count[(n - 1) + (n - 1) * n - n] += 1
nbhr_count[(n - 1) + (n - 1) * n - 1 - n] += 1
# next, addressing the edges without the corners....
# e1. addressing the top horizontal edge
b = 1
while b < n - 1:
if previous[b] == "*":
nbhr_count[(b) + (0) * n + 1] += 1
nbhr_count[(b) + (0) * n - 1] += 1
nbhr_count[(b) + (0) * n + n] += 1
nbhr_count[(b) + (0) * n + 1 + n] += 1
nbhr_count[(b) + (0) * n - 1 + n] += 1
b += 1
# e2. addressing the bottom horizontal edge
b = 1
while b < n - 1:
if previous[(n - 1) * n + b] == "*":
nbhr_count[(b) + (n - 1) * n + 1] += 1
nbhr_count[(b) + (n - 1) * n - 1] += 1
nbhr_count[(b) + (n - 1) * n - n] += 1
nbhr_count[(b) + (n - 1) * n + 1 - n] += 1
nbhr_count[(b) + (n - 1) * n - 1 - n] += 1
b += 1
# e3. addresssing the left vertical edge
a = 1
while a < n - 1:
if previous[a * n] == "*":
nbhr_count[(0) + (a) * n - n] += 1
nbhr_count[(0) + (a) * n - n + 1] += 1
nbhr_count[(0) + (a) * n + 1] += 1
nbhr_count[(0) + (a) * n + n] += 1
nbhr_count[(0) + (a) * n + n + 1] += 1
a += 1
# e4. addressing the right vertical edge
a = 1
while a < n - 1:
if previous[a * n + n - 1] == "*":
nbhr_count[(n - 1) + (a) * n - n] += 1
nbhr_count[(n - 1) + (a) * n - n - 1] += 1
nbhr_count[(n - 1) + (a) * n - 1] += 1
nbhr_count[(n - 1) + (a) * n + n] += 1
nbhr_count[(n - 1) + (a) * n + n - 1] += 1
a += 1
# Finally, we address all the remaining cells
# 'b' represents the width 'a' represents the height all starting from the top left corner....
a = 1
while a < n - 1:
b = 1
while b < n - 1:
if previous[n * a + b] == "*":
nbhr_count[(b) + (a) * n + 1 - n] += 1
nbhr_count[(b) + (a) * n - n] += 1
nbhr_count[(b) + (a) * n - 1 - n] += 1
nbhr_count[(b) + (a) * n + 1] += 1
nbhr_count[(b) + (a) * n - 1] += 1
nbhr_count[(b) + (a) * n + 1 + n] += 1
nbhr_count[(b) + (a) * n + n] += 1
nbhr_count[(b) + (a) * n - 1 + n] += 1
b += 1
a += 1
# now we apply the world physics laws
i = 0
while i < N:
m = nbhr_count[i]
if previous[i] == "*": # if the cell is alive
# should be m<2 or m>3 for original conway's set of rules.
if m < 2 or m > 3:
previous[i] = " "
else: # if the cell is dead
# should be m == 3 for original conway's set of ruless
if m == 3:
previous[i] = "*"
i += 1
return previous
# world/universe physics: no edge of the world but a planet globe connected + laws by Conway OR Alternatives
# we achieve this by applying modulo n to the index arithmetic accordingly (with minor adjustments for User display).
def create_next_state_planet(previous):
nbhr_count = [] # neighborhood count
for k in range(0, n):
row = [0] * n
nbhr_count.append(row)
#'b' represents "x" the width AND 'a' represents "y" the height all starting from the top left corner....
y = 0
while y < n:
x = 0
while x < n:
if previous[n * y + x] == "*": # there is nothing wrong here,,,
nbhr_count[(y + 1) % n][(x - 1) % n] += 1
nbhr_count[(y + 1) % n][x] += 1
nbhr_count[(y + 1) % n][(x + 1) % n] += 1
nbhr_count[y][(x + 1) % n] += 1
nbhr_count[y][(x - 1) % n] += 1
nbhr_count[(y - 1) % n][(x - 1) % n] += 1
nbhr_count[(y - 1) % n][x] += 1
nbhr_count[(y - 1) % n][(x + 1) % n] += 1
x += 1
y += 1
# now we apply the world physics laws
y = 0
while y < n:
x = 0
while x < n:
m = nbhr_count[y][x]
if previous[n * y + x] == "*": # if the cell is alive
# should be m < 2 or m > 3 for original conway's set of rules.
if m < 2 or m > 3:
previous[n * y + x] = " "
else: # if the cell is dead
# should be m == 3 for original conway's set of rules
if m == 3:
previous[n * y + x] = "*"
x += 1
y += 1
return previous
def create_next_state_planet_research(
previous,
lower,
lowerDecrease,
lowerDeviationChancePercent,
upper,
upperIncrease,
upperDeviationChancePercent,
):
# neighborhood count
nbhr_count = []
for k in range(0, n):
row = [0] * n
nbhr_count.append(row)
### (Fixed) this is where the indexing bug was ... ###
# 'b' represents "x" the width AND 'a' represents "y"
# , which is the height all starting from the top left corner.
y = 0
while y < n:
x = 0
while x < n:
if previous[n * y + x] == "*":
nbhr_count[(y + 1) % n][(x - 1) % n] += 1
nbhr_count[(y + 1) % n][x] += 1
nbhr_count[(y + 1) % n][(x + 1) % n] += 1
nbhr_count[y][(x + 1) % n] += 1
nbhr_count[y][(x - 1) % n] += 1
nbhr_count[(y - 1) % n][(x - 1) % n] += 1
nbhr_count[(y - 1) % n][x] += 1
nbhr_count[(y - 1) % n][(x + 1) % n] += 1
x += 1
y += 1
################################################
# now we apply the world/realm's physics/rules #
################################################
y = 0
while y < n:
x = 0
while x < n:
###########################################
### Research World Physics Customizable ###
###########################################
# lower = 2
# lowerDecrease = 1
# lowerDeviationChancePercent = 0.05
# upper = 3
# upperIncrease = random.randint(1, 8 - upper) # this allows up to maximum 8 neighbors
# upperDeviationChancePercent = int(1 / (500*(upper + upperIncrease)))
###########################################
###########################################
###########################################
lowerProbabilistic = lower
upperProbabilistic = upper
dice = random.randint(1, 100)
if dice / 100 <= lowerDeviationChancePercent:
lowerProbabilistic -= lowerDecrease
dice = random.randint(1, 100)
if dice / 100 <= upperDeviationChancePercent:
upperProbabilistic += upperIncrease
m = nbhr_count[y][x]
if previous[n * y + x] == "*": # if the cell is alive
# should be m<2 or m>3 for original conway's set of rules.
if m < lowerProbabilistic or m > upperProbabilistic:
previous[n * y + x] = " "
else: # if the cell is dead
# should be m == 3 for original conway's set of rules
if m == upperProbabilistic:
previous[n * y + x] = "*"
x += 1
y += 1
return previous
# can be used to create completely anomalous events
def veryunlikelyeventv2(previous):
unlikelyevent = []
for i in range(5):
unlikelyevent.append(random.choice([0, 1]))
if unlikelyevent == [0, 0, 0, 0, 0]: # since five, 1/32 chance,,,
previous = createlength3star(previous, 7)
return previous
def createlength3star(previous, count):
# note: star could be "buggy" with edges
for i in range(count):
# note: the same can be flipped again
r = random.randint(0, N - 1)
verticalORhorizontal = random.choice([0, 1])
# vertical star
if verticalORhorizontal == 0:
if r - n >= 0:
previous[r - n] = "*"
previous[r] = "*"
if r + n <= N - 1:
previous[r + n] = "*"
else: # horizontal star
if r - 1 >= 0:
previous[r - 1] = "*"
previous[r] = "*"
if r + 1 <= N - 1:
previous[r + 1] = "*"
return previous