class Mazemap::Pathfinder
Pathfinder
object class
@author Evgenii Shevchenko @!attribute [r] lines
@return [Array] maze representation
@!attribute [r] rows
@return [Integer] number of the rows
@!attribute [r] cols
@return [Integer] number of the cols
@!attribute [r] start
@return [Integer] start vertex coords
@!attribute [r] finish
@return [Integer] finish vertex coords
Constants
- VERTEX_VALUES
Attributes
cols[R]
finish[R]
lines[R]
rows[R]
start[R]
Public Class Methods
new(filename)
click to toggle source
Initializes Pathfinder
, sets @lines, @rows and @cols
@param filename [String] path to file with maze
# File lib/mazemap/pathfinder.rb, line 32 def initialize(filename) if filename @lines = IO.readlines(filename).map(&:chomp) @rows = lines.length @cols = lines.first.length end end
Public Instance Methods
solution()
click to toggle source
Starts a search for the shortest path
# File lib/mazemap/pathfinder.rb, line 41 def solution graph_map = parse_graph_map.flatten set_route(graph_map) graph = Graph.new(rows, cols, graph_map) graph.shortest_path(start, finish).map(&:reverse).reverse end
Private Instance Methods
convert_letter(letter)
click to toggle source
Converts letter according to maze format
@param letter [String] letter to convert
# File lib/mazemap/pathfinder.rb, line 65 def convert_letter(letter) VERTEX_VALUES[letter] || letter end
parse_graph_map()
click to toggle source
Parses the maze map and converts it to the internal format
# File lib/mazemap/pathfinder.rb, line 70 def parse_graph_map lines.map do |line| line.split('').map do |letter| convert_letter(letter) end end end
set_route(graph_map)
click to toggle source
Sets start and finish vertex coords
@param graph_map [Array] flatterned graph map
# File lib/mazemap/pathfinder.rb, line 53 def set_route(graph_map) start_element = graph_map.index(Graph::START) finish_element = graph_map.index(Graph::FINISH) graph_map[start_element] = Graph::BLANK graph_map[finish_element] = Graph::BLANK @start = [start_element / cols, start_element % cols] @finish = [finish_element / cols, finish_element % cols] end