-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.lua
70 lines (63 loc) · 1.34 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
local maze = {}
maze.__index = maze
function maze:new(grid)
grid[2][2] = "X"
return setmetatable(
{
grid = grid,
row = 2,
col = 2,
state_set = function(_, _)
end,
},
self
)
end
function maze:draw()
io.write("\027[H\027[2J")
for _, row in ipairs(self.grid) do
for _, cell in ipairs(row) do
io.write(cell)
end
print("")
end
print("")
end
function maze:move(input)
local row, col = self.row, self.col
self.state_set(row, col)
if input == "w" then
row = row - 1
elseif input == "s" then
row = row + 1
elseif input == "a" then
col = col - 1
elseif input == "d" then
col = col + 1
else
error("wrong command")
end
if row < 1 or col < 1 or row > #self.grid or col > #self.grid[1] then
return false
end
local r = self.grid[self.row]
r[self.col] = " " -- clear the marker
local lastr = r
r = self.grid[row]
if r[col] == "#" then
-- win
r[col] = "X"
return true
end
if r[col] == " " then
self.row = row
self.col = col
r[self.col] = "X"
return false
else
-- collision
lastr[self.col] = "x"
return false
end
end
return maze