Skip to content

Instantly share code, notes, and snippets.

@t-mw
Created July 18, 2021 13:16
Show Gist options
  • Save t-mw/aa714f7f160ac92d346edbc2f0230045 to your computer and use it in GitHub Desktop.
Save t-mw/aa714f7f160ac92d346edbc2f0230045 to your computer and use it in GitHub Desktop.
// {"enableUpdate":true,"enableVisualMode":true,"enableClearOnUpdate":true,"updateFrequency":4,"gridWidth":20,"gridHeight":20,"throne-playground-script":"0.1.0"}
// Conway's Game of Life (https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life):
// 1. Any live cell with two or three live neighbours survives.
// 2. Any dead cell with three live neighbours becomes a live cell.
// 3. All other live cells die in the next generation. Similarly, all other dead cells stay dead.
// glider pattern
cell 1 0
cell 2 1
cell 0 2
cell 1 2
cell 2 2
cell 18 19
cell 17 18
cell 19 17
cell 18 17
cell 17 17
#update = #generate-neighbors
// place an 'n' marker phrase for each grid square neighbouring a live cell
#generate-neighbors . $cell X Y . - X 1 X0 . + X 1 X1 . - Y 1 Y0 . + Y 1 Y1 . !processed X Y =
processed X Y . n X0 Y0 1 . n X Y0 1 . n X1 Y0 1 . n X1 Y 1 . n X1 Y1 1 . n X Y1 1 . n X0 Y1 1 . n X0 Y 1 . #combine
#generate-neighbors . () = #apply-rules-1-2
#combine: {
// combine overlapping 'n' marker phrases
n X Y 1 . n X Y N1 . + N1 1 N2 = n X Y N2
() = #generate-neighbors
}
#apply-rules-1-2: {
// keep any cell with exactly two live neighbours (rule 1)
cell X Y . n X Y 2 = survive-cell X Y
// spawn a live cell on any grid square with exactly three live neighbours (rules 1 and 2)
n X Y 3 = survive-cell X Y
() = #apply-rule-3
}
#apply-rule-3: {
// discard any cells that did not survive (rule 3)
cell X Y = ()
() = #draw
}
#draw: {
// draw each 'survive-cell' and convert it to a 'cell' for the next iteration
survive-cell X Y = cell X Y . draw "⬛" X Y
() = #post-update
}
#post-update: {
// clean up temporary phrases
processed _ _ = ()
n _ _ _ = ()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment