Browse Source

Cleaner living neighbour counter

Add presets : BLOOM and FROG
master
arofarn 2 years ago
parent
commit
30b80c1b92
  1. 4
      code.py
  2. BIN
      lib/conway.mpy
  3. 47
      src/conway.py

4
code.py

@ -9,7 +9,9 @@ import adafruit_adxl34x
import conway # Some functions are in an .mpy module for performance
# 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

BIN
lib/conway.mpy

47
src/conway.py

@ -9,13 +9,13 @@ 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]]
[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],
@ -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]]
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)]
@ -52,7 +71,7 @@ def draw_universe(trellis, universe):
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 = [[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
for x_ext in range(10):
@ -77,15 +96,9 @@ def extend_universe(universe):
def living_neighbour_count(xcoord, ycoord, ext_universe):
"""Count living cells around xcoord, ycoord"""
living_neighbours = []
living_neighbours.append(ext_universe[xcoord][ycoord])
living_neighbours.append(ext_universe[xcoord][ycoord+1])
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])
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)
Loading…
Cancel
Save