Last active
February 11, 2022 17:01
-
-
Save gurzgri/c98a54cd5367862d079c10656f30d678 to your computer and use it in GitHub Desktop.
Conway's Game of Life
This file contains hidden or 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
Red [ | |
title: "Conway's Game of Life" | |
author: "Gurzgri" | |
needs: 'view | |
] | |
life: leaf + 0.0.0.0 | |
dead: linen + 0.0.0.0 | |
init: func [size [pair!] density [percent!]] [ | |
grid: make image! reduce [size dead] | |
loop size/x * size/y * density [poke grid random size life] grid | |
] | |
evolve: func [gen [image!]] [ | |
size: gen/size | |
new: copy gen | |
repeat y size/y - 2 [repeat x size/x - 2 [ | |
cell: pick gen home: as-pair x + 1 y + 1 | |
other: sort trim reduce [ | |
pick gen home + -1x-1 pick gen home + 0x-1 pick gen home + 1x-1 | |
pick gen home + -1x0 pick gen home + 1x0 | |
pick gen home + -1x1 pick gen home + 0x1 pick gen home + 1x1 | |
] | |
alive: attempt [index? find/last other life] | |
poke new home get pick [dead life] none? find either life = cell [[2 3]] [[3]] alive | |
]] | |
head new | |
] | |
view [ | |
across | |
world: base 800x800 on-time [ | |
world/draw/2: evolve world/draw/2 | |
] react [ | |
face/draw: compose [image (init min 48x48 max 3x3 48x48 * slider/data density/data) 0x0 800x800] | |
face/rate: 1 + to integer! speed/data * 10 | |
] return | |
text "World size:" 92 slider: slider 700 66% return | |
text "Population:" 92 density: slider 700 33% return | |
text "Speed: " 92 speed: slider 700 10% | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment