Class: Theseus::Formatters::ASCII
- Inherits:
-
Object
- Object
- Theseus::Formatters::ASCII
- Defined in:
- lib/theseus/formatters/ascii.rb,
lib/theseus/formatters/ascii/delta.rb,
lib/theseus/formatters/ascii/sigma.rb,
lib/theseus/formatters/ascii/upsilon.rb,
lib/theseus/formatters/ascii/orthogonal.rb
Overview
ASCII formatters render a maze as ASCII art. The ASCII representation is intended mostly to give you a “quick look” at the maze, and will rarely suffice for showing more than an overview of the maze’s shape.
This is the abstract superclass of the ASCII formatters, and provides helpers for writing to a textual “canvas”.
Direct Known Subclasses
Defined Under Namespace
Classes: Delta, Orthogonal, Sigma, Upsilon
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
The height of the canvas.
-
#width ⇒ Object
readonly
The width of the canvas.
Instance Method Summary collapse
-
#[](x, y) ⇒ Object
Returns the character at the given coordinates.
-
#[]=(x, y, char) ⇒ Object
Sets the character at the given coordinates.
-
#initialize(width, height) ⇒ ASCII
constructor
Create a new ASCII canvas with the given width and height.
-
#to_s ⇒ Object
Returns the canvas as a multiline string, suitable for displaying.
Constructor Details
#initialize(width, height) ⇒ ASCII
Create a new ASCII canvas with the given width and height. The canvas is initially blank (set to whitespace).
20 21 22 23 |
# File 'lib/theseus/formatters/ascii.rb', line 20 def initialize(width, height) @width, @height = width, height @chars = Array.new(height) { Array.new(width, " ") } end |
Instance Attribute Details
#height ⇒ Object (readonly)
The height of the canvas. This corresponds to, but is not necessarily the same as, the height of the maze.
16 17 18 |
# File 'lib/theseus/formatters/ascii.rb', line 16 def height @height end |
#width ⇒ Object (readonly)
The width of the canvas. This corresponds to, but is not necessarily the same as, the width of the maze.
12 13 14 |
# File 'lib/theseus/formatters/ascii.rb', line 12 def width @width end |
Instance Method Details
#[](x, y) ⇒ Object
Returns the character at the given coordinates.
26 27 28 |
# File 'lib/theseus/formatters/ascii.rb', line 26 def [](x, y) @chars[y][x] end |
#[]=(x, y, char) ⇒ Object
Sets the character at the given coordinates.
31 32 33 |
# File 'lib/theseus/formatters/ascii.rb', line 31 def []=(x, y, char) @chars[y][x] = char end |
#to_s ⇒ Object
Returns the canvas as a multiline string, suitable for displaying.
36 37 38 |
# File 'lib/theseus/formatters/ascii.rb', line 36 def to_s @chars.map { |row| row.join }.join("\n") end |