36 lines
1.0 KiB
Lua
36 lines
1.0 KiB
Lua
DrunkenWalk = {
|
|
curr_x = 0,
|
|
curr_y = 0,
|
|
map_w = 0,
|
|
map_h = 0,
|
|
iterations = 30
|
|
}
|
|
|
|
function DrunkenWalk:initialize(w, h)
|
|
math.randomseed()
|
|
self.map_w = w
|
|
self.map_h = h
|
|
self.iterations = 30
|
|
visualizer.map:fill(true)
|
|
self.curr_x = math.random(0, w-1)
|
|
self.curr_y = math.random(0, h-1);
|
|
end
|
|
|
|
function DrunkenWalk:update()
|
|
new_x = self.curr_x + math.random(-1, 1)
|
|
new_y = self.curr_y + math.random(-1, 1)
|
|
while new_x >= self.map_w or new_x < 0 or new_y >= self.map_h or new_y < 0 do
|
|
new_x = self.curr_x + math.random(-1, 1)
|
|
new_y = self.curr_y + math.random(-1, 1)
|
|
end
|
|
self.curr_x = new_x
|
|
self.curr_y = new_y
|
|
visualizer.map:draw_floor(self.curr_x, self.curr_y)
|
|
self.iterations = self.iterations - 1
|
|
if self.iterations == 0 then
|
|
return true
|
|
end
|
|
return false
|
|
end
|
|
|
|
visualizer.algorithm_manager:register_algorithm("Drunken Walk", function (w, h) DrunkenWalk:initialize(w, h) end, function () return DrunkenWalk:update() end, 5) |