Skip to content

Instantly share code, notes, and snippets.

@iArnold
Forked from Mufferaw/gameoflife2.red
Created September 30, 2016 07:03
Show Gist options
  • Save iArnold/6f14b818230cfca62ed39f2c93300d35 to your computer and use it in GitHub Desktop.
Save iArnold/6f14b818230cfca62ed39f2c93300d35 to your computer and use it in GitHub Desktop.
Red [
Title: "Conway's Game of Life"
Needs: 'View
]
grid: collect [repeat i 50 [keep/only collect [repeat j 50 [keep random true]]]]
scratchgrid: collect [repeat i 50 [keep/only collect [repeat j 50 [keep false]]]]
a: copy grid/1
b: copy grid/50
c: collect [keep/only b keep grid keep/only a]
count-neighbors: function [gridd x y][
count: 0
foreach [h v][
(x - 1) (y - 1)
(x - 1) y
(x - 1) (y + 1)
x (y - 1)
x (y + 1)
(x + 1) y
(x + 1) (y - 1)
(x + 1) (y + 1)
][
if gridd/(do h)/(do v) [count: count + 1]
]
;?? count
count
]
iterate: function [] [
repeat i 50 [
ii: i + 1
repeat j 50 [
ncount: count-neighbors c ii j
scratchgrid/:i/:j: make logic! any [
all [c/:ii/:j ncount > 1 ncount < 4]
all [not c/:ii/:j ncount = 3]
]
]
]
]
refresh: function [cmds][
clear cmds
repeat i 50 [
repeat j 50 [
insert cell: tail cmds [fill-pen <color> circle <coord> 5]
cell/2: pick [red white] scratchgrid/:i/:j
cell/4: 10 * as-pair i j
]
]
]
sync-grids: [
grid: copy scratchgrid
clear scratchgrid
scratchgrid: collect [repeat i 50 [keep/only collect [repeat j 50 [keep false]]]]
a: copy grid/1
b: copy grid/50
c: collect [keep/only b keep grid keep/only a]
]
update-all: does [
do iterate
refresh canvas/draw
do sync-grids
]
cmds: make block! 500
view [
title "Conway's Game of Life"
size 530x530
canvas: base 510x510 draw cmds rate 30
on-time [update-all]
do [update-all]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment