Class: Mazemap::Pathfinder
- Inherits:
-
Object
- Object
- Mazemap::Pathfinder
- Defined in:
- lib/mazemap/pathfinder.rb
Overview
Pathfinder object class
Constant Summary collapse
- VERTEX_VALUES =
{ ' ' => Graph::BLANK, '*' => Graph::INF, 'A' => Graph::START, 'B' => Graph::FINISH }
Instance Attribute Summary collapse
-
#cols ⇒ Integer
readonly
Number of the cols.
-
#finish ⇒ Integer
readonly
Finish vertex coords.
-
#lines ⇒ Array
readonly
Maze representation.
-
#rows ⇒ Integer
readonly
Number of the rows.
-
#start ⇒ Integer
readonly
Start vertex coords.
Instance Method Summary collapse
-
#initialize(filename) ⇒ Pathfinder
constructor
Initializes Pathfinder, sets @lines, @rows and @cols.
-
#solution ⇒ Object
Starts a search for the shortest path.
Constructor Details
#initialize(filename) ⇒ Pathfinder
Initializes Pathfinder, sets @lines, @rows and @cols
32 33 34 35 36 37 38 |
# 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 |
Instance Attribute Details
#cols ⇒ Integer (readonly)
Returns number of the cols.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mazemap/pathfinder.rb', line 15 class Pathfinder attr_reader :lines attr_reader :rows attr_reader :cols attr_reader :start attr_reader :finish VERTEX_VALUES = { ' ' => Graph::BLANK, '*' => Graph::INF, 'A' => Graph::START, 'B' => Graph::FINISH } # Initializes Pathfinder, sets @lines, @rows and @cols # # @param filename [String] path to file with maze def initialize(filename) if filename @lines = IO.readlines(filename).map(&:chomp) @rows = lines.length @cols = lines.first.length end end # Starts a search for the shortest path 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 # Sets start and finish vertex coords # # @param graph_map [Array] flatterned graph map 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 # Converts letter according to maze format # # @param letter [String] letter to convert def convert_letter(letter) VERTEX_VALUES[letter] || letter end # Parses the maze map and converts it to the internal format def parse_graph_map lines.map do |line| line.split('').map do |letter| convert_letter(letter) end end end end |
#finish ⇒ Integer (readonly)
Returns finish vertex coords.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mazemap/pathfinder.rb', line 15 class Pathfinder attr_reader :lines attr_reader :rows attr_reader :cols attr_reader :start attr_reader :finish VERTEX_VALUES = { ' ' => Graph::BLANK, '*' => Graph::INF, 'A' => Graph::START, 'B' => Graph::FINISH } # Initializes Pathfinder, sets @lines, @rows and @cols # # @param filename [String] path to file with maze def initialize(filename) if filename @lines = IO.readlines(filename).map(&:chomp) @rows = lines.length @cols = lines.first.length end end # Starts a search for the shortest path 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 # Sets start and finish vertex coords # # @param graph_map [Array] flatterned graph map 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 # Converts letter according to maze format # # @param letter [String] letter to convert def convert_letter(letter) VERTEX_VALUES[letter] || letter end # Parses the maze map and converts it to the internal format def parse_graph_map lines.map do |line| line.split('').map do |letter| convert_letter(letter) end end end end |
#lines ⇒ Array (readonly)
Returns maze representation.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mazemap/pathfinder.rb', line 15 class Pathfinder attr_reader :lines attr_reader :rows attr_reader :cols attr_reader :start attr_reader :finish VERTEX_VALUES = { ' ' => Graph::BLANK, '*' => Graph::INF, 'A' => Graph::START, 'B' => Graph::FINISH } # Initializes Pathfinder, sets @lines, @rows and @cols # # @param filename [String] path to file with maze def initialize(filename) if filename @lines = IO.readlines(filename).map(&:chomp) @rows = lines.length @cols = lines.first.length end end # Starts a search for the shortest path 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 # Sets start and finish vertex coords # # @param graph_map [Array] flatterned graph map 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 # Converts letter according to maze format # # @param letter [String] letter to convert def convert_letter(letter) VERTEX_VALUES[letter] || letter end # Parses the maze map and converts it to the internal format def parse_graph_map lines.map do |line| line.split('').map do |letter| convert_letter(letter) end end end end |
#rows ⇒ Integer (readonly)
Returns number of the rows.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mazemap/pathfinder.rb', line 15 class Pathfinder attr_reader :lines attr_reader :rows attr_reader :cols attr_reader :start attr_reader :finish VERTEX_VALUES = { ' ' => Graph::BLANK, '*' => Graph::INF, 'A' => Graph::START, 'B' => Graph::FINISH } # Initializes Pathfinder, sets @lines, @rows and @cols # # @param filename [String] path to file with maze def initialize(filename) if filename @lines = IO.readlines(filename).map(&:chomp) @rows = lines.length @cols = lines.first.length end end # Starts a search for the shortest path 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 # Sets start and finish vertex coords # # @param graph_map [Array] flatterned graph map 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 # Converts letter according to maze format # # @param letter [String] letter to convert def convert_letter(letter) VERTEX_VALUES[letter] || letter end # Parses the maze map and converts it to the internal format def parse_graph_map lines.map do |line| line.split('').map do |letter| convert_letter(letter) end end end end |
#start ⇒ Integer (readonly)
Returns start vertex coords.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mazemap/pathfinder.rb', line 15 class Pathfinder attr_reader :lines attr_reader :rows attr_reader :cols attr_reader :start attr_reader :finish VERTEX_VALUES = { ' ' => Graph::BLANK, '*' => Graph::INF, 'A' => Graph::START, 'B' => Graph::FINISH } # Initializes Pathfinder, sets @lines, @rows and @cols # # @param filename [String] path to file with maze def initialize(filename) if filename @lines = IO.readlines(filename).map(&:chomp) @rows = lines.length @cols = lines.first.length end end # Starts a search for the shortest path 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 # Sets start and finish vertex coords # # @param graph_map [Array] flatterned graph map 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 # Converts letter according to maze format # # @param letter [String] letter to convert def convert_letter(letter) VERTEX_VALUES[letter] || letter end # Parses the maze map and converts it to the internal format def parse_graph_map lines.map do |line| line.split('').map do |letter| convert_letter(letter) end end end end |