class Theseus::Formatters::ASCII

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”.

Attributes

height[R]

The height of the canvas. This corresponds to, but is not necessarily the same as, the height of the maze.

width[R]

The width of the canvas. This corresponds to, but is not necessarily the same as, the width of the maze.

Public Class Methods

new(width, height) click to toggle source

Create a new ASCII canvas with the given width and height. The canvas is initially blank (set to whitespace).

# File lib/theseus/formatters/ascii.rb, line 20
def initialize(width, height)
  @width, @height = width, height
  @chars = Array.new(height) { Array.new(width, " ") }
end

Public Instance Methods

[](x, y) click to toggle source

Returns the character at the given coordinates.

# File lib/theseus/formatters/ascii.rb, line 26
def [](x, y)
  @chars[y][x]
end
[]=(x, y, char) click to toggle source

Sets the character at the given coordinates.

# File lib/theseus/formatters/ascii.rb, line 31
def []=(x, y, char)
  @chars[y][x] = char
end
to_s() click to toggle source

Returns the canvas as a multiline string, suitable for displaying.

# File lib/theseus/formatters/ascii.rb, line 36
def to_s
  @chars.map { |row| row.join }.join("\n")
end