DEFINT A-Z
'------------------------------------------------------------'
'  Program ANTY.BAS by David Rice, based upon the article in '
'  July 1994 _Scientific_American_ called "The Ultimate in   '
'  Anty-Particles," page 104.                                '
'  This program demonstrates how a complex structure (called '
'  "highway building" in the article) may result from very   '
'  simple, basic natural laws. The laws for the ant are as   '
'  follows:                                                  '
'     On a 320 X 200 grid, an ant moves one square in any    '
'     of four directions. If the square it moves on is       '
'     black, it paints it white and changes direction to     '
'     the left. If it lands on a white square, it paints     '
'     it black and changes direction to the right.           '
'------------------------------------------------------------'
DECLARE SUB Completed (AntMovedCount%)
RANDOMIZE TIMER: SCREEN 1: CLS
AntX = (320 \ 2): AntY = (200 \ 2)
AntDirection = ((RND * 4) + 1): '  for random direction
AntDirection = 1: AntMovedCount% = 0
White = 16: Black = 0
'
DATA -1,0: ' east       1
DATA 0,-1: ' north      2
DATA 1,0: '  west       3
DATA 0,1: '  south      4
DIM Directions%(1 TO 4, 1 TO 2)
FOR A% = 1 TO 4
   READ Directions%(A%, 1), Directions%(A%, 2)
NEXT
DO
   AntX = AntX + Directions%(AntDirection, 1)
   AntY = AntY + Directions%(AntDirection, 2)
   IF AntX < 0 OR AntX > 319 THEN CALL Completed(AntMovedCount%)
   IF AntY < 0 OR AntY > 199 THEN CALL Completed(AntMovedCount%)
   AntMovedCount% = AntMovedCount% + 1
   '
   IF POINT(AntX, AntY) = Black THEN
      PSET (AntX, AntY), White
      SELECT CASE AntDirection
         CASE 1: AntDirection = 4
         CASE 2: AntDirection = 1
         CASE 3: AntDirection = 2
         CASE 4: AntDirection = 3
      END SELECT
   ELSE
      PSET (AntX, AntY), Black
      SELECT CASE AntDirection
         CASE 1: AntDirection = 2
         CASE 2: AntDirection = 3
         CASE 3: AntDirection = 4
         CASE 4: AntDirection = 1
      END SELECT
   END IF
LOOP
SUB Completed (AntMovedCount%)
   BEEP
   A$ = INPUT$(1)
   SYSTEM
END SUB