Class: Amaze::Formatter::ASCII

Inherits:
Object
  • Object
show all
Defined in:
lib/amaze/formatter/ascii.rb

Direct Known Subclasses

Delta, Ortho, Polar, Sigma, Upsilon

Defined Under Namespace

Classes: Delta, Ortho, Polar, Sigma, Upsilon

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grid, options = {}) ⇒ ASCII

Returns a new instance of ASCII.



18
19
20
21
# File 'lib/amaze/formatter/ascii.rb', line 18

def initialize grid, options={}
  @grid = grid
  @options = options
end

Instance Attribute Details

#gridObject (readonly)

The grid



13
14
15
# File 'lib/amaze/formatter/ascii.rb', line 13

def grid
  @grid
end

#optionsObject (readonly)

Options for the ASCII renderer



16
17
18
# File 'lib/amaze/formatter/ascii.rb', line 16

def options
  @options
end

Instance Method Details

#ansi_clearObject



45
46
47
# File 'lib/amaze/formatter/ascii.rb', line 45

def ansi_clear
  "\e[H\e[2J"
end

#cell_sizeObject

The size of the cell



54
55
56
# File 'lib/amaze/formatter/ascii.rb', line 54

def cell_size
  options[:cell_size] || 1
end

#charObject



23
24
25
26
27
28
29
# File 'lib/amaze/formatter/ascii.rb', line 23

def char
  @char ||= Array.new(char_array_height) do |x|
    Array.new(char_array_width) do |y|
      clear
    end
  end
end

#clearObject



49
50
51
# File 'lib/amaze/formatter/ascii.rb', line 49

def clear
  ' '
end

#distance(cell) ⇒ Object



77
78
79
80
# File 'lib/amaze/formatter/ascii.rb', line 77

def distance cell
  # .to_s(36) => 0..9a..z
  distances && distances[cell] ? distances[cell].to_s(36).upcase : ' '
end

#distance_color(cell) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/amaze/formatter/ascii.rb', line 82

def distance_color cell
  return @options[:distances_color] if @options[:distances_color]
  _, max = distances.max
  high = (255-47).to_f / max * distances[cell] + 47
  low = high / 4
  p [:low, low] unless (0..255).include? low
  p [:high, high] unless (0..255).include? high
  [0,low,high]
end

#distancesObject

Distances



73
74
75
# File 'lib/amaze/formatter/ascii.rb', line 73

def distances
  options[:distances]
end

#grid_colorObject

The color of the grid



59
60
61
# File 'lib/amaze/formatter/ascii.rb', line 59

def grid_color
  options[:grid_color] || :white
end

#path?(direction, cell) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/amaze/formatter/ascii.rb', line 41

def path? direction, cell
  cell.linked?(cell.send(direction)) && path_cell?(cell.send(direction))
end

#path_cell?(cell) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/amaze/formatter/ascii.rb', line 68

def path_cell? cell
  Array(options[:path_cells]).include? cell
end

#path_colorObject

The color used to draw the solution or the longest path



64
65
66
# File 'lib/amaze/formatter/ascii.rb', line 64

def path_color
  options[:path_color] || :blue
end

#renderObject



31
32
33
34
35
36
37
38
39
# File 'lib/amaze/formatter/ascii.rb', line 31

def render
  grid.each_cell do |cell|
    draw_cell cell
    draw_content cell if distances
    draw_path cell if path_cell? cell
  end
  
  ansi_clear + char.map{|l| l.join }.join("\n")
end