Jump to content

Maze generation: Difference between revisions

Tidy up QB64 entry a bit, add Basic-256 entry
m (→‎{{header|Phix}}: added syntax colouring, marked p2js compatible)
(Tidy up QB64 entry a bit, add Basic-256 entry)
Line 985:
 
=={{header|BASIC}}==
==={{header|QB64}}===
This implementation was written using QB64. It should also be compatible with Qbasic, as it uses no QB64-exclusive features.
<lang BASICqb64>OPTION BASE 0
RANDOMIZE TIMER
 
Line 1,078 ⟶ 1,079:
# # # # # # # # # # # # #
#########################################</pre>
 
==={{header|BASIC256}}===
<lang basic256>global size_x, size_y
size_x = 25
size_y = 15
 
global char_wall, char_room
char_wall = "#"
char_room = " "
 
global directions_permutations
directions_permutations = {{0, 1, 2, 3}, {0, 1, 3, 2}, {0, 2, 1, 3}, {0, 2, 3, 1}, {0, 3, 1, 2}, {0, 3, 2, 1}, {1, 0, 2, 3}, {1, 0, 3, 2}, {1, 2, 0, 3}, {1, 2, 3, 0}, {1, 3, 0, 2}, {1, 3, 2, 0}, {2, 0, 1, 3}, {2, 0, 3, 1}, {2, 1, 0, 3}, {2, 1, 3, 0}, {2, 3, 0, 1}, {2, 3, 1, 0}, {3, 0, 1, 2}, {3, 0, 2, 1}, {3, 1, 0, 2}, {3, 1, 2, 0}, {3, 2, 0, 1}, {3, 2, 1, 0}}
 
global maze
dim maze[size_x * 2 + 1][size_y * 2 + 1]
for i = 0 to size_x * 2
for j = 0 to size_y * 2
maze[i][j] = char_wall
next j
next i
 
call make_room(0, 0)
 
call draw_maze()
 
end
 
subroutine make_room(room_x, room_y)
maze[1 + room_x * 2][1 + room_y * 2] = char_room
random_directions_index = rand * 24
for i = 0 to 3
random_direction = directions_permutations[random_directions_index][i]
if ((random_direction / 2) mod 2) < 1 then
dx = (random_direction mod 2) * 2 - 1
dy = 0
else
dx = 0
dy = (random_direction mod 2) * 2 - 1
end if
if can_dig(room_x + dx, room_y + dy) then
call make_door(room_x, room_y, dx, dy)
call make_room(room_x + dx, room_y + dy)
end if
next i
end subroutine
 
function can_dig(room_x, room_y)
if (room_x < 0) or (room_x >= size_x) or (room_y < 0) or (room_y >= size_y) then
can_dig = false
else
can_dig = (maze[1 + room_x * 2][1 + room_y * 2] = char_wall)
end if
end function
 
subroutine make_door(room_x, room_y, dx, dy)
maze[1 + room_x * 2 + dx][1 + room_y * 2 + dy] = char_room
end subroutine
 
subroutine draw_maze()
for i = 0 to size_y * 2
for j = 0 to size_x * 2
print maze[j][i];
next j
print
next i
end subroutine</lang>
 
{{out}}
<pre>###################################################
# # # # # # # # #
# # ### ### ##### ### ### # ### # # ### # # ### ###
# # # # # # # # # # # # # # # # #
# # # ### # # ##### ### # ### # ##### # ######### #
# # # # # # # # # # #
##### # ### ### ### # ################### # ##### #
# # # # # # # # # # #
# ##################### ### ##### # # # ##### # ###
# # # # # # # # # #
# ### ############### # # ##### ### # ##### # # # #
# # # # # # # # # # # # # # #
####### ### # # ### # ### # ########### # # ### # #
# # # # # # # # # # # # # # #
# # ##### ##### ### ####### # ### # # ### # # ### #
# # # # # # # # # # # # #
# # # ########### ### # ### # # ############### # #
# # # # # # # # #
# ### # # ##### ######### ####### # ### ###########
# # # # # # # # # # # # #
# ### # ### # ##### ### ### # ##### # ### ####### #
# # # # # # # # # # # # # #
### ### # ### # # ####### ##### ####### ### # # # #
# # # # # # # # # # # # # # #
# ### # ### # # # # ### ######### # # ### ####### #
# # # # # # # # # # # # # #
# ### # # ############### # # # # # # ##### ##### #
# # # # # # # # # # # # # #
# ######### # # ### # # ########### ##### # # ### #
# # # # # # #
###################################################</pre>
 
=={{header|Batch File}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.