Drunken walk working but needs some updates as it can go 'off map' and then walk back in, effectively
This commit is contained in:
34
scripts/algorithms/drunk_walk.lua
Normal file
34
scripts/algorithms/drunk_walk.lua
Normal 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)
|
||||
18
scripts/algorithms/test.lua
Normal file
18
scripts/algorithms/test.lua
Normal file
@@ -0,0 +1,18 @@
|
||||
Test = {}
|
||||
|
||||
function Test.initialize(w, h)
|
||||
visualizer.map:fill(false)
|
||||
for mx = 0, w-1 do
|
||||
for my = 0, h-1 do
|
||||
if mx == 0 or mx == w-1 or my == 0 or my == h-1 then
|
||||
visualizer.map:draw_wall(mx, my)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Test.update()
|
||||
|
||||
end
|
||||
|
||||
visualizer.algorithm_manager:register_algorithm("Test", Test.initialize, Test.update, 1)
|
||||
Reference in New Issue
Block a user