DECLARE SUB Boxes () 'This is a QBasic program that can be run on any DOS machine with 'VGA graphics. It was written by Charles Wells, Department of 'Mathematics, Case Western Reserve University, Cleveland, OH '44106-7058, USA. Email cfw2@po.cwru.edu. 'Phone 216 368 2893 or 216 774 1926. 'Version of 24 April 1993. This version incorporates a code 'improvement suggested by Michael Somos and a bug correction and 'many suggestions by Robert Israel. 'This program implements Chris Langton's cellular automaton 'described in the Mathematical Intelligencer, volume 15, number 2 '(Spring 1993), page 54. It describes the path of an ant who 'starts pointing in a certain direction. If the ant is on a 'non-white square it turns the square red, rotates 90 degrees 'clockwise and moves one pixel in the direction it is pointing. 'If it is on a red square it turns the square white, rotates 90 'degrees counterclockwise and moves one pixel in the direction it 'is pointing. In this implementation the path of the ant wraps 'around when it reaches the edge of the screen. Most amazingly 'these simple rules produce behavior which looks chaotic for 'THOUSANDS of moves, then goes into a steady state during which 'it runs off the screen. 'To run the program, save it with extension ".bas" in the same 'directory as QBASIC. Run QBASIC, open this program, and hit F5. 'To halt it, hit the escape key. SCREEN 7 '320 by 200 dots on the screen wdth = 320 ht = 200 x = 160: y = 100 'midpoint of screen 'Comment out the preceding 4 lines and include the following for a 'screen with more pixels. (It may be hard to see on small screens.) 'SCREEN 12 '640 by 480 dots on the screen 'wdth = 640 'ht = 480 'x = 320: y = 240 'midpoint of screen LINE (0, 0)-(wdth, ht), 7, BF 'Paint the screen white 'initialize direction vector DIM xdirs(3) 'x coordinate of direction vector DIM ydirs(3) 'y coordinate of direction vector DATA -1, 0, 1, 0, 0, 1, 0, -1 FOR i = 0 TO 3 READ xdirs(i) NEXT i FOR i = 0 TO 3 READ ydirs(i) NEXT i vector = 0 'direction at beginning is to left RANDOMIZE TIMER 'seed the random number generator maxdots = 0 'make this positive if you want to start with 'some random dots on the screen FOR counter = 1 TO maxdots u = INT(RND * wdth) + 1 v = INT(RND * ht) + 1 PSET (u, v), 4 'put a dot NEXT counter maxlines = 0 'make this positive if you want to start with 'some random lines on the screen FOR counter = 1 TO maxlines u = INT(RND * wdth) + 1 v = INT(RND * ht) + 1 w = INT(RND * wdth) + 1 z = INT(RND * ht) + 1 LINE (u, v)-(w, z), 4 NEXT counter 'Try including the next two lines for more fun: 'LINE (100, 75)-(ht, 75), 4 'LINE (100, 140)-(ht, 140), 4 'The following subroutine suggested by Robert Israel puts some random 'boxes on the screen for the ant to play with. It 'produces some striking effects. Remove the apostrophe to 'make it run. 'Boxes count = 0 'this keeps track of the number of iterations. 'the following do loop is the main process DO WHILE (INKEY$ <> CHR$(27)) IF x < 0 THEN x = wdth - 1 IF x > wdth - 1 THEN x = 0 IF y < 0 THEN y = ht - 1 IF y > ht - 1 THEN y = 0 count = count + 1 q = POINT(x, y) IF q = 7 THEN PSET (x, y), 4 'change the color vector = (vector + 1) MOD 4 'rotate direction clockwise x = x + xdirs(vector): y = y + ydirs(vector) ELSE PSET (x, y), 7 vector = (vector + 3) MOD 4 'rotate counterclockwise x = x + xdirs(vector): y = y + ydirs(vector) END IF LOOP PRINT "Number of iterations:", count SUB Boxes SHARED wdth, ht maxboxes = 5 LINE (0, 0)-(wdth, ht), 0, BF FOR counter = 1 TO maxboxes u = INT(RND * wdth) + 1 v = INT(RND * ht) + 1 w = INT(RND * wdth) + 1 z = INT(RND * ht) + 1 LINE (u, v)-(w, z), 7, BF NEXT counter END SUB