require "math"
DrunkenWalk = {
    curr_x = 0,
    curr_y = 0,
    map_w = 0,
    map_h = 0,
    iterations = 30
}

function DrunkenWalk:initialize(w, h)
    math.randomseed(os.time())
    self.map_w = w
    self.map_h = h
    visualizer.map:fill(true)
    self.curr_x = math.random(0, w-1)
    self.curr_y = math.random(0, h-1);
end

function DrunkenWalk:update()
    self.curr_x = self.curr_x + math.random(-1, 1)
    self.curr_y = self.curr_y + math.random(-1, 1)
    while self.curr_x >= self.map_w or self.curr_x < 0 or self.curr_y >= self.map_h or self.curr_y < 0 do
        self.curr_x = self.curr_x + math.random(-1, 1)
        self.curr_y = self.curr_y + math.random(-1, 1)
    end
    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)