Skip to content

Commit 5a72134

Browse files
committed
game-of-life: some tweaks
1 parent 80e27f2 commit 5a72134

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

extra/game-of-life/game-of-life.factor

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
! See https://factorcode.org/license.txt for BSD license
33

44
USING: accessors arrays assocs bit-arrays byte-arrays calendar
5-
colors combinators kernel kernel.private math
6-
math.order ranges namespaces opengl random sequences
5+
colors combinators io kernel kernel.private make math math.order
6+
math.private namespaces opengl random sequences
77
sequences.private timers ui ui.commands ui.gadgets
8-
ui.gadgets.toolbar ui.gadgets.tracks ui.gestures ui.render words ;
8+
ui.gadgets.toolbar ui.gadgets.tracks ui.gestures ui.render words
9+
;
910
1011
IN: game-of-life
1112
@@ -21,27 +22,28 @@ IN: game-of-life
2122
bit-array boa
2223
] map! drop ;
2324

25+
: grid. ( grid -- )
26+
[ [ CHAR: # CHAR: . ? ] "" map-as ] map write-lines ;
27+
28+
:: adjacent-indices ( n max -- n-1 n n+1 )
29+
n [ max ] when-zero 1 fixnum-fast
30+
n
31+
n 1 fixnum+fast dup max = [ drop 0 ] when ; inline
32+
2433
:: count-neighbors ( grid -- counts )
2534
grid grid-dim { fixnum fixnum } declare :> ( rows cols )
26-
rows 1 - { fixnum } declare :> max-rows
27-
cols 1 - { fixnum } declare :> max-cols
2835
rows [ cols <byte-array> ] replicate :> neighbors
2936
grid { array } declare [| row j |
30-
j 0 eq? [ max-rows ] [ j 1 - ] if
31-
j
32-
j max-rows eq? [ 0 ] [ j 1 + ] if
37+
j rows adjacent-indices
3338
[ neighbors nth-unsafe { byte-array } declare ] tri@ :>
3439
( above same below )
3540

3641
row { bit-array } declare [| cell i |
3742
cell [
38-
i 0 eq? [ max-cols ] [ i 1 - ] if
39-
i
40-
i max-cols eq? [ 0 ] [ i 1 + ] if
41-
42-
[ [ above [ 1 + ] change-nth-unsafe ] tri@ ]
43-
[ nip [ same [ 1 + ] change-nth-unsafe ] bi@ ]
44-
[ [ below [ 1 + ] change-nth-unsafe ] tri@ ]
43+
i cols adjacent-indices
44+
[ [ above [ 1 fixnum+fast ] change-nth-unsafe ] tri@ ]
45+
[ nip [ same [ 1 fixnum+fast ] change-nth-unsafe ] bi@ ]
46+
[ [ below [ 1 fixnum+fast ] change-nth-unsafe ] tri@ ]
4547
3tri
4648
] when
4749
] each-index
@@ -74,23 +76,25 @@ M: grid-gadget ungraft*
7476
[ timer>> stop-timer ] [ call-next-method ] bi ;
7577

7678
M: grid-gadget pref-dim*
77-
[ grid>> grid-dim swap ] [ size>> '[ _ * ] bi@ 1 + 2array ] bi ;
79+
[ grid>> grid-dim swap ] [ size>> '[ _ * ] bi@ 2array ] bi ;
80+
81+
M: grid-gadget gadget-text*
82+
grid>> [ CHAR: \n , ] [ [ CHAR: # CHAR: . ? ] "" map-as % ] interleave ;
7883

7984
:: update-grid ( gadget -- )
8085
gadget dim>> first2 :> ( w h )
8186
gadget size>> :> size
8287
h w [ size /i ] bi@ :> ( new-rows new-cols )
8388
gadget grid>> :> grid
8489
grid grid-dim :> ( rows cols )
85-
rows new-rows = not
86-
cols new-cols = not or [
90+
rows new-rows = not cols new-cols = not or [
8791
new-rows new-cols make-grid :> new-grid
88-
rows new-rows min <iota> [| j |
89-
cols new-cols min <iota> [| i |
92+
rows new-rows min [| j |
93+
cols new-cols min [| i |
9094
i j grid nth-unsafe nth-unsafe
9195
i j new-grid nth-unsafe set-nth-unsafe
92-
] each
93-
] each
96+
] each-integer
97+
] each-integer
9498
new-grid gadget grid<<
9599
] when ;
96100

@@ -111,14 +115,14 @@ M: grid-gadget pref-dim*
111115
gadget grid>> grid-dim :> ( rows cols )
112116
COLOR: gray gl-color
113117
cols rows [ size * ] bi@ :> ( w h )
114-
rows [0..b] [| j |
118+
rows 1 + [| j |
115119
j size * :> y
116120
{ 0 y } { w y } gl-line
117-
] each
118-
cols [0..b] [| i |
121+
] each-integer
122+
cols 1 + [| i |
119123
i size * :> x
120124
{ x 0 } { x h } gl-line
121-
] each ;
125+
] each-integer ;
122126

123127
M: grid-gadget draw-gadget*
124128
[ update-grid ] [ draw-cells ] [ draw-lines ] tri ;
@@ -143,7 +147,8 @@ SYMBOL: last-click
143147
j 0 rows 1 - between? and [
144148
last-click get i j
145149
gadget grid>> nth-unsafe set-nth-unsafe
146-
] when gadget relayout-1 ;
150+
gadget relayout-1
151+
] when ;
147152

148153
: on-scroll ( gadget -- )
149154
[
@@ -174,7 +179,7 @@ SYMBOL: last-click
174179
:: com-glider ( gadget -- )
175180
gadget grid>> :> grid
176181
{ { 2 1 } { 3 2 } { 1 3 } { 2 3 } { 3 3 } }
177-
[ first2 grid nth t -rot set-nth ] each
182+
[ grid nth t -rot set-nth ] assoc-each
178183
gadget relayout-1 ;
179184

180185
grid-gadget "toolbar" f {

0 commit comments

Comments
 (0)