Class: Amaze::Formatter::ASCII
- Inherits:
-
Object
- Object
- Amaze::Formatter::ASCII
- Defined in:
- lib/amaze/formatter/ascii.rb
Defined Under Namespace
Classes: Delta, Ortho, Polar, Sigma, Upsilon
Instance Attribute Summary collapse
-
#grid ⇒ Object
readonly
The grid.
-
#options ⇒ Object
readonly
Options for the ASCII renderer.
Instance Method Summary collapse
- #ansi_clear ⇒ Object
-
#cell_size ⇒ Object
The size of the cell.
- #char ⇒ Object
- #clear ⇒ Object
- #distance(cell) ⇒ Object
- #distance_color(cell) ⇒ Object
-
#distances ⇒ Object
Distances.
-
#grid_color ⇒ Object
The color of the grid.
-
#initialize(grid, options = {}) ⇒ ASCII
constructor
A new instance of ASCII.
- #path?(direction, cell) ⇒ Boolean
- #path_cell?(cell) ⇒ Boolean
-
#path_color ⇒ Object
The color used to draw the solution or the longest path.
- #render ⇒ Object
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, ={} @grid = grid @options = end |
Instance Attribute Details
#grid ⇒ Object (readonly)
The grid
13 14 15 |
# File 'lib/amaze/formatter/ascii.rb', line 13 def grid @grid end |
#options ⇒ Object (readonly)
Options for the ASCII renderer
16 17 18 |
# File 'lib/amaze/formatter/ascii.rb', line 16 def @options end |
Instance Method Details
#ansi_clear ⇒ Object
45 46 47 |
# File 'lib/amaze/formatter/ascii.rb', line 45 def ansi_clear "\e[H\e[2J" end |
#cell_size ⇒ Object
The size of the cell
54 55 56 |
# File 'lib/amaze/formatter/ascii.rb', line 54 def cell_size [:cell_size] || 1 end |
#char ⇒ Object
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 |
#clear ⇒ Object
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 |
#distances ⇒ Object
Distances
73 74 75 |
# File 'lib/amaze/formatter/ascii.rb', line 73 def distances [:distances] end |
#grid_color ⇒ Object
The color of the grid
59 60 61 |
# File 'lib/amaze/formatter/ascii.rb', line 59 def grid_color [:grid_color] || :white end |
#path?(direction, cell) ⇒ 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
68 69 70 |
# File 'lib/amaze/formatter/ascii.rb', line 68 def path_cell? cell Array([:path_cells]).include? cell end |
#path_color ⇒ Object
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 [:path_color] || :blue end |
#render ⇒ Object
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 |