factor/contrib/x11/automata.factor

106 lines
2.2 KiB
Factor
Raw Normal View History

2005-10-29 01:37:38 -04:00
! Ed Cavazos - wayo.cavazos@gmail.com
IN: automata
USING: parser kernel hashtables namespaces sequences lists math io
2005-12-02 05:47:18 -05:00
threads strings arrays prettyprint xlib x ;
2005-10-29 01:37:38 -04:00
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! set-rule
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SYMBOL: rule
2005-12-02 05:47:18 -05:00
8 <hashtable> rule set
2005-10-29 01:37:38 -04:00
SYMBOL: char-0
48 char-0 set
: rule-keys ( -- { ... } )
{ { 0 0 0 }
{ 0 0 1 }
{ 0 1 0 }
{ 0 1 1 }
{ 1 0 0 }
{ 1 0 1 }
{ 1 1 0 }
{ 1 1 1 } } ;
: rule-values ( n -- { ... } )
>bin 8 char-0 get pad-left
2005-12-02 05:47:18 -05:00
>array
2005-10-29 01:37:38 -04:00
[ 48 - ] map ;
: set-rule ( n -- )
rule-values rule-keys [ rule get set-hash ] 2each ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! step
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: 3nth ( n seq -- slice ) >r dup 3 + r> <slice> ;
: next-chunk ( << slice: a b c >> - value )
2005-12-02 05:47:18 -05:00
>array rule get hash ;
: (step) ( line -- new-line )
dup length 2 - [ swap 3nth next-chunk ] map-with ;
2005-10-29 01:37:38 -04:00
: step-line ( line -- new-line )
>r { 0 } r> { 0 } append append
(step) ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Display the rule
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! SYMBOL: win
: setup-window
":0.0" initialize-x
create-window win set
{ 400 400 } resize-window
map-window
2005-10-29 01:37:38 -04:00
flush-dpy ;
: random-line ( -- line )
0 400 <range>
2005-12-02 05:47:18 -05:00
[ drop 2 random-int ]
2005-10-29 01:37:38 -04:00
map ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! show-line
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: show-point ( { x y } p -- )
2005-12-02 05:47:18 -05:00
1 = [ draw-point ] [ drop ] if ;
2005-10-29 01:37:38 -04:00
: (show-line) ( { x y } line -- )
[ >r dup r> show-point { 1 0 } v+ ] each drop ;
: show-line ( y line -- )
2005-12-02 05:47:18 -05:00
>r >r 0 r> 2array r> (show-line) ;
2005-10-29 01:37:38 -04:00
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Go
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: run-rule
clear-window
2005-10-29 01:37:38 -04:00
0 random-line
400
[ drop
2dup show-line >r
1 +
r> step-line ] each
flush-dpy ;
: random-gallery
2005-12-02 05:47:18 -05:00
255 random-int 1 +
2005-10-29 01:37:38 -04:00
dup unparse print
set-rule
run-rule
5000 sleep
random-gallery ;