count-neighbors ( grid -- counts )


Vocabulary
game-of-life

Inputs
gridan object


Outputs
countsan object


Definition


:: count-neighbors ( grid -- counts )
grid grid-dim { fixnum fixnum } declare
:> ( rows cols ) rows [ cols <byte-array> ] replicate
:> neighbors grid { array } declare [| row j |
j rows adjacent-indices
[ neighbors nth-unsafe { byte-array } declare ] tri@
:> ( above same below ) row { bit-array } declare [|
cell i |
cell [
i cols adjacent-indices
[ [ above ~quotation~ change-nth-unsafe ] tri@ ]
[
nip
[ same ~quotation~ change-nth-unsafe ] bi@
]
[ [ below ~quotation~ change-nth-unsafe ] tri@ ]
3tri
] when
] each-index
] each-index neighbors ;