Drunken walk working but needs some updates as it can go 'off map' and then walk back in, effectively

This commit is contained in:
m
2023-07-23 00:57:19 -04:00
parent 55e2186c86
commit d7fa6069f8
9 changed files with 145 additions and 15 deletions

View File

@@ -0,0 +1,34 @@
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)