Files
2024-11-22 13:46:04 -05:00

36 lines
1.0 KiB
Lua

DrunkenWalk = {
curr_x = 0,
curr_y = 0,
map_w = 0,
map_h = 0,
iterations = 0
}
function DrunkenWalk:initialize(w, h)
math.randomseed()
self.map_w = w
self.map_h = h
self.iterations = 200
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, 60)