Class: Mazemap::Pathfinder

Inherits:
Object
  • Object
show all
Defined in:
lib/mazemap/pathfinder.rb

Overview

Pathfinder object class

Author:

  • Evgenii Shevchenko

Since:

  • 0.0.1

Constant Summary collapse

VERTEX_VALUES =

Since:

  • 0.0.1

{
	' ' => Graph::BLANK,
	'*' => Graph::INF,
	'A' => Graph::START,
	'B' => Graph::FINISH
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Pathfinder

Initializes Pathfinder, sets @lines, @rows and @cols

Parameters:

  • filename (String)

    path to file with maze

Since:

  • 0.0.1



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

#colsInteger (readonly)

Returns number of the cols.

Returns:

  • (Integer)

    number of the cols

Since:

  • 0.0.1



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

#finishInteger (readonly)

Returns finish vertex coords.

Returns:

  • (Integer)

    finish vertex coords

Since:

  • 0.0.1



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

#linesArray (readonly)

Returns maze representation.

Returns:

  • (Array)

    maze representation

Since:

  • 0.0.1



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

#rowsInteger (readonly)

Returns number of the rows.

Returns:

  • (Integer)

    number of the rows

Since:

  • 0.0.1



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

#startInteger (readonly)

Returns start vertex coords.

Returns:

  • (Integer)

    start vertex coords

Since:

  • 0.0.1



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

Instance Method Details

#solutionObject

Starts a search for the shortest path

Since:

  • 0.0.1



41
42
43
44
45
46
# 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