-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.lua
76 lines (59 loc) · 1.99 KB
/
maze.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
local utils = require 'utils'
local maze = {}
local function createGrid( gridSize )
local grid = {}
for i = 0, gridSize.width do
grid[i] = {}
for j = 0, gridSize.height do
grid[i][j] = '1111'
end
end
return grid
end
function maze.generate( gridSize, startingPosition )
local currentCell = {['x'] = startingPosition.x, ['y'] = startingPosition.y}
local totalCells = gridSize.width * gridSize.height
local totalVisited = 0
local visited = {}
local grid = createGrid(gridSize)
visited[currentCell.x .. currentCell.y] = true
totalVisited = totalVisited + 1
while totalVisited < totalCells do
-- go to random cell
local direction = utils.random(0, 4) -- 0, 1, 2, 3
local neighbor = {['x'] = currentCell.x, ['y'] = currentCell.y}
if direction == 0 then
neighbor.x = currentCell.x - 1
elseif direction == 1 then
neighbor.y = currentCell.y - 1
elseif direction == 2 then
neighbor.x = currentCell.x + 1
else
neighbor.y = currentCell.y + 1
end
while neighbor.x < 0 or neighbor.x >= gridSize.width or neighbor.y < 0 or neighbor.y >= gridSize.height do
direction = utils.random(0, 4)
neighbor = {['x'] = currentCell.x, ['y'] = currentCell.y}
if direction == 0 then
neighbor.x = currentCell.x - 1
elseif direction == 1 then
neighbor.y = currentCell.y - 1
elseif direction == 2 then
neighbor.x = currentCell.x + 1
else
neighbor.y = currentCell.y + 1
end
end
if not visited[neighbor.x .. neighbor.y] then
-- remove the walls
grid[currentCell.x][currentCell.y] = utils.replace_char(direction + 1, grid[currentCell.x][currentCell.y], 0)
grid[neighbor.x][neighbor.y] = utils.replace_char((direction + 2) % 4 + 1, grid[neighbor.x][neighbor.y], 0)
visited[neighbor.x .. neighbor.y] = true
totalVisited = totalVisited + 1
end
currentCell.x = neighbor.x
currentCell.y = neighbor.y
end
return grid
end
return maze