94 lines
1.7 KiB
Plaintext
94 lines
1.7 KiB
Plaintext
'
|
|
' Langton's ant
|
|
'
|
|
' World is a global boolean array, 100x100 in size
|
|
width%=100
|
|
height%=100
|
|
DIM world!(width%,height%)
|
|
ARRAYFILL world!(),FALSE
|
|
' Time in world
|
|
time%=0
|
|
' Ant is represented by a global three-element array
|
|
' holding: x, y, direction [0=north,1=west,2=south,3=east]
|
|
DIM ant%(3)
|
|
'
|
|
@setup_ant
|
|
@run_ant
|
|
@display_world
|
|
'
|
|
' Displays the world to file "langton.out": . for false, # for true
|
|
'
|
|
PROCEDURE display_world
|
|
LOCAL i%,j%
|
|
OPEN "o",#1,"langton.out"
|
|
PRINT #1,"Time in world: ";time%;" ticks"
|
|
FOR i%=0 TO width%-1
|
|
FOR j%=0 TO height%-1
|
|
IF world!(i%,j%)
|
|
PRINT #1,"#";
|
|
ELSE
|
|
PRINT #1,".";
|
|
ENDIF
|
|
NEXT j%
|
|
PRINT #1,""
|
|
NEXT i%
|
|
CLOSE #1
|
|
RETURN
|
|
'
|
|
' Set up the ant to start at (50,50) facing north
|
|
'
|
|
PROCEDURE setup_ant
|
|
ant%(0)=50
|
|
ant%(1)=50
|
|
ant%(2)=0
|
|
RETURN
|
|
'
|
|
' check if ant position is within world's bounds
|
|
'
|
|
FUNCTION ant_in_world
|
|
RETURN ant%(0)>=0 AND ant%(0)<width% AND ant%(1)>=0 AND ant%(1)<height%
|
|
ENDFUNC
|
|
'
|
|
' Turn ant direction to left
|
|
'
|
|
PROCEDURE ant_turn_left
|
|
ant%(2)=(ant%(2)+1) MOD 4
|
|
RETURN
|
|
'
|
|
' Turn ant direction to right
|
|
'
|
|
PROCEDURE ant_turn_right
|
|
ant%(2)=(ant%(2)+3) MOD 4
|
|
RETURN
|
|
'
|
|
' Ant takes a step forward in current direction
|
|
'
|
|
PROCEDURE ant_step_forward
|
|
SELECT ant%(2)
|
|
CASE 0
|
|
ant%(0)=ant%(0)+1
|
|
CASE 1
|
|
ant%(1)=ant%(1)+1
|
|
CASE 2
|
|
ant%(0)=ant%(0)-1
|
|
CASE 3
|
|
ant%(1)=ant%(1)-1
|
|
ENDSELECT
|
|
RETURN
|
|
'
|
|
' Run the ant until it falls out of the world
|
|
'
|
|
PROCEDURE run_ant
|
|
WHILE @ant_in_world
|
|
time%=time%+1
|
|
IF world!(ant%(0),ant%(1)) ! true for white
|
|
world!(ant%(0),ant%(1))=FALSE
|
|
@ant_turn_left
|
|
ELSE ! false for black
|
|
world!(ant%(0),ant%(1))=TRUE
|
|
@ant_turn_right
|
|
ENDIF
|
|
@ant_step_forward
|
|
WEND
|
|
RETURN
|