maze code - Printable Version +- Qbasicnews.com (http://qbasicnews.com/newforum) +-- Forum: QBasic (http://qbasicnews.com/newforum/forum-4.html) +--- Forum: QB Projects (http://qbasicnews.com/newforum/forum-12.html) +--- Thread: maze code (/thread-7805.html) |
maze code - loking1235 - 07-28-2005 Hello, Do not know if anyone would be able to use this but here is the code for what I have used to make my mazes. I have put as much info in it as I can. Good luck. Doug '************* This makes maze. All rooms will be visited and all rooms '************* can be reached from every other room. There will be blank spaces '************* to make it less dense like a dungeon(some rooms are filled in). '************* You can remove The code that '************* adds loops and removes dead ends to make a pure maze(all rooms used). '************* The value of the array sps% could be saved as a file. It is the '************* room number and the directions 1=north 2=south 3=west 4=east '************* and if it is blocked(1) or open(0). '************* The maze is drawn on screen 13 so you can see it. Once it is '************* drawn press any key to make a new maze or ESC to exit. '************* Change mx,my to change the size of the maze(max x=31 max y=19) '************* Change lop to change the chance for a loop.(min = 2) '************* Change dense to change the number of rooms with one exit that '************* are filled in to make the dungeon less dense. '************* mx=x size of maze '************* my=y size of maze '************* lop=chance of loops being made. A higher number is a lesser '************* chance for a loop. '************* dense=chance of dead ends being removed and filled in. '************* sp=total spaces in maze(mx*my) '************* vs%(#,#)=if room is visited first # is room x # second # is room y '************* drt%(1-4)=random direction check for maze generation '************* svbps%(#,1-4)=direction of doors of room 1st # is cell # 2nd # is '************* direction(1=north 2=south 3=west 4=east) =blocked(1) or open(0) '************* csp=current cell space (c = cy - 1 : cc = c * mx : csp = cc + cx) '************* pcx=graphic x pcx = ((cx * 10) - 5) '************* pcy=graphic y pcy = ((cy * 10) - 5) '************* c and ts are used as space holders. '************* Set screen 13. '************* Randomize. SCREEN 13 RANDOMIZE (timer) st: '((((-*-)))))) '************** start of program. '************* '************* Start of making the maze. '************* '************* Set maze size mx,my.Set lop as 5. Set denst as 5 mx = 20: my = 19 : lop = 5 : dense=5 '************* Clear the screen. CLS '************** Draw the mazes basic shape on the screen. Just rooms with no doors. FOR xx = 1 TO (mx * 10) STEP 10 FOR yy = 1 TO (my * 10) STEP 10 LINE (xx, yy)-(xx + 9, yy + 9), 1, B NEXT yy NEXT xx '************* '************* Start of makeing the basic maze. '************* '************* sp=total spaces in maze(mx*my). '************* vs%(#,#)=cell visited first # is cell # 2nt # is 0 not visited '************* 1 visited . '************* drt%(1-4)=direction check random for maze generation . '************* sps%(#,1-4)=doors of cells 1st # is cell # 2nd # is direction '************* 1=north 2=south 3=west 4=east =blocked(1) or open(0). '************* dim arrays. sp = (mx * my) DIM vs%(mx, my), drt%(5), sps%(sp, 4), dsp%(sp) '************* Set the array vs%(x,y) to all not visited(0). '************* This makes all rooms not visited. FOR t = 1 TO mx FOR tt = 1 TO my vs%(t, tt) = 0 NEXT tt NEXT t '************* Make array for each room and make each direction blocked(1). '************* sps%(#,1-4)=blocked(1). '************* This makes all rooms have no doors . FOR t = 1 TO sp sps%(t, 1) = 1 sps%(t, 2) = 1 sps%(t, 3) = 1 sps%(t, 4) = 1 NEXT t '************* Pick a random room. This is a number between 1 and sp(total rooms '************* in the maze). rndsp = INT(RND(1) * sp) + 1 '************* This converts the room number into the cx,cy values. c = INT(rndsp / mx) cy = (c + 1) cx = (rndsp - (c * mx)) IF cx = 0 THEN cx = mx: cy = cy - 1 '************** Set first random room as visited. vs%(cx, cy) = 1 '((((-*-)))))) Pick random number 1 to 4, then set drt% as a random direction '************** (1=north 2=south 3=west 4=east). drc is used to see if all directions '************** have been checked. (1=1 direction checked 2=2 directions checked ect..) pic: dr = INT(RND(1) * 4) + 1 IF dr = 1 THEN drt%(1) = 1: drt%(2) = 2: drt%(3) = 3: drt%(4) = 4: drc = 1 IF dr = 2 THEN drt%(1) = 2: drt%(2) = 3: drt%(3) = 4: drt%(4) = 1: drc = 1 IF dr = 3 THEN drt%(1) = 3: drt%(2) = 4: drt%(3) = 1: drt%(4) = 2: drc = 1 IF dr = 4 THEN drt%(1) = 4: drt%(2) = 1: drt%(3) = 2: drt%(4) = 3: drc = 1 '((((-*-)))))) Check the direction to see if valid. chdr: '************** If all directions are invalid pick new visited room. IF drc = 4 THEN drc = 1: GOTO fndvis: '************** Set dc as the direction (1=north 2=south 3=west 4=east). dc = drt%(drc) '************** Check to see if there there is a room in that direction. '************** If there is no room in that direction then add one to drc '************** and go to chdr: to check the next direction. IF dc = 1 AND cy = 1 THEN drc = drc + 1: GOTO chdr: IF dc = 2 AND cy = my THEN drc = drc + 1: GOTO chdr: IF dc = 3 AND cx = 1 THEN drc = drc + 1: GOTO chdr: IF dc = 4 AND cx = mx THEN drc = drc + 1: GOTO chdr: '************** Set the room you are looking at as lx,ly. IF dc = 1 THEN ly = cy - 1: lx = cx IF dc = 2 THEN ly = cy + 1: lx = cx IF dc = 3 THEN lx = cx - 1: ly = cy IF dc = 4 THEN lx = cx + 1: ly = cy '************** If the room you are looking at is visited then then add one to drc '************** and go to chdr: to check next direction. IF vs%(lx, ly) = 1 THEN drc = drc + 1: GOTO chdr: '************** If all directions are invalid then pick a new visited room. IF drc = 4 THEN drc = 1: GOTO fndvis: '************** Set current room as csp. Converts cx,cy into space number csp c = cy - 1 cc = c * mx csp = cc + cx '************** Make door open in the direction (1=north 2=south 3=west 4=east). '************** sps%(csp,direction)=0. From current room to the room you are '************* looking at. IF dc = 1 THEN sps%(csp, 1) = 0 IF dc = 2 THEN sps%(csp, 2) = 0 IF dc = 3 THEN sps%(csp, 3) = 0 IF dc = 4 THEN sps%(csp, 4) = 0 '************* Set graphic points for room. pcx = ((cx * 10) - 5) pcy = ((cy * 10) - 5) '************** Draw the opening for the room.dc=(1=north 2=south 3=west 4=east). IF dc = 1 THEN LINE (pcx - 1, pcy - 5)-(pcx + 2, pcy - 4), o, B IF dc = 2 THEN LINE (pcx - 1, pcy + 5)-(pcx + 2, pcy + 6), o, B IF dc = 3 THEN LINE (pcx - 5, pcy - 1)-(pcx - 4, pcy + 2), o, B IF dc = 4 THEN LINE (pcx + 5, pcy - 1)-(pcx + 6, pcy + 2), o, B '************** Make the room you are looking at the current room and make visited. cx = lx: cy = ly: vs%(cx, cy) = 1 '******csp=current space cx,cy. Converts cx,cy into space number csp. c = cy - 1 cc = c * mx csp = cc + cx '************** Make door open in the direction (1=north 2=south 3=west 4=east). '************** sps%(csp,direction)=0. From current room and the room you just '************* moved from. IF dc = 1 THEN sps%(csp, 2) = 0 IF dc = 2 THEN sps%(csp, 1) = 0 IF dc = 3 THEN sps%(csp, 4) = 0 IF dc = 4 THEN sps%(csp, 3) = 0 '************** Check to see if all rooms have been visited. If not goto pic: '************** to pick next room. FOR t = 1 TO mx FOR tt = 1 TO my c = vs%(t, tt) IF c = 0 THEN GOTO pic: NEXT tt NEXT t '************* '************* End of making the basic maze. '************* '************* '************* Start of removing part of the dead ends to create loops. '************* '************* ts is used as a counter '************* Check each room to see if it has only one exit. C counts '************* the number of exits that are blocked in the room and if '************* it is 3(only one exit) then it may be made into a loop. ts = 1 FOR tt = 1 TO my FOR t = 1 TO mx c = 0: cc = 0 cc = sps%(ts, 1): c = c + cc cc = sps%(ts, 2): c = c + cc cc = sps%(ts, 3): c = c + cc cc = sps%(ts, 4): c = c + cc '************* Set graphic points for room. pcx = ((t * 10) - 5) pcy = ((tt * 10) - 5) '************* If only one exit then goto lpic: and see if loop is made. IF c = 3 THEN GOTO lpic: '************* Go to lnext: to go to next room. GOTO lnext: '((((-*-)))))) lpic: '************* Make a loop if rdd=2. This gives a one and lop chance for a '************* room with only one exit to be made in to a loop. Go to lpic2: '************* to make a loop. rdd = INT(RND(1) * lop) + 1 IF rdd = 2 THEN GOTO lpic2: '************* Goto lnext: to go to the next room. GOTO lnext: '((((-*-)))))) Pick random number 1 to 4, then set drt% as a random direction '************** (1=north 2=south 3=west 4=east). drc is used to see if all directions '************** have been checked. (1=1 direction checked 2=2 directions checked ect..) lpic2: dr = INT(RND(1) * 4) + 1 IF dr = 1 THEN drt%(1) = 1: drt%(2) = 2: drt%(3) = 3: drt%(4) = 4: drc = 1 IF dr = 2 THEN drt%(1) = 2: drt%(2) = 3: drt%(3) = 4: drt%(4) = 1: drc = 1 IF dr = 3 THEN drt%(1) = 3: drt%(2) = 4: drt%(3) = 1: drt%(4) = 2: drc = 1 IF dr = 4 THEN drt%(1) = 4: drt%(2) = 1: drt%(3) = 2: drt%(4) = 3: drc = 1 '((((-*-)))))) '************** If all directions are invalid pick new visited room. lchdr: IF drc = 4 THEN drc = 1: GOTO lnext: dc = drt%(drc) '************** Check to see if there there is a room in that direction. '************** If there is no room in that direction then add one to drc '************** and go to lchdr: to check the next direction. IF dc = 1 AND tt = 1 THEN drc = drc + 1: GOTO lchdr: IF dc = 2 AND tt = my THEN drc = drc + 1: GOTO lchdr: IF dc = 3 AND t = 1 THEN drc = drc + 1: GOTO lchdr: IF dc = 4 AND t = mx THEN drc = drc + 1: GOTO lchdr: '************** Set the room you are looking at as lx,ly. IF dc = 1 THEN ly = tt - 1: lx = t IF dc = 2 THEN ly = tt + 1: lx = t IF dc = 3 THEN lx = t - 1: ly = tt IF dc = 4 THEN lx = t + 1: ly = tt '************** Set current room as csp. Converts cx,cy into space number csp c = tt - 1 cc = c * mx csp = cc + t '************** Make door open in the direction (1=north 2=south 3=west 4=east). '************** sps%(csp,direction)=0. From current room to the room you are '************* looking at. IF dc = 1 THEN sps%(csp, 1) = 0 IF dc = 2 THEN sps%(csp, 2) = 0 IF dc = 3 THEN sps%(csp, 3) = 0 IF dc = 4 THEN sps%(csp, 4) = 0 '************* Set graphic points for room. pcx = ((t * 10) - 5) pcy = ((tt * 10) - 5) '************** Draw the opening for the room.dc=(1=north 2=south 3=west 4=east). IF dc = 1 THEN LINE (pcx - 1, pcy - 5)-(pcx + 2, pcy - 4), o, B IF dc = 2 THEN LINE (pcx - 1, pcy + 5)-(pcx + 2, pcy + 6), o, B IF dc = 3 THEN LINE (pcx - 5, pcy - 1)-(pcx - 4, pcy + 2), o, B IF dc = 4 THEN LINE (pcx + 5, pcy - 1)-(pcx + 6, pcy + 2), o, B '************** Make the room you are looking at the current room and make visited. cx = lx: cy = ly: vs%(cx, cy) = 1 c = cy - 1 cc = c * mx csp = cc + cx '************** Make door open in the direction (1=north 2=south 3=west 4=east). '************** sps%(csp,direction)=0. From current room and the room you just '************* moved from. IF dc = 1 THEN sps%(csp, 2) = 0 IF dc = 2 THEN sps%(csp, 1) = 0 IF dc = 3 THEN sps%(csp, 4) = 0 IF dc = 4 THEN sps%(csp, 3) = 0 '((((-*-)))))) '********** Next room. lnext: ts = ts + 1 NEXT t NEXT tt '************* '************* End of removing part of the dead ends to create loops. '************* '************* '************* Start of filling in dead ends to make maze less dense. '************* '************* count the number of rooms with only one exit. '************* if dead end then remove room and door to other room and door from '************* other room. Do it dense times. The higher dense is the less dense '************* the maze is FOR v = 1 TO dense '************* ts is used as a counter '************* Check each room to see if it has only one exit. C counts '************* the number of exits that are blocked in the room and if '************* it is 3(only one exit) then it may be filled in. ts = 1 FOR tt = 1 TO my FOR t = 1 TO mx c = 0: cc = 0 cc = sps%(ts, 1): c = c + cc cc = sps%(ts, 2): c = c + cc cc = sps%(ts, 3): c = c + cc cc = sps%(ts, 4): c = c + cc '************* Set graphic points for room. pcx = ((t * 10) - 5) pcy = ((tt * 10) - 5) '************* if only one exit erase the room and remove doors from both rooms. '************* Fill the room. IF c = 3 AND sps%(ts, 1) = 0 THEN LINE (pcx - 3, pcy - 5)-(pcx + 4, pcy + 4), 1, BF: sps%((ts - mx), 2) = 1: vs%(t, tt) = 1 IF c = 3 AND sps%(ts, 2) = 0 THEN LINE (pcx - 3, pcy - 3)-(pcx + 4, pcy + 6), 1, BF: sps%((ts + mx), 1) = 1: vs%(t, tt) = 1 IF c = 3 AND sps%(ts, 3) = 0 THEN LINE (pcx - 5, pcy - 4)-(pcx + 4, pcy + 4), 1, BF: sps%((ts - 1), 4) = 1: vs%(t, tt) = 1 IF c = 3 AND sps%(ts, 4) = 0 THEN LINE (pcx - 3, pcy - 4)-(pcx + 6, pcy + 4), 1, BF: sps%((ts + 1), 3) = 1: vs%(t, tt) = 1 ts = ts + 1 NEXT t NEXT tt NEXT v '************* '************* End of filling in dead ends to make maze less dense. '************* '************* '************* end of makeing the maze. '************* '************* '************* Start of waiting for input to make new maze or exit. '************* '************** Loop untill a key is pressed. do a$=inkey$ '************** End the program. if a$=chr$(27) then end if a$=chr$(255)+"X" then end loop while a$="" Goto st: '************* '************* End of waiting for input to make new maze or exit. '************* '************* '************* Start of finding a random room and checking if it has been visited. '************* '((((-*-)))))) fndvis: '************* Pick a random room. This is a number between 1 and sp(total rooms '************* in the maze). rndsp = INT(RND(1) * sp) + 1 '************* This converts the room number into the cx,cy values. c = INT(rndsp / mx) cy = (c + 1) cx = (rndsp - (c * mx)) IF cx = 0 THEN cx = mx: cy = cy - 1 '************** Check to see if the room has been visited. If not goto pic: '************** to pick next room. IF vs%(cx, cy) = 1 THEN GOTO pic: GOTO fndvis: '************* '************* End of finding a random room and checking if it has been visited. '************* |