Class: Theseus::Formatters::ASCII::Orthogonal
- Inherits:
-
Theseus::Formatters::ASCII
- Object
- Theseus::Formatters::ASCII
- Theseus::Formatters::ASCII::Orthogonal
- Defined in:
- lib/theseus/formatters/ascii/orthogonal.rb
Overview
Renders an OrthogonalMaze to an ASCII representation.
The ASCII formatter for the OrthogonalMaze actually supports three different output types:
- :plain
-
Uses standard 7-bit ASCII characters. Width is 2x+1, height is y+1. This mode cannot render weave mazes without significant ambiguity.
- :unicode
-
Uses unicode characters to render cleaner lines. Width is 3x, height is 2y. This mode has sufficient detail to correctly render mazes with weave!
- :lines
-
Draws passages as lines, using unicode characters. Width is x, height is y. This mode can render weave mazes, but with some ambiguity.
The :plain mode is the default, but you can specify a different one using the :mode option.
You shouldn’t ever need to instantiate this class directly. Rather, use OrthogonalMaze#to(:ascii) (or OrthogonalMaze#to_s to get the string directly).
Instance Attribute Summary
Attributes inherited from Theseus::Formatters::ASCII
Class Method Summary collapse
-
.dimensions_for(maze, mode) ⇒ Object
Returns the dimensions of the given maze, rendered in the given mode.
Instance Method Summary collapse
-
#initialize(maze, options = {}) ⇒ Orthogonal
constructor
Create and return a fully initialized ASCII canvas.
Methods inherited from Theseus::Formatters::ASCII
Constructor Details
#initialize(maze, options = {}) ⇒ Orthogonal
Create and return a fully initialized ASCII canvas. The options parameter may specify a :mode parameter, as described in the documentation for this class.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/theseus/formatters/ascii/orthogonal.rb', line 47 def initialize(maze, ={}) mode = [:mode] || :plain width, height = self.class.dimensions_for(maze, mode) super(width, height) maze.height.times do |y| length = maze.row_length(y) length.times do |x| case mode when :plain then draw_plain_cell(maze, x, y) when :unicode then draw_unicode_cell(maze, x, y) when :lines then draw_line_cell(maze, x, y) end end end end |
Class Method Details
.dimensions_for(maze, mode) ⇒ Object
Returns the dimensions of the given maze, rendered in the given mode. The mode must be :plain, :unicode, or :lines.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/theseus/formatters/ascii/orthogonal.rb', line 31 def self.dimensions_for(maze, mode) case mode when :plain, nil then [maze.width * 2 + 1, maze.height + 1] when :unicode then [maze.width * 3, maze.height * 2] when :lines then [maze.width, maze.height] else abort "unknown mode #{mode.inspect}" end end |