conways-game-of-life-on-neo.../src/conway.py
arofarn 30b80c1b92 Cleaner living neighbour counter
Add presets : BLOOM and FROG
2020-04-16 23:27:33 +02:00

105 lines
4.3 KiB
Python

from random import randrange
# Acceleration threshold to end the genesis and start
# evolving
ACCEL_THRESHOLD = 100
# Colors:
OFF = (0, 0, 0)
ON = (80, 80, 50)
# Some presets :
VOID = [[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False]]
GLIDER = [[False, False, False, False, False, False, False, False],
[False, False, False, False, True, False, False, False],
[False, False, True, False, True, False, False, False],
[False, False, False, True, True, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False]]
BEATING_HEART = [[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, True, True, True, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, True, True, True, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False]]
BLOOM = [[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, True, True, True, False, False, False],
[False, True, False, True, False, True, False, False],
[False, False, True, True, True, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False]]
FROG = [[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, True, True, True, False, False],
[False, False, True, True, True, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False]]
_EXT_UNIVERSE = [[False for x_ext in range(10)] for y_ext in range(10)]
def RANDOM_UNIVERSE(rate):
return [[randrange(0,100) < rate for i in range(8) ] for j in range(8)]
def draw_universe(trellis, universe):
for x in range(8):
for y in range(8):
#activate rising edge events on all keys
if universe[x][y]:
trellis.color(x, y, ON)
else:
trellis.color(x, y, OFF)
def extend_universe(universe):
"""Extend an 8x8 universe to 10x10 with first and last columns and rows as in an donut-shaped universe"""
ext_universe = _EXT_UNIVERSE.copy()
# Copy universe in an extended donut-shaped universe
for x_ext in range(10):
for y_ext in range(10):
if x_ext == 0:
loc_x = 7
elif x_ext == 9:
loc_x = 0
else:
loc_x = x_ext-1
if y_ext == 0:
loc_y = 7
elif y_ext == 9:
loc_y = 0
else:
loc_y = y_ext-1
ext_universe[x_ext][y_ext] = universe[loc_x][loc_y]
return ext_universe
def living_neighbour_count(xcoord, ycoord, ext_universe):
"""Count living cells around xcoord, ycoord"""
living_neighbours = ext_universe[xcoord][ycoord:ycoord+3]
living_neighbours += ext_universe[xcoord+1][ycoord:ycoord+3:2]
living_neighbours += ext_universe[xcoord+2][ycoord:ycoord+3]
return living_neighbours.count(True)