Cleaner living neighbour counter

Add presets : BLOOM and FROG
This commit is contained in:
arofarn 2020-04-16 23:27:33 +02:00
parent 0f1023dff8
commit 30b80c1b92
3 changed files with 33 additions and 18 deletions

View File

@ -9,7 +9,9 @@ import adafruit_adxl34x
import conway # Some functions are in an .mpy module for performance import conway # Some functions are in an .mpy module for performance
# Starting point: # Starting point:
universe = conway.RANDOM_UNIVERSE(30) # * VOID, GLIDER, BEATING_HEART, BLOOM, FROG
# * RANDOM_UNIVERSE(percentage)
universe = conway.GLIDER
# Create the i2c object for the trellis # Create the i2c object for the trellis

Binary file not shown.

View File

@ -9,13 +9,13 @@ ON = (80, 80, 50)
# Some presets : # Some presets :
VOID = [[False, False, False, False, False, False, False, False], 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], [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], GLIDER = [[False, False, False, False, False, False, False, False],
[False, False, False, False, True, False, False, False], [False, False, False, False, True, False, False, False],
@ -35,6 +35,25 @@ BEATING_HEART = [[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]]
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): def RANDOM_UNIVERSE(rate):
return [[randrange(0,100) < rate for i in range(8) ] for j in range(8)] return [[randrange(0,100) < rate for i in range(8) ] for j in range(8)]
@ -52,7 +71,7 @@ def draw_universe(trellis, universe):
def extend_universe(universe): def extend_universe(universe):
"""Extend an 8x8 universe to 10x10 with first and last columns and rows as in an donut-shaped universe""" """Extend an 8x8 universe to 10x10 with first and last columns and rows as in an donut-shaped universe"""
ext_universe = [[False for x_ext in range(10)] for y_ext in range(10)] ext_universe = _EXT_UNIVERSE.copy()
# Copy universe in an extended donut-shaped universe # Copy universe in an extended donut-shaped universe
for x_ext in range(10): for x_ext in range(10):
@ -77,15 +96,9 @@ def extend_universe(universe):
def living_neighbour_count(xcoord, ycoord, ext_universe): def living_neighbour_count(xcoord, ycoord, ext_universe):
"""Count living cells around xcoord, ycoord""" """Count living cells around xcoord, ycoord"""
living_neighbours = [] living_neighbours = ext_universe[xcoord][ycoord:ycoord+3]
living_neighbours.append(ext_universe[xcoord][ycoord]) living_neighbours += ext_universe[xcoord+1][ycoord:ycoord+3:2]
living_neighbours.append(ext_universe[xcoord][ycoord+1]) living_neighbours += ext_universe[xcoord+2][ycoord:ycoord+3]
living_neighbours.append(ext_universe[xcoord][ycoord+2])
living_neighbours.append(ext_universe[xcoord+1][ycoord])
living_neighbours.append(ext_universe[xcoord+1][ycoord+2])
living_neighbours.append(ext_universe[xcoord+2][ycoord])
living_neighbours.append(ext_universe[xcoord+2][ycoord+1])
living_neighbours.append(ext_universe[xcoord+2][ycoord+2])
return living_neighbours.count(True) return living_neighbours.count(True)