-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_of_life.py
executable file
·136 lines (106 loc) · 3.66 KB
/
game_of_life.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
#!/usr/bin/python2
import colorsys
import math
import random
import time
from collections import namedtuple
from display import Display
ROWS = 10
COLS = 20
rand = random.Random()
class Color(namedtuple('Color', ['red', 'green', 'blue'])):
def to_int(self):
"""Convert the provided red, green, blue color to a 24-bit color value.
Each color component should be a value 0-255 where 0 is the lowest intensity
and 255 is the highest intensity.
"""
return (255 << 24) | (self.green << 16) | (self.red << 8) | self.blue
def __mul__(self, factor):
def d(c):
return min(255, max(0, int(factor * c)))
return Color(d(self.red), d(self.green), d(self.blue))
class Coords(namedtuple('Coords', ['row', 'col'])):
def neighbors(self):
for dr in range(-1, 2):
for dc in range(-1, 2):
if dr != 0 or dc != 0:
yield Coords(self.row + dr, self.col + dc)
class Cell(object):
def __init__(self, color=None, age=0):
if color is None:
hue = rand.uniform(0, 1)
saturation = 1
value = 1
r, g, b = colorsys.hsv_to_rgb(hue, saturation, value)
color = Color(r * 255, g * 255, b * 255)
self.color = color
self.age = age
def happy_birthday_to_me(self):
return Cell(self.color, self.age + 1)
class Grid:
def __init__(self):
self.clear()
def clear(self):
self._cells = {}
def __getitem__(self, coords):
return self._cells[coords]
def __contains__(self, coords):
return coords in self._cells
def spawn(self, coords, cell):
self._cells[coords] = cell
def count_neighbors(self, coords):
return len([c for c in coords.neighbors() if c in self])
def advance(self):
new_grid = Grid()
for row in range(ROWS):
for col in range(COLS):
coords = Coords(row, col)
num_neighbors = self.count_neighbors(coords)
if coords in self:
if 2 <= num_neighbors <= 3:
new_grid.spawn(coords, self[coords].happy_birthday_to_me())
else:
if num_neighbors == 3:
new_grid.spawn(coords, Cell())
return new_grid
def is_empty(self):
return len(self._cells) == 0
def fingerprint(self):
f = 0
for row in range(ROWS):
for col in range(COLS):
f = f << 1
if Coords(row, col) in self:
f = f | 1
return f
def randomize(self):
self.clear()
for row in range(ROWS):
for col in range(COLS):
if rand.randint(0, 1):
self.spawn(Coords(row, col), Cell())
def render_grid(grid, display, brightness):
for row in range(ROWS):
for col in range(COLS):
coords = Coords(row, col)
display.set(row, col, grid[coords].color * brightness if coords in grid else Color(0, 0, 0))
display.show()
if __name__ == '__main__':
seen_fingerprints = set()
brightness = 1
grid = Grid()
grid.randomize()
display = Display(ROWS, COLS)
display.begin()
while True:
render_grid(grid, display, math.pow(brightness, 3.0))
if grid.fingerprint() in seen_fingerprints:
brightness -= 0.05
if grid.is_empty() or brightness <= 0:
time.sleep(1.0)
grid.randomize()
seen_fingerprints = set()
brightness = 1
seen_fingerprints.add(grid.fingerprint())
grid = grid.advance()
time.sleep(0.1)