-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconway.py
193 lines (166 loc) · 5.98 KB
/
conway.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
#!/usr/bin/env python3
# ========================================================================
# conway.py
#
# Description: Conway's game of life.
#
# Author: Jim Ing
# Date: 2024-08-24
# ========================================================================
import random
import time
import argparse
import logging
from config import sense
# Setup logging to file
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# Create handlers for file
file_handler = logging.FileHandler('conway.log')
# Set level
file_handler.setLevel(logging.INFO)
# Create a logging format
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
# Add handler to the logger
logger.addHandler(file_handler)
# Constants
WIDTH, HEIGHT = 8, 8
MAX_GENERATIONS = 1000
# Directions for checking neighbors
NEIGHBOR_DIRECTIONS = [
(-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1)
]
# Color definitions
RED = [255, 0, 0] # Dying cells
GREEN = [0, 255, 0] # Stable (alive) cells
BLUE = [0, 0, 255] # Newly born cells
BLACK = [0, 0, 0] # Dead cells
# Predefined patterns that fit within an 8x8 grid
PATTERNS = {
"block": [(3, 3), (3, 4), (4, 3), (4, 4)],
"blinker": [(3, 3), (3, 4), (3, 5)],
"toad": [(2, 4), (3, 4), (4, 4), (3, 3), (4, 3), (5, 3)],
"glider": [(1, 0), (2, 1), (0, 2), (1, 2), (2, 2)],
"beacon": [(2, 2), (2, 3), (3, 2), (3, 3), (4, 4), (4, 5), (5, 4), (5, 5)],
"pulsar": [(2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3), (4, 1), (4, 2),
(4, 3), (1, 2), (1, 4), (5, 2), (5, 4), (6, 2), (6, 4)],
"diehard": [(1, 0), (1, 1), (2, 1), (2, 2), (3, 0), (3, 2), (4, 1),
(4, 2)],
"acorn": [(1, 2), (2, 1), (2, 2), (3, 0), (3, 2), (3, 3), (4, 1), (4, 3)],
"r_pentomino": [(3, 4), (4, 3), (4, 4), (4, 5), (5, 3)],
"pentadecathlon": [(2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (3, 5), (4, 5),
(5, 5)]
}
# Initialize grid
def init_grid(pattern=None):
grid = [[0 for _ in range(WIDTH)] for _ in range(HEIGHT)]
if pattern:
for x, y in PATTERNS[pattern]:
grid[y][x] = 1
else:
grid = [[random.choice([0, 1]) for _ in range(WIDTH)]
for _ in range(HEIGHT)]
return grid
# Count neighbors
def count_neighbors(grid, x, y):
count = 0
for dx, dy in NEIGHBOR_DIRECTIONS:
nx, ny = (x + dx) % WIDTH, (y + dy) % HEIGHT
count += grid[ny][nx]
return count
# Next generation
def next_generation(grid):
new_grid = [[0] * WIDTH for _ in range(HEIGHT)]
for y in range(HEIGHT):
for x in range(WIDTH):
neighbors = count_neighbors(grid, x, y)
if grid[y][x] == 1:
if neighbors in (2, 3):
new_grid[y][x] = 1
elif neighbors == 3:
new_grid[y][x] = 1
return new_grid
# Display grid with color coding
def display_grid(grid, previous_grid):
pixels = []
for y in range(HEIGHT):
for x in range(WIDTH):
if grid[y][x] == 1: # Alive cell
if previous_grid and previous_grid[y][x] == 1:
# Stable cell (was alive last generation and still alive)
pixels.append(GREEN)
else:
# Newly born cell
pixels.append(BLUE)
elif previous_grid and previous_grid[y][x] == 1:
# Dying cell (was alive last generation, now dead)
pixels.append(RED)
else:
# Dead cell
pixels.append(BLACK)
sense.set_pixels(pixels)
# Print grid to console and log it
def print_grid(grid, previous_grid):
lines = "\n"
for y in range(HEIGHT):
line = ""
for x in range(WIDTH):
if grid[y][x] == 1: # Alive cell
if previous_grid and previous_grid[y][x] == 1:
line += "G" # Green (stable)
else:
line += "B" # Blue (newly born)
elif previous_grid and previous_grid[y][x] == 1:
line += "R" # Red (dying)
else:
line += "." # Blank for dead cells
lines += line + "\n"
logging.info(lines) # Log the grid line
# Main game loop
def game_of_life(pattern=None):
generation = 0
grid = init_grid(pattern)
previous_grids = []
previous_color_grid = None # To track the color state for each cell
logging.info("Initial grid:")
print_grid(grid, previous_color_grid)
while generation < MAX_GENERATIONS:
display_grid(grid, previous_color_grid)
time.sleep(1)
new_grid = next_generation(grid)
# Check stability
if new_grid in previous_grids:
logging.info(f"Stable pattern reached at generation {generation}:")
print_grid(grid, previous_color_grid)
break
if sum(map(sum, new_grid)) == 0:
logging.info(f"All cells dead at generation {generation}:")
print_grid(grid, previous_color_grid)
break
# Keep track of previous grids (limit to the last 10 generations)
previous_grids.append(grid)
if len(previous_grids) > 10:
previous_grids.pop(0) # Keep history of 10 generations
previous_color_grid = grid
grid = new_grid
generation += 1
if generation >= MAX_GENERATIONS:
logging.info(f"Reached {MAX_GENERATIONS} generations:")
print_grid(grid, previous_color_grid)
time.sleep(20) # Pause before resetting
sense.clear()
if __name__ == "__main__":
try:
parser = argparse.ArgumentParser(
description="Conway's Game of Life with patterns")
parser.add_argument('--pattern', type=str, choices=PATTERNS.keys(),
help='Start with a common pattern')
args = parser.parse_args()
print("To quit, press Ctrl+C")
while True:
game_of_life(pattern=args.pattern)
except KeyboardInterrupt:
sense.clear()