-
Notifications
You must be signed in to change notification settings - Fork 0
/
gol.py
292 lines (235 loc) · 9.07 KB
/
gol.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
from __future__ import division
import numpy as np
import time
import pygame
import argparse
import lumos
def generate_game_of_life_matrix(height, width):
# Start with an empty matrix
matrix = np.zeros((height, width), dtype=int)
# Define a glider
glider = np.array([
[0, 1, 0],
[0, 0, 1],
[1, 1, 1]
], dtype=np.float64)
# Define a Beacon
beacon = np.array([
[1, 1, 0, 0],
[1, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 1]
], dtype=np.float64)
# Define a Toad
toad = np.array([
[0, 1, 1, 1],
[1, 1, 1, 0]
], dtype=np.float64)
# Define a Lightweight spaceship
lw_spaceship = np.array([
[1, 0, 0, 1, 0],
[0, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[0, 1, 1, 1, 1]
], dtype=np.float64)
# Define the Gosper Glider Gun pattern
glider_gun = np.array([
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1],
[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1],
[1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
])
# Add the glider guns to the matrix
matrix[5:5+glider_gun.shape[0], 5:5+glider_gun.shape[1]] = glider_gun
# matrix[25:25+glider_gun.shape[0], 25:25+glider_gun.shape[1]] = glider_gun
# Place multiple gliders in the matrix
matrix[0:3, 0:3] = glider
matrix[0:3, 10:13] = glider
# Place Beacon
matrix[3:7, 20:24] = beacon
# Place Toad
matrix[3:5, 40:44] = toad
# Place Lightweight spaceship
matrix[6:10, 60:65] = lw_spaceship
return matrix
def game_of_life_matrix_to_frame(matrix, gradient):
frame = ''
for y, row in enumerate(matrix):
for x, val in enumerate(row):
if val:
r, g, b = tuple(int(c * 255) for c in gradient[y, x])
else:
r = g = b = 0
frame += '{:02x}{:02x}{:02x}'.format(r, g, b)
return frame
def generate_random_color_with_min_distance(reference_color, min_distance):
new_color = np.random.uniform(size=3)
while np.linalg.norm(new_color - reference_color) < min_distance:
new_color = np.random.uniform(size=3)
return new_color
# def update_game_of_life_matrix(matrix):
# neighbors = np.zeros(matrix.shape, dtype=int)
# for y in range(-1, 2):
# for x in range(-1, 2):
# if y != 0 or x != 0:
# neighbors += np.roll(np.roll(matrix, y, axis=0), x, axis=1)
# matrix = (neighbors == 3) | (matrix & (neighbors == 2))
# return matrix
def convolve2d(matrix, kernel):
# Matrix and kernel dimensions
m, n = matrix.shape
ky, kx = kernel.shape
# Calculate output dimensions
y = m - ky + 1
x = n - kx + 1
# Zero pad the matrix
padded_matrix = np.pad(
matrix, [(ky//2, ky//2), (kx//2, kx//2)], mode='wrap')
# Initialize output matrix
output = np.zeros((y, x))
# Convolution operation
for i in range(y):
for j in range(x):
output[i, j] = np.sum(padded_matrix[i:i+ky, j:j+kx] * kernel)
return output
def update_game_of_life_matrix(matrix):
neighbors = np.zeros(matrix.shape, dtype=int)
for y in range(-1, 2):
for x in range(-1, 2):
if y != 0 or x != 0:
neighbors += np.roll(np.roll(matrix, y, axis=0), x, axis=1)
matrix = ((neighbors == 3) | (matrix & (neighbors == 2))).astype(int)
return matrix
def show_frame_pygame(frame, width, height, scale, screen):
frame_list = [frame[i:i+6] for i in range(0, len(frame), 6)]
pixel_matrix = [frame_list[i * width:(i + 1) * width]
for i in range(height)]
for y, row in enumerate(pixel_matrix):
for x, pixel in enumerate(row):
color = tuple(int(pixel[i:i+2], 16) for i in (0, 2, 4))
pygame.draw.rect(screen, color, pygame.Rect(
x * scale, y * scale, scale, scale))
def show_frame_lumos(frame, width, height):
frame_list = [frame[i:i+6] for i in range(0, len(frame), 6)]
pixel_matrix = [frame_list[i * width:(i + 1) * width]
for i in range(height)]
# Convert RGB hex strings back to a single string
frame_hex = ''.join([pixel for row in pixel_matrix for pixel in row])
lumos.push(frame_hex)
def rgb_to_hsv(r, g, b):
max_value = float(max(r, g, b))
min_value = float(min(r, g, b))
difference = max_value - min_value
# print("Max value: ", max_value)
# print("Min value: ", min_value)
# print("Difference: ", difference)
if max_value == min_value:
h = 0
elif max_value == r:
h = (60 * ((g - b) / difference) + 360) % 360
elif max_value == g:
h = (60 * ((b - r) / difference) + 120) % 360
elif max_value == b:
h = (60 * ((r - g) / difference) + 240) % 360
# print("Hue: ", h)
if max_value == 0:
s = 0
else:
s = (difference / max_value)
# print("Saturation: ", s)
v = max_value / 255.0
# print("Value: ", v)
return h / 360.0, s, v
def hsv_to_rgb(h, s, v):
# v = max(0.1,v)
h = h * 360.0
hi = int(h / 60.0) % 6
f = h / 60.0 - hi
# print("Hue (degrees): ", h)
# print("Hue interval: ", hi)
# print("Fractional part: ", f)
p = v * (1 - s)
q = v * (1 - s * f)
t = v * (1 - s * (1 - f))
# print("P: ", p)
# print("Q: ", q)
# print("T: ", t)
if hi == 0:
r, g, b = v, t, p
elif hi == 1:
r, g, b = q, v, p
elif hi == 2:
r, g, b = p, v, t
elif hi == 3:
r, g, b = p, q, v
elif hi == 4:
r, g, b = t, p, v
elif hi == 5:
r, g, b = v, p, q
# print("RGB: ", int(r * 255), int(g * 255), int(b * 255))
return int(round(r * 255)), int(round(g * 255)), int(round(b * 255))
def main(display_method):
height, width, scale = 20, 140, 5 # Increase the grid size
display_height, display_width = 10, 120 # Define the size of the display
screen = ''
color1 = np.random.uniform(size=3)
min_distance = 0.5
color2 = generate_random_color_with_min_distance(color1, min_distance)
if display_method == 'pygame':
# Initialize pygame
pygame.init()
# Update this to use the display size
screen = pygame.display.set_mode(
(display_width * scale, display_height * scale))
pygame.display.set_caption('Game of Life')
clock = pygame.time.Clock()
flame_matrix = generate_game_of_life_matrix(height, width)
running = True
fc = 999
while running:
if display_method == 'pygame':
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
fc += 1
# Shift hue for the gradient colors
color1_hsv = rgb_to_hsv(*color1)
# print("before ", color1_hsv[0])
# print("increment ", color1_hsv[0], color1_hsv[0] +
# float(fc) / 1000.0, (color1_hsv[0] + float(fc) / 1000.0) % 1)
# print("after ", (color1_hsv[0] + float(fc) / 1000.0) % 1)
color1_hsv = ((color1_hsv[0] + 1 / 100.0) %
1, color1_hsv[1], color1_hsv[2])
color1 = np.array(hsv_to_rgb(*color1_hsv))
color2_hsv = rgb_to_hsv(*color2)
color2_hsv = ((color2_hsv[0] + float(fc) / 1000.0) %
1, color2_hsv[1], color2_hsv[2])
color2 = np.array(hsv_to_rgb(*color2_hsv))
gradient = np.array([[color1 + (color2 - color1) * (float(x + y) /
(width + height - 2)) for x in range(width)] for y in range(height)])
flame_matrix = update_game_of_life_matrix(flame_matrix)
# Update this to use only the display portion of the matrix and gradient
frame = game_of_life_matrix_to_frame(
flame_matrix[:display_height, :display_width], gradient[:display_height, :display_width])
if display_method == 'pygame':
screen.fill((0, 0, 0))
# Update this to use the display size
show_frame_pygame(frame, display_width,
display_height, scale, screen)
pygame.display.flip()
clock.tick(30) # 30 FPS
elif display_method == 'lumos':
# Update this to use the display size
show_frame_lumos(frame, display_width, display_height)
# time.sleep(1/40) # 30 FPS
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--display', choices=['pygame', 'lumos'], default='pygame',
help='Choose the display method: pygame or lumos')
args = parser.parse_args()
main(args.display)