Class: MazeCrosser::MazeSolver

Inherits:
Object
  • Object
show all
Defined in:
lib/maze_crosser/maze_solver.rb

Overview

Class responsible for solving a maze, by using a given algorithm or a cache.

maze_solver = MazeSolver.new(maze, algorithm, cache_provider) maze_solver.solve

Instance Method Summary collapse

Constructor Details

#initialize(maze, algorithm, cache_provider) ⇒ MazeSolver

Returns a new instance of MazeSolver.



8
9
10
11
12
# File 'lib/maze_crosser/maze_solver.rb', line 8

def initialize(maze, algorithm, cache_provider)
  @algorithm = algorithm
  @cache_provider = cache_provider
  @maze = maze
end

Instance Method Details

#solveObject



14
15
16
17
18
19
20
21
22
# File 'lib/maze_crosser/maze_solver.rb', line 14

def solve
  if @cache_provider.get_solution(@maze.grid)
    return @cache_provider.get_solution(@maze.grid)
  end

  solution = @algorithm.run
  @cache_provider.add(@maze.grid, solution)
  solution
end