summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2018-04-22 17:16:47 +0200
committerNoah Loomans <noahloomans@gmail.com>2018-04-22 17:16:47 +0200
commit30226e6ea3de0b36fb3e1f34800befc94b8dc167 (patch)
treead8ba9a1066a32238f2f0ddd2be939484390982f
parent104f73d835c2565dfab12b2909bd2ab68c265027 (diff)
Refactor algorithm switchHEADmaster
-rw-r--r--maze_generator.rb30
-rw-r--r--maze_solver.rb1
2 files changed, 15 insertions, 16 deletions
diff --git a/maze_generator.rb b/maze_generator.rb
index 951e0d7..ef8e21b 100644
--- a/maze_generator.rb
+++ b/maze_generator.rb
@@ -16,6 +16,14 @@ class MazeGenerator
@visitedTiles = Array.new(width) { Array.new(height) { false } }
end
+ def current_algorithm
+ if @stack.length < @stack_threshold
+ :dfs
+ else
+ :bfs
+ end
+ end
+
def generate!()
start_pos = Pos.new(rand(@maze.width), rand(@maze.height))
@visitedTiles[start_pos.x][start_pos.y] = true
@@ -39,13 +47,7 @@ class MazeGenerator
puts
TUI::Screen.reset_line
puts " Stack size: #{@stack.length}"
- if @stack.length < @stack_threshold
- TUI::Screen.reset_line
- puts " Current algorithm: Depth-first search"
- else
- TUI::Screen.reset_line
- puts " Current algorithm: Breath-first search"
- end
+ puts " Current algorithm: #{current_algorithm}"
end
end
@@ -55,10 +57,9 @@ class MazeGenerator
end
def step()
- current_tile = if @stack.length < @stack_threshold
- @stack.last
- else
- @stack.first
+ current_tile = case current_algorithm
+ when :dfs then @stack.last
+ when :bfs then @stack.first
end
neighbors = @maze.neighbors(current_tile)
@@ -67,10 +68,9 @@ class MazeGenerator
end
if neighbors.empty?
- if @stack.length < @stack_threshold
- @stack.pop()
- else
- @stack.shift()
+ case current_algorithm
+ when :dfs then @stack.pop()
+ when :bfs then @stack.shift()
end
else
randomNeighbor = neighbors.sample
diff --git a/maze_solver.rb b/maze_solver.rb
index 2f75111..031551b 100644
--- a/maze_solver.rb
+++ b/maze_solver.rb
@@ -33,7 +33,6 @@ class MazeSolver
puts
TUI::Screen.reset_line
puts " Stack size: #{@stack.length}"
- puts " Current algorithm: Depth-first search"
end
end