' !! SAVE THIS SOURCE AS MICE.BAS !! 'Program : The mice in the maze 'Programmer : Lawrence Law, Malaysia 'Note : For QBASIC/QuickBASIC DECLARE SUB Delay (second AS INTEGER) OPTION BASE 1 CONST True = 1 DIM m(20, 20) 'Indicate the maze DIM s(1000, 2) 'The stack DIM ss, stack AS INTEGER 'ss is the pointer to stack DIM x, y, ox, oy, way AS INTEGER ss = 0 CLS 'Create the maze randomly RANDOMIZE TIMER FOR x = 1 TO 20 FOR y = 1 TO 20 a = INT(RND * 3) + 1 IF a = 1 THEN m(y, x) = 1: LOCATE y, x: PRINT CHR$(178) ELSE LOCATE y, x: PRINT "." END IF NEXT y NEXT x 'Print out the walls FOR x = 1 TO 20 m(1, x) = 1: LOCATE 1, x: PRINT CHR$(178) m(20, x) = 1: LOCATE 20, x: PRINT CHR$(178) NEXT x FOR y = 1 TO 20 m(y, 1) = 1: LOCATE y, 1: PRINT CHR$(178) m(y, 20) = 1: LOCATE y, 20: PRINT CHR$(178) NEXT y 'Locate the mice randomly DO x = INT(RND * 20) + 1 y = INT(RND * 20) + 1 LOOP UNTIL m(y, x) = 0 LOCATE y, x: PRINT "@" 'The control portion DO WHILE True m(y, x) = -1 ox = x: oy = y: way = 0 IF m(y, x + 1) = 0 THEN way = way + 1: dir = 4 IF m(y, x - 1) = 0 THEN way = way + 1: dir = 3 IF m(y + 1, x) = 0 THEN way = way + 1: dir = 2 IF m(y - 1, x) = 0 THEN way = way + 1: dir = 1 SELECT CASE way CASE 0 IF ss = 0 THEN END ELSE y = y - s(ss, 1) x = x - s(ss, 2) ss = ss - 1 END IF CASE 1 SELECT CASE ss CASE 0 SELECT CASE dir CASE 1 y = y - 1 CASE 2 y = y + 1 CASE 3 x = x - 1 CASE 4 x = x + 1 END SELECT CASE IS > 0 stack = 1 END SELECT CASE IS > 1 stack = 1 END SELECT IF stack = 1 THEN ss = ss + 1 SELECT CASE dir CASE 1 y = y - 1 s(ss, 1) = -1 s(ss, 2) = 0 CASE 2 y = y + 1 s(ss, 1) = 1 s(ss, 2) = 0 CASE 3 x = x - 1 s(ss, 1) = 0 s(ss, 2) = -1 CASE 4 x = x + 1 s(ss, 1) = 0 s(ss, 2) = 1 END SELECT stack = 0 END IF 'Clear the old position and locate the mice at new location LOCATE oy, ox: PRINT " " LOCATE y, x: PRINT "@" Delay .5 LOOP SUB Delay (second AS INTEGER) i = TIMER DO LOOP UNTIL TIMER - i > second END SUB