Skip to content

Commit 19c57eb

Browse files
committed
Stellar: Refined stalemate detection in Life.
1 parent 94c5d74 commit 19c57eb

File tree

1 file changed

+17
-10
lines changed
  • micropython/examples/stellar_unicorn/numpy

1 file changed

+17
-10
lines changed

micropython/examples/stellar_unicorn/numpy/life.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
# machine.freq(250_000_000)
1717

1818
INITIAL_LIFE = 128 # Number of live cells to seed
19-
GENERATION_TIME_MS = 50 # MS between generations
20-
MINIMUM_LIFE = 15 # Auto reseed when only this many alive cells remain
19+
GENERATION_TIME_MS = 100 # MS between generations
2120
SMOOTHED = True # Enable for a more organic if somewhat unsettling feel
21+
STALEMATE_DEPTH = 5 # How many generations of changes must match before reset
2222

2323
DECAY = 0.90 # Rate at which smoothing effect decays, higher number = more persistent, 1.0 = no decay
2424
TENACITY = 32 # Rate at which smoothing effect increases
@@ -27,12 +27,15 @@
2727
su.set_brightness(0.5)
2828
graphics = PicoGraphics(DISPLAY_STELLAR_UNICORN, pen_type=PEN_P8)
2929

30+
changed_cells = []
31+
32+
3033
for c in range(256):
3134
graphics.create_pen(c // 2, 0, c)
3235

3336

3437
def update():
35-
global last_gen
38+
global last_gen, changed_cells
3639

3740
if SMOOTHED:
3841
duration[:] += life * TENACITY
@@ -43,10 +46,6 @@ def update():
4346

4447
last_gen = time.ticks_ms()
4548

46-
if numpy.sum(life) < MINIMUM_LIFE:
47-
seed_life()
48-
return
49-
5049
# Rollin' rollin' rollin.
5150
_N = numpy.roll(life, -1, axis=0)
5251
_NW = numpy.roll(_N, -1, axis=1)
@@ -71,7 +70,15 @@ def update():
7170
# Any alive cells with more than three neighbours should die
7271
next_generation[:] -= (neighbours[:] > 3) * life
7372

74-
life[:] = numpy.clip(next_generation, 0, 1)
73+
next_generation[:] = numpy.clip(next_generation, 0, 1)
74+
75+
changed_cells.append(numpy.sum(life != next_generation))
76+
changed_cells = changed_cells[-STALEMATE_DEPTH:]
77+
78+
life[:] = next_generation
79+
80+
if changed_cells.count(changed_cells[0]) == STALEMATE_DEPTH:
81+
seed_life(INITIAL_LIFE // 2)
7582

7683

7784
def draw():
@@ -83,8 +90,8 @@ def draw():
8390
su.update(graphics)
8491

8592

86-
def seed_life():
87-
for _ in range(INITIAL_LIFE):
93+
def seed_life(amount=INITIAL_LIFE):
94+
for _ in range(amount):
8895
x = random.randint(0, width - 1)
8996
y = random.randint(0, height - 1)
9097
life[y][x] = int(True) # Avoid: TypeError: 'bool' object isn't iterable

0 commit comments

Comments
 (0)