Class: MazeSolution
- Inherits:
-
Object
- Object
- MazeSolution
- Defined in:
- lib/my_own_maze/generator/maze_solution.rb
Constant Summary collapse
- DIRS =
{ 'l' => [-1, 0], 'r' => [1, 0], 'u' => [0, -1], 'd' => [0, 1] }
Instance Method Summary collapse
- #calculate(x, y) ⇒ Object
-
#initialize(maze) ⇒ MazeSolution
constructor
A new instance of MazeSolution.
Constructor Details
#initialize(maze) ⇒ MazeSolution
Returns a new instance of MazeSolution.
9 10 11 12 13 |
# File 'lib/my_own_maze/generator/maze_solution.rb', line 9 def initialize(maze) @maze = maze @points = [] init_dirs end |
Instance Method Details
#calculate(x, y) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/my_own_maze/generator/maze_solution.rb', line 15 def calculate(x, y) @points = [@maze.find_point(x, y)] while !@maze.is_end?(@points.last.x, @points.last.y) start_point = @points.last result = false while !result && @dirs[start_point].size > 0 next_step = @dirs[start_point].last @dirs[start_point].pop x, y = start_point.x + DIRS[next_step][0], start_point.y + DIRS[next_step][1] next_point = @maze.find_point(x, y) if next_point && !has_point?(x, y) @points << next_point result = true end end @points.pop unless result end @points[1...@points.size] end |