From 30226e6ea3de0b36fb3e1f34800befc94b8dc167 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Sun, 22 Apr 2018 17:16:47 +0200 Subject: Refactor algorithm switch --- maze_generator.rb | 30 +++++++++++++++--------------- maze_solver.rb | 1 - 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 -- cgit v1.1