' "Automaton"
' The "automaton" is a very simple digital machine,
' extensively described in the december 1986 edition of the Byte
' magazine. It consists of a one-dimensional array of elements
' that can each have 1 out of 4 statusses.
' By assigning a color to each of the statusses, the array can
' be put on the screen as a multicolored line.
' For the next step, the status of each element is recalculated
' as the sum of his former status and those of its neighbours (points).
' This sum can be anything between 0 and 9. that sum is reduced
' to a value between 0 and 3 by a "rule". an example of such rule:
' sum: 0 1 2 3 4 5 6 7 8 9
' rule: 0 2 3 0 1 1 0 2 2 1
' the new line is drawn, and the proces is repeated a number
' of times to produce a pattern.
' Dependent on the starting values of the points and the rule
' that is applied, interesting patterns may emerge.
' In general, all patterns converge to a stable pattern,
' some quickly, others only after a considerable number of steps.
'
' The screen has a display window in which the pattern enrolls,
' an edit window containing the current rule, a listbox
' containing all saved rules that produce an interesting pattern,
' and some buttons.
' Tapping the display square while rendering will stop the rendering.
' In most cases, a part of the rendering will give enough idea whether
' the rule is an interesting one.
'
' Buttons:
' Tapping the "new random rule" button places a new rule in the edit
' window and starts rendering that rule.
' The "new start" button will use the same rule in the edit window
' for rendering, but generates a new random starting line.
' The "partial start" button uses a small starting line in the middle
' of the rendering square.
' "thumb nail size" is a toggle switch. Thumb nail size is handy to
' have a quick look at the different random rule patterns.
' "continue" button continues the rendering with the same rule and
' using the last generated line as starting line.
'
' Rules:
' In the edit window the rule may be modified. Spaces are inserted
' for readability, but they need not be retained. Just take care,
' that 10 numbers remain in the window after editing.
' An rule in the edit window can be added to the rules table with
' the "Add" button.
' The rules table can be displayed in a scrollable listbox. you may
' select a rule by touching the little number button aside of it.
' After selection the listbox must be closed.
' a selected rule may be deleted from the table using the "del"
' button, or be placed in the edit window using the "load" button.
' The rules in the listbox are kept in a file, which is loaded and
' updated automatically.
'
' ATTENTION
' In the present version of Smart Basic it is not possible to test
' for the existance of a file. For this reason, a data file is
' appended at the end of the program. That file must be moved
' (using cut and paste) to the same directory where the program
' is saved under the filename "auto_rules". that file contains
' interesting rules.
' The program will not run otherwise.
'
'
option base 1
dim p1(201),p2(201),r(10),rr(10),rules$(100)
gosub init_prog ! goto starting
loop1:
for i=1 to 10 ! rr(i)=r(i) ! next i ' if user makes a mess of it
if button_pressed("random") then
ran_rule(r,t$)
for i=1 to 200 ! p1(i)=rnd(4) ! next i
goto starting
end if
if button_pressed("begin") then
for i=1 to 200 ! p1(i)=rnd(4) ! next i
goto starting
end if
if button_pressed("part") then
for i=1 to 200
if i>90 and i<110 then p1(i)=rnd(4) else p1(i)=0
next i
goto starting
end if
if button_pressed("thumb") then
if mat=200 then
mat=50
button "thumb" title "full size" at 80,820 size 240,50
fill color .8,.8,.8
fill rect 80,20 to 684,624
else
mat=200
button "thumb" title "thumb nail size" at 80,820 size 240,50
end if
goto starting
end if
if button_pressed("sele") then
button "sele" delete
selected=sel_box("Rules",rules$,nrules,5,400,760,280)
button "sele" title "open the rules table" at 400,760 size 280,50
end if
if button_pressed("load") and selected then
t$=" "
for i=1 to 10 ! t$=t$ & " " & substr$(rules$(selected),i,i) ! next i
field "rule" text t$ at 400,640 size 280,50
aton(t$,r)
goto starting
end if
if button_pressed("dele") and selected then
if selected<nrules then
for i=selected to nrules-1 ! rul$(i)=rules$(i+1) ! next i
end if
nrules=nrules-1
save_table(rules$,nrules)
end if
if button_pressed("save") and nrules<100 then
t$=field_text$("rule") ! tcon$=""
for i=1 to len(t$)
c$=substr$(t$,i,i) ! if c$<>" " then tcon$=tcon$ & c$
next i
nrules=nrules+1 ! rules$(nrules)=tcon$ !
save_table(rules$,nrules)
end if
if field_changed("rule") then
t$=field_text$("rule")
if aton(t$,r)<>10 then
for i=1 to 10 ! r(i)=rr(i) ! next i
end if
goto starting
end if
if button_pressed("cont") then starting
goto loop1
starting:
y=20 ! disp(p1,y,mat)
for k=1 to mat/2
p2(1)=+p1(1)+p1(2) ! p2(1)=r(p2(1)+1)
for i=2 to mat
p2(i)=p1(i-1)+p1(i)+p1(i+1) ! p2(i)=r(p2(i)+1)
next i
y=y+3 ! disp(p2,y,mat)
p1(1)=p2(1)+p2(2) ! p1(1)=r(p1(1)+1)
for i=2 to mat
p1(i)=p2(i-1)+p2(i)+p2(i+1) ! p1(i)=r(p1(i)+1)
next i
y=y+3 ! disp(p1,y,mat)
xt=touch_x(0)
if xt>=0 then
yt=touch_y(0)
if xt>80 and xt<680 and yt>20 and yt<620 then loop1
end if
next k
goto loop1
end
def disp(p(),y,mat)
graphics lock
xp=80
for i=1 to mat
x=p(i)
if x=0 then
fill color 0,0,0
else
if x=1 then
fill color 1,0,0
else
if x=2 then
fill color 0,1,0
else
fill color 0,0,1
end if
end if
end if
xp=xp+3
fill rect xp,y to xp+3,y+3
next i
graphics unlock
end def
def ntoa$(r())
t$=" "
for i=1 to 10 ! t$=t$ & " " & r(i) ! next i
ntoa$=t$
end def
def aton(t$,r())
k=0
for i=1 to len(t$)
c$=substr$(t$,i,i)
if c$<>" " then
k=k+1 ! r(k)=c$
end if
next i
aton=k
end def
def ran_rule(r(),t$)
for i=1 to 10 ! r(i)=rnd(4) ! next i
tt$=ntoa$(r)
field "rule" text tt$ at 400,640 size 280,50
end def
def sel_box (titel$,name$(),max,nr,xtop,ytop,breed)
dh=25 ! selected=0
if breed=0 then
for i=1 to max
l=len(name$(i)) ! if breed<l then breed=l
next i
breed=12*breed+104
end if
xbot=xtop+breed ! ybot=ytop+(nr+1)*dh+40
w_open (titel$,xtop,ytop,xbot,ybot)
button "ok" title "klaar?" at xbot-130,ybot-35 size 120,30
xs=xtop+50 ! ys=ytop ! top=1
goto sel3
sel1:
graphics lock
fill rect xs,ys+30 to xs+breed-55,ybot-5
for j=top to bot
k=j-top+1
b$="s" & j
button b$ title "" & j at xbot-50,ys+25*k size 40,20
if j=selected then draw color .8,0,0 else draw color 0,0,0
draw text name$(j) at xs,ys+25*k
next j
graphics unlock
sel2:
for j=top to bot
b$="s" & j
if button_pressed(b$) then
if selected=j then flag=1 else flag=0
sel_oud=selected ! selected=j
draw color .8,0,0
draw text name$(j) at xs,ys+25*(j-top+1)
draw color 0,0,0
if sel_oud>0 then
k=sel_oud-top+1
if k>0 and k<=nr then draw text name$(sel_oud) at xs,ys+25*k
end if
end if
next j
if button_pressed("down")=1 then
bot=bot+nr
if bot>=max then
bot=max ! button "down" delete
end if
top=bot-nr+1
if top<=1 then
top=1 ! button "up" delete
else
button "up" title "^" at xs-40,ys+40 size 30,60
end if
goto sel1
end if
if button_pressed("up")=1 then
top=top-nr
if top<=1 then
top=1 ! button "up" delete
end if
sel3:
bot=top+nr-1
if bot>=max then
bot=max ! button "down" delete
else
button "down" title "v" at xs-40,ybot-90 size 30,60
end if
goto sel1
end if
if button_pressed("ok")=1 then
button "ok" delete ! button "up" delete ! button "down" delete
for j=1 to max
b$="s" & j ! button b$ delete
next j
fill rect xtop-2,ytop-2 to xbot+2,ybot+2
if flag=1 then selected=0
sel_box=selected
return
endif
goto sel2
end def
def w_open (title$,xtop,ytop,xbot,ybot)
r=10 ! draw color 0,0,0 ! fill color .8,.8,.8 ! draw size 4
draw circle xtop,ytop to xtop+20,ytop+20
draw circle xbot-20,ytop to xbot,ytop+20
draw circle xtop,ybot-20 to xtop+20,ybot
draw circle xbot-20,ybot-20 to xbot,ybot
draw line xtop+r,ytop to xbot-r,ytop
draw line xtop+r,ybot to xbot-r,ybot
draw line xtop,ytop+r to xtop,ybot-r
draw line xbot,ytop+r to xbot,ybot-r
fill rect xtop+r,ytop+2 to xbot-r,ybot-2
fill rect xtop+2,ytop+r to xbot-2,ybot-r
if title$<>"" then
l=(xbot-xtop-12*len(title$))/2
draw line xtop,ytop+24 to xbot,ytop+24
draw color 0,0,1
draw text title$ at xtop+l,ytop-2
draw color 0,0,0
end if
end def
def load_table(rul$())
file "auto_rules" input nrules
if nrules>0 then
for i=1 to nrules ! file "auto_rules" input rul$(i) ! next i
end if
load_table=nrules
end def
def save_table(rul$(),nrules)
file "auto_rules" delete
file "auto_rules" print nrules
if nrules then
for i=1 to nrules
rr$="""" & rul$(i) & """"
file "auto_rules" print rr$
next i
end if
end def
init_prog:
randomize
graphics
graphics clear .8,.8,.8
draw color 0,0,0
ran_rule(r,t$)
mat=200 ! nrules=0
nrules=load_table(rules$)
for i=1 to 200 ! p1(i)=rnd(4) ! next i
button "random" title "new random rule ==>>" at 80,640 size 240,50
button "begin" title "new start" at 80,700 size 240,50
button "part" title "partial start" at 80,760 size 240,50
button "thumb" title "thumb nail size" at 80,820 size 240,50
button "cont" title "continue" at 80,880 size 240,50
button "load" title "use ^" at 400,700 size 80,50
button "dele" title "del x" at 500,700 size 80,50
button "save" title "add v" at 600,700 size 80,50
button "sele" title "open the rules table" at 400,760 size 280,50
return
def test (in$,in)
fill rect 30,900 to 400,940
a$="test " & in$ & in
draw text a$ at 30,900
button "cont" title "doorgaan ?" at 440,890
loopje:
if button_pressed("cont")=0 then goto loopje
button "cont" delete
fill rect 30,900 to 400,940
end def
'
'
'Hereafter the datafile that has to be moved into the same directory as 'the program with the name "auto_rules".
30
"0212011010"
"2200201010"
"0132232122"
"2111213302"
"0232231201"
"1200130123"
"1023030223"
"0133000101"
"1310122303"
"1031202132"
"1202313310"
"3232102033"
"0202213130"
"1323122301"
"2312201001"
"2112201010"
"2320302012"
"0011320122"
"1113010320"
"1122010103"
"0012030213"
"3220113310"
"3220113310"
"0033013023"
"1323323121"
"0230001303"
"2100010003"
"0103302300"
"0103202300"
"3323100320"