'amazing: heavily optimized by m barry 4-30-98

DEFINT A-Z: hmax = 25: vmax = 100: RANDOMIZE TIMER

DIM w(hmax + 1, vmax + 1): DEF fnr (t) = INT(RND * t) + 1

PRINT "This program prints out a maze of dimensions up to"; hmax; "x"; vmax

PRINT "To get a copy of the maze be sure that you have the printer"

INPUT "activated before continuing. To start hit RETURN"; h

DO 'forever

DO 'get a legal set of dimensions

INPUT "width, length"; h, v

gd = h > 0 AND h <= hmax AND v > 0 AND v <= vmax

IF gd THEN PRINT "One moment please..." ELSE PRINT "Illegal dimensions."

LOOP UNTIL gd

FOR y = 0 TO v + 1: FOR x = 0 TO h + 1 'initialize blank maze

w(x, y) = NOT ((x < 1 OR x > h) AND -2 OR (y < 1 OR y > v) AND -3)

NEXT x, y

x = 1: y = 1: c = h * v: w(x, y) = 3

DO WHILE c > 1 'fill the entire maze with paths

lt = w(x - 1, y): up = w(x, y - 1): rt = w(x + 1, y): dn = w(x, y + 1)

IF (lt OR up OR rt OR dn) < 0 THEN

z = 2 * 2 ^ fnr(4): c = c - 1: dx = 0: dy = 0

DO 'knock down a wall in a random but legal direction

IF lt AND z AND 4 THEN m = 3: dx = -1: n = lt AND 2: EXIT DO

IF up AND z AND 8 THEN m = 3: dy = -1: n = up AND 1: EXIT DO

IF rt AND z AND 16 THEN m = 2: dx = 1: n = rt AND 3: EXIT DO

IF dn AND z AND 32 THEN m = 1: dy = 1: n = dn AND 3: EXIT DO

z = 60 'if random direction was illegal, go back and try all

LOOP

w(x, y) = w(x, y) AND m: x = x + dx: y = y + dy: w(x, y) = n

ELSE

DO 'search for another path to branch from

y = y + 1: IF y > v THEN y = 1: x = x + 1: IF x > h THEN x = 1

LOOP UNTIL w(x, y) >= 0

END IF

LOOP

w(fnr(h), 0) = 0: x = fnr(h): w(x, v) = w(x, v) AND 1 'knock out entrance and exit

FOR y = 0 TO v 'print out the completed maze

FOR x = 0 TO h: PRINT " "; MID$(" |", 1 + (w(x, y) AND 1), 1); : NEXT: PRINT

FOR x = 0 TO h: PRINT MID$(" --", 1 + (w(x, y) AND 2), 2); "+"; : NEXT: PRINT

NEXT y

LOOP

END

barrym95838

EO144107

P.S. Don't be misled by the two-digit year in the comment line. I have tested it in QBASIC, and it does NOT have a Y2K bug.

“Never attribute to malice that which can be adequately explained by stupidity.”