From 30b80c1b925816ce6afb66621df4cfb0a5765fb5 Mon Sep 17 00:00:00 2001 From: arofarn Date: Thu, 16 Apr 2020 23:27:33 +0200 Subject: [PATCH] Cleaner living neighbour counter Add presets : BLOOM and FROG --- code.py | 4 +++- lib/conway.mpy | Bin 1513 -> 1615 bytes src/conway.py | 47 ++++++++++++++++++++++++++++++----------------- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/code.py b/code.py index b7df65e..8db5e9d 100644 --- a/code.py +++ b/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 diff --git a/lib/conway.mpy b/lib/conway.mpy index e69f0ac9d889a7ab65af6269e6f47a6b58e75a7b..6645fd71b8b7a3d02d99c70c2f05380d68e55ff9 100644 GIT binary patch delta 415 zcmaFKeV&Kcmzhbv#e$y!1gs|V$_x2xK>(cg-KcBKI5AOdvOOawtB?Q?PL^g=nK++= zC6Gf!d18VRvyc!>NEIlg!kEAy{feP0g)xOuMU^puQAL9>fk{P&F@agdh%tdh#cXmh zV~#wllaIf@FN<4{zdLJDVqSVGZ@g*daGV+XR%tod>Ans&m<`@k&Zm_WuMj-tj3YltZYN~3k zDj>qe256LwDx+KvLmI>6lgyQ|s7_@`&M&BBVBjdt%PdPRDo%CdWME)sVBj?a8I|Cm xZ2)r0rVWZ4H#UEaU{XB1dDDi*k6>O?1he8(5Wfk+2g*J@JUNwBbMi4(WdPb3Z8QJ? delta 416 zcmX@l^OBp_mzhbvseqpW1T-h|$}{F~v@>QDPGC?`W=vpIQD;nGQqg5hU{*1nY|WH2 z`52QMKZ_g-0|O%i1E(^h6l0V6Q8A6t2*z&wh|Ft3;sIrlRDyWOvOqSHEQr_mQ3PQW aNC2XIGBcZ|1zTc4L26zK$<$;WHe~>z$aebx diff --git a/src/conway.py b/src/conway.py index a0fcfbb..bed9cde 100644 --- a/src/conway.py +++ b/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)