RosettaCodeData/Task/Maze-generation/BASIC256/maze-generation.basic

65 lines
1.7 KiB
Plaintext

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(int(rand * size_x), int(rand * size_y))
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