113 lines
1.9 KiB
Text
113 lines
1.9 KiB
Text
'
|
|
' One Dimensional Cellular Automaton
|
|
'
|
|
start$="01110110101010100100"
|
|
max_cycles%=20 ! give a maximum depth
|
|
'
|
|
' Global variables hold the world, with two rows
|
|
' world! is set up with 2 extra cells width, so there is a FALSE on either side
|
|
' cur% gives the row for current world,
|
|
' new% gives the row for the next world.
|
|
'
|
|
size%=LEN(start$)
|
|
DIM world!(size%+2,2)
|
|
cur%=0
|
|
new%=1
|
|
clock%=0
|
|
'
|
|
@setup_world(start$)
|
|
OPENW 1
|
|
CLEARW 1
|
|
DO
|
|
@display_world
|
|
@update_world
|
|
EXIT IF @same_state
|
|
clock%=clock%+1
|
|
EXIT IF clock%>max_cycles% ! safety net
|
|
LOOP
|
|
~INP(2)
|
|
CLOSEW 1
|
|
'
|
|
' parse given string to set up initial states in world
|
|
' -- assumes world! is of correct size
|
|
'
|
|
PROCEDURE setup_world(defn$)
|
|
LOCAL i%
|
|
' clear out the array
|
|
ARRAYFILL world!(),FALSE
|
|
' for each 1 in string, set cell to true
|
|
FOR i%=1 TO LEN(defn$)
|
|
IF MID$(defn$,i%,1)="1"
|
|
world!(i%,0)=TRUE
|
|
ENDIF
|
|
NEXT i%
|
|
' set references to cur and new
|
|
cur%=0
|
|
new%=1
|
|
RETURN
|
|
'
|
|
' Display the world
|
|
'
|
|
PROCEDURE display_world
|
|
LOCAL i%
|
|
FOR i%=1 TO size%
|
|
IF world!(i%,cur%)
|
|
PRINT "#";
|
|
ELSE
|
|
PRINT ".";
|
|
ENDIF
|
|
NEXT i%
|
|
PRINT ""
|
|
RETURN
|
|
'
|
|
' Create new version of world
|
|
'
|
|
PROCEDURE update_world
|
|
LOCAL i%
|
|
FOR i%=1 TO size%
|
|
world!(i%,new%)=@new_state(@get_value(i%))
|
|
NEXT i%
|
|
' reverse cur/new
|
|
cur%=1-cur%
|
|
new%=1-new%
|
|
RETURN
|
|
'
|
|
' Test if cur/new states are the same
|
|
'
|
|
FUNCTION same_state
|
|
LOCAL i%
|
|
FOR i%=1 TO size%
|
|
IF world!(i%,cur%)<>world!(i%,new%)
|
|
RETURN FALSE
|
|
ENDIF
|
|
NEXT i%
|
|
RETURN TRUE
|
|
ENDFUNC
|
|
'
|
|
' Return new state of cell given value
|
|
'
|
|
FUNCTION new_state(value%)
|
|
SELECT value%
|
|
CASE 0,1,2,4,7
|
|
RETURN FALSE
|
|
CASE 3,5,6
|
|
RETURN TRUE
|
|
ENDSELECT
|
|
ENDFUNC
|
|
'
|
|
' Compute value for cell + neighbours
|
|
'
|
|
FUNCTION get_value(cell%)
|
|
LOCAL result%
|
|
result%=0
|
|
IF world!(cell%-1,cur%)
|
|
result%=result%+4
|
|
ENDIF
|
|
IF world!(cell%,cur%)
|
|
result%=result%+2
|
|
ENDIF
|
|
IF world!(cell%+1,cur%)
|
|
result%=result%+1
|
|
ENDIF
|
|
RETURN result%
|
|
ENDFUNC
|