Class: LabyrinthSolver::Solver
- Inherits:
-
Object
- Object
- LabyrinthSolver::Solver
- Extended by:
- Forwardable
- Defined in:
- lib/subparry_labyrinth_solver/solver.rb
Overview
Takes a labyrinth and attempts to solve it saving the path taken
Constant Summary collapse
- OPPOSITE_DIRECTIONS =
{ up: :down, right: :left, down: :up, left: :right }.freeze
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #go_next ⇒ Object
-
#initialize(labyrinth) ⇒ Solver
constructor
A new instance of Solver.
- #solve ⇒ Object
Constructor Details
#initialize(labyrinth) ⇒ Solver
Returns a new instance of Solver.
15 16 17 18 19 20 |
# File 'lib/subparry_labyrinth_solver/solver.rb', line 15 def initialize labyrinth raise ArgumentError unless labyrinth.instance_of? Labyrinth @labyrinth = labyrinth @path = [] end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/subparry_labyrinth_solver/solver.rb', line 7 def path @path end |
Instance Method Details
#go_next ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/subparry_labyrinth_solver/solver.rb', line 22 def go_next next_dir = OPPOSITE_DIRECTIONS.find { |dir, opp| @labyrinth.open?(dir) && opp != @path.last } return dead_end unless next_dir go(next_dir.first) @path.push(next_dir.first) end |
#solve ⇒ Object
30 31 32 |
# File 'lib/subparry_labyrinth_solver/solver.rb', line 30 def solve go_next until cheese? end |