game-of-life: simpler wraparound logic in count-neighbors.
parent
0e88bd305e
commit
c5a7ce216b
|
@ -22,26 +22,23 @@ IN: game-of-life
|
||||||
bit-array boa
|
bit-array boa
|
||||||
] map! drop ;
|
] map! drop ;
|
||||||
|
|
||||||
:: wraparound ( x min max -- y )
|
|
||||||
x min < [ max ] [ x max > min x ? ] if ; inline
|
|
||||||
|
|
||||||
:: count-neighbors ( grid -- counts )
|
:: count-neighbors ( grid -- counts )
|
||||||
grid grid-dim { fixnum fixnum } declare :> ( rows cols )
|
grid grid-dim { fixnum fixnum } declare :> ( rows cols )
|
||||||
rows 1 - { fixnum } declare :> max-rows
|
rows 1 - { fixnum } declare :> max-rows
|
||||||
cols 1 - { fixnum } declare :> max-cols
|
cols 1 - { fixnum } declare :> max-cols
|
||||||
rows [ cols <byte-array> ] replicate :> neighbors
|
rows [ cols <byte-array> ] replicate :> neighbors
|
||||||
grid { array } declare [| row j |
|
grid { array } declare [| row j |
|
||||||
j 1 - 0 max-rows wraparound
|
j 0 eq? [ max-rows ] [ j 1 - ] if
|
||||||
j
|
j
|
||||||
j 1 + 0 max-rows wraparound
|
j max-rows eq? [ 0 ] [ j 1 + ] if
|
||||||
[ neighbors nth-unsafe { byte-array } declare ] tri@ :>
|
[ neighbors nth-unsafe { byte-array } declare ] tri@ :>
|
||||||
( above same below )
|
( above same below )
|
||||||
|
|
||||||
row { bit-array } declare [| cell i |
|
row { bit-array } declare [| cell i |
|
||||||
cell [
|
cell [
|
||||||
i 1 - 0 max-cols wraparound
|
i 0 eq? [ max-cols ] [ i 1 - ] if
|
||||||
i
|
i
|
||||||
i 1 + 0 max-cols wraparound
|
i max-cols eq? [ 0 ] [ i 1 + ] if
|
||||||
|
|
||||||
[ [ above [ 1 + ] change-nth-unsafe ] tri@ ]
|
[ [ above [ 1 + ] change-nth-unsafe ] tri@ ]
|
||||||
[ nip [ same [ 1 + ] change-nth-unsafe ] bi@ ]
|
[ nip [ same [ 1 + ] change-nth-unsafe ] bi@ ]
|
||||||
|
|
Loading…
Reference in New Issue