Last active
July 28, 2018 16:52
-
-
Save Guevara-chan/384bb23bb040a6069e486ded529dd2ec to your computer and use it in GitHub Desktop.
Barebone wireworld simulator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#.{ [Classes] | |
class Cell | |
@[name] = idx for name, idx in ['void', 'cond', 'head', 'tail'] | |
@cycle: (type, shift) -> (type + shift) %% 4 | |
# -------------------- # | |
class Automata | |
# --Methods goes here. | |
constructor: (@width = 4, @height = 4, @wrap = false) -> | |
if @clear() and @wrap | |
@get = (x, y) -> @cells[y %% @height][x %% @width] | |
@set = (x, y, type = Cell.cond) -> @cells[y %% @height][x %% @width] = type | |
else | |
@get = (x, y) -> @cells[y]?[x] | |
@set = (x, y, type = Cell.cond) -> @cells[y]?[x] = type | |
clear: (init = Cell.void) -> | |
@cells = Array.from Array(@height), (=> new Uint8Array(@width).fill init) | |
@ticks = 0 | |
return @ | |
tick: (steps = 1) -> | |
while steps-- and ++@ticks | |
@cells = for row, y in @cells | |
accum = new Uint8Array(@width) | |
for cell, x in row | |
accum[x] = switch cell | |
when Cell.head then Cell.tail | |
when Cell.tail then Cell.cond | |
when Cell.cond # Conductivity | |
heads = 0 | |
for [tx, ty] in [[0, 1], [0, -1], [1, 0], [-1, 0], [1, 1], [1, -1], [-1, 1], [-1, -1]] | |
heads++ if @get(x + tx, y + ty) is Cell.head | |
if 1 <= heads <= 2 then Cell.head else Cell.cond | |
else cell | |
accum # (return). | |
return @ | |
#.} [Classes] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment