Maze generation: Difference between revisions

Added an implementation written in BASIC (QB64)
(→‎{{header|Swift}}: fix bad shuffle)
(Added an implementation written in BASIC (QB64))
Line 856:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
</pre>
 
=={{header|BASIC}}==
This implementation was written using QB64. It should also be compatible with Qbasic, as it uses no QB64-exclusive features.
<lang BASIC>OPTION BASE 0
RANDOMIZE TIMER
 
REM must be even
width% = 40
height% = 20
 
REM make array and fill
DIM maze$(width%, height%)
FOR x% = 0 TO width%
FOR y% = 0 TO height%
maze$(x%, y%) = "#"
NEXT y%
NEXT x%
 
REM initial start location
currentx% = INT(RND * (width% - 1))
currenty% = INT(RND * (height% - 1))
REM value must be odd
IF currentx% MOD 2 = 0 THEN currentx% = currentx% + 1
IF currenty% MOD 2 = 0 THEN currenty% = currenty% + 1
maze$(currentx%, currenty%) = " "
 
REM generate maze
done% = 0
DO WHILE done% = 0
FOR i% = 0 TO 99
oldx% = currentx%
oldy% = currenty%
 
REM move in random direction
SELECT CASE INT(RND * 4)
CASE 0
IF currentx% + 2 < width% THEN currentx% = currentx% + 2
CASE 1
IF currenty% + 2 < height% THEN currenty% = currenty% + 2
CASE 2
IF currentx% - 2 > 0 THEN currentx% = currentx% - 2
CASE 3
IF currenty% - 2 > 0 THEN currenty% = currenty% - 2
END SELECT
 
REM if cell is unvisited then connect it
IF maze$(currentx%, currenty%) = "#" THEN
maze$(currentx%, currenty%) = " "
maze$(INT((currentx% + oldx%) / 2), ((currenty% + oldy%) / 2)) = " "
END IF
NEXT i%
 
REM check if all cells are visited
done% = 1
FOR x% = 1 TO width% - 1 STEP 2
FOR y% = 1 TO height% - 1 STEP 2
IF maze$(x%, y%) = "#" THEN done% = 0
NEXT y%
NEXT x%
LOOP
 
REM draw maze
FOR y% = 0 TO height%
FOR x% = 0 TO width%
PRINT maze$(x%, y%);
NEXT x%
PRINT
NEXT y%
 
REM wait
DO: LOOP WHILE INKEY$ = ""</lang>
 
{{out}}
This used a slightly modified version that outputs to a text file. (You can't copy from a QB64 window.)
<pre>#########################################
# # # # # # # # # #
# ### # # # # ##### # # ### # # # # # ###
# # # # # # # # # # # # # #
# # # ####### # ####### ####### ##### # #
# # # # # # # #
# ##### ### ### # # ### # # ##### ### ###
# # # # # # # # # # # # #
### ##### # ##### ### ### # ### # # #####
# # # # # # # # # # #
# # # ### # # ##### ### # # # # ##### ###
# # # # # # # # # # #
# ### # # ######### ### ####### # ##### #
# # # # # # # # # # # # # # #
# ### ####### # ### # ##### # # ### # # #
# # # # # # # # # #
##### # ### ### ### ### # # # # # # # # #
# # # # # # # # # # # #
# # # # ### # ### ### ### # ### ### ### #
# # # # # # # # # # # # #
#########################################</pre>
 
=={{header|BBC BASIC}}==
Anonymous user