
Show HN: I implemented Pong as a cellular automaton - uranium
https://ericu.github.io/CellCulTuring/
======
uranium
I'd been explaining sonic booms and high explosives to my kids, and thought
that showing them a simulation of wavefront propagation might be a fun way to
illustrate how they worked. I figured I could do that with a simple cellular
automaton, so I threw something together in a few hours, but my sim didn't
have anything like the right behavior. So there I was with a general cellular
automaton framework, thinking about wave propagation, and wondering what else
I could do with it. Inspiration struck, and I just had to try doing this.

~~~
basementcat
Reminded me of a problem set I was once assigned.
[https://en.wikipedia.org/wiki/Firing_squad_synchronization_p...](https://en.wikipedia.org/wiki/Firing_squad_synchronization_problem)

~~~
uranium
Neat--I hadn't heard of that. The wave propagation makes me think back to the
problem I was working on the first place.

~~~
basementcat
The nice property of the “firing squad” is that the number states is
independent of the length of the firing squad.

------
tokyodude
That is pretty dang incredible. I love the debugging colors etc.

Maybe someone has already done this but I'm surprised someone hasn't made an
LLVM -> Life compiler just because. Might as well do LLVM -> sh while they're
at it.

------
sgentle
This is super cool! By any chance have you seen Dave Ackley's Movable Feast
Machine and related projects? He's working on some similar ideas – cellular
automata with different classes of cell that pass local state around to build
large-scale systems.

Here's "demon horde sort", which is a kind of stochastic self-healing bubble-
sorting automaton:
[https://www.youtube.com/watch?v=lbgzXndaNKk](https://www.youtube.com/watch?v=lbgzXndaNKk)

~~~
uranium
I hadn't seen that; thanks for the pointer.

------
imode
This is remarkably beautiful. Could you, perhaps, give some detail as to how
it functions? I see wave propagation, but I'm interested in the state dynamics
of a particular cell.

~~~
uranium
There's some general background at
[https://github.com/ericu/CellCulTuring/blob/master/README.md](https://github.com/ericu/CellCulTuring/blob/master/README.md)
but basically I've got a number of different general classes of cell
[background, counter, wall, ball, paddle, etc.] and each knows the kinds of
situations that will cause it to change into something else. Within each class
of cell, there's further state, so e.g. the ball's a different color when it's
moving up and to the right than when it's moving down and to the left. Each
cell is basically its own state machine, taking its state and the states of
its neighbors into account, and progressing through the substates of its class
until it turns into something else.

Say a blank background cell sees a ball cell to its left. It looks at the
precise color of the ball to determine that the ball's moving to the right, so
the next cycle, the background cell must become a ball cell with the same
motion. It's a bit more complicated than that because balls only move every
other cycle, they've got internal counters [colors] to deal with moving
diagonally, bouncing off walls, hitting the paddle, etc., but that's basically
it.

The actual state machine code takes thousands of lines of JavaScript to
express.

~~~
pvg
Is the score and game over message part of the automaton? Couldn't quite tell
from the debug pixels output.

~~~
uranium
Yes, everything inside the canvas is part of the same giant automaton, and
follows the same rules.

In fact, @imode, if you want to look at one of the smaller state machines,
look at the scoreboard code in
[https://github.com/ericu/CellCulTuring/blob/master/scoreboar...](https://github.com/ericu/CellCulTuring/blob/master/scoreboard.js).
It's a seven-segment display with some tweaks and a hack to show the game-over
message. I initialize the scoreboards by drawing the segments in hard-to-see
colors so that they're invisible when they're turned off. Each cell of the
scoreboard always knows the current score; they propagate the updates to each
other as fast as the find out about them. Each segment has an ID [a different
color field] and a lookup table that tells for which scores e.g. ID 3 should
be visible. So then when each segment is supposed to be visible, it sets a
high-order color bit to show up. That's one of the few places that I actually
have to set bits just for visibility. For the ball, wall, paddle, and such I
just chose the bits such that you could see what the cells were naturally--I
didn't have any bits to spare. For the sub-space of bits allocated to the
scoreboard, I've got plenty extra. I was considering doing a fancier font
using a 1-hot encoding instead of 7-segment, but I wanted to get this done.

~~~
taneq
Wow, that's awesome. The score display makes me think of some of the stuff
that's been done in Wireworld: [https://unnikked.ga/simulate-logic-circuits-
using-wireworld-...](https://unnikked.ga/simulate-logic-circuits-using-
wireworld-cellular-automaton-11391bc26b1c)

(There was a similar thing posted here a while back, too, that used modified
rules to make it a bit quicker and simpler, but I can't remember what it was
called...)

~~~
rzzzt
The Powder Toy includes Wireworld elements, among other electronic bits:
[https://powdertoy.co.uk/Wiki/W/Elements:Electronics.html#Wir...](https://powdertoy.co.uk/Wiki/W/Elements:Electronics.html#WireWorld_Wire)

~~~
taneq
That's really cool, I'd never encountered Powder Toy before. Thanks!

------
baddox
This is awesome. Now I am wondering if you could do effectively the same thing
in Conway’s game of life, where if you zoomed out far enough you would see the
game’s graphics.

~~~
Cyphase
You mean something like this?

If you don't want to be "spoiled" as to what exactly the video is going to
show as it zooms out, use this link.. and don't pay attention to the browser
tab title, or the video title in the top left corner, until it disappears in
about 3 seconds along with the rest of the UI:

[https://www.youtube.com/embed/xP5-iIeKXE8?autoplay=1](https://www.youtube.com/embed/xP5-iIeKXE8?autoplay=1)
[1:29]

Canonical link:

[https://www.youtube.com/watch?v=xP5-iIeKXE8](https://www.youtube.com/watch?v=xP5-iIeKXE8)
[1:29]

~~~
baddox
Yep, the OTCA metacell was exactly what I was thinking of. We know that the
game of life is Turing complete, so the only challenge is figuring out how to
make the graphics look presentable when zoomed out. Someone must have just
implemented a 2d graphics environment on top of game of life.

