Not logged in

Artifact 5747b4bb74e37d16c0e6f432f9a303dfa4f708f3

File 1d-cellular-automota.rx part of check-in [8bb2d1c8d7] - 1d cellular automota example by charleschilders on 2011-04-02 23:37:55. [annotate]


: $, dup getLength 0 swap [ 2over + @ , 1+ ] times 2drop 0 , ;

create world   ".###.##.#.#.#.#..#.." $,
create world2 21 allot
create group   4 allot
variable neighbors

: middle ( - )
  0 !neighbors
  group     @ '# = [ neighbors ++ ] ifTrue
  group 2 + @ '# = [ neighbors ++ ] ifTrue ;

: left.edge  (  -  ) @world '# = [ 1 ] [ 0 ] if !neighbors ;
: right.edge (  -  ) world 19 + @ '# = [ 1 ] [ 0 ] if !neighbors ;
: flip       ( n-n ) dup world + @ '# = [ '. ] [ '# ] if ;

: check ( n-n )
  [  0 = ] [ left.edge  ] when
  [ 19 = ] [ right.edge ] when
  dup world + 1- group 3 copy middle ;

: gen ( - )
  0 20 [
    0 !neighbors
    check
    @neighbors 0 = [ dup world2 + '. swap ! ] ifTrue
    @neighbors 1 = [ dup world + @ over world2 + ! ] ifTrue
    @neighbors 2 = [ flip over world2 + ! ] ifTrue
    1+
  ] times drop
  world2 world 20 copy ;

: generations ( n- )
  cr 0 swap [ dup putn space world puts cr gen 1+ ] times drop ;


10 generations