REBOL [ Title: "Conway's Game of Life" Author: "Gregg Irwin" Email: greggirwin@acm.org Version: 0.0.1 Date: 30-Oct-2005 Notes: { Should add some standard patterns to load as seeds. Fix edge-wrapping } Type: 'graphic Purpose: { Show image access and manipulation, calculating pixel offsets to find neighboring pixels (though the edge- wrapping logic is a cheat at the moment). } ] config: [ img-size 96x96 ; this is our "data set" image 128x128 view-size 384x384 ; how big is the viewer ] prev-row: negate config/img-size/x next-row: config/img-size/x neighbors: reduce [ prev-row - 1 prev-row prev-row + 1 -1 1 next-row - 1 next-row next-row + 1 ] neighbor-len: length? neighbors img: make image! config/img-size alt-img: copy img len: length? img on-color: to integer! #FFFFFF ;white off-color: 0 ;black on-ct: 0 pos: 0 do-gen: rebcode [img alt-img /local pos color] [ length? len img set pos 0 repeat cell len [ repeat n neighbor-len [ pick offset neighbors n set.i pos cell add.i pos offset ; See if we're in the middle somewhere glte.i pos 1 len brat pick-it ; If neighbor is before head of image, fixup top-wrap offsets lt.i pos 1 ift [add.i pos len] brat pick-it ; If neighbor is past tail of image, fixup bottom-wrap offsets sub.i pos len label pick-it pick color img pos eq.i on-color color ift [add.i on-ct 1] ] pick color img cell eq.i off-color color set color off-color either [ eq.i on-ct 3 ift [set color on-color] ][ eq.i on-ct 2 ift [set color on-color] eq.i on-ct 3 ift [set color on-color] ] poke alt-img cell color set.i on-ct 0 ] return alt-img ] view layout [ world: image img config/view-size effect 'fit return button "Random Seed" [ random/seed now/precise repeat i divide length? img 4 [ poke img random length? img white ;on-color ] show world ] button "Go" [ forever [ world/image: do-gen img alt-img tmp: img img: alt-img alt-img: tmp show world wait 0.001 ] ] button "Stop" [unview halt] ]