Class: Theseus::Maze

Inherits:
Object
  • Object
show all
Defined in:
lib/theseus/maze.rb

Overview

Theseus::Maze is an abstract class, intended to act solely as a superclass for specific maze types. Subclasses include OrthogonalMaze, DeltaMaze, SigmaMaze, and UpsilonMaze.

Each cell in the maze is a bitfield. The bits that are set indicate which passages exist leading AWAY from this cell. Bits in the low byte (corresponding to the PRIMARY bitmask) represent passages on the normal plane. Bits in the high byte (corresponding to the UNDER bitmask) represent passages that are passing under this cell. (Under/over passages are controlled via the #weave setting, and are not supported by all maze types.)

Direct Known Subclasses

DeltaMaze, OrthogonalMaze, SigmaMaze, UpsilonMaze

Constant Summary collapse

N =

North

0x01
S =

South

0x02
E =

East

0x04
W =

West

0x08
NW =

Northwest

0x10
NE =

Northeast

0x20
SW =

Southwest

0x40
SE =

Southeast

0x80
PRIMARY =

bitmask identifying directional bits on the primary plane

0x000000FF
UNDER =

bitmask identifying directional bits under the primary plane

0x0000FF00
RESERVED =

bits reserved for use by individual algorithm implementations

0xFFFF0000
UNDER_SHIFT =

The size of the PRIMARY bitmask (e.g. how far to the left the UNDER bitmask is shifted).

8

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Maze

Creates and returns a new maze object. Note that the maze will not be generated; the maze is initially blank.

Many options are supported:

:width

The number of columns in the maze. Note that different maze types count columns and rows differently; you’ll want to see individual maze types for more info.

:height

The number of rows in the maze.

:algorithm

The maze algorithm to use. This should be a class, adhering to the interface described by Theseus::Algorithms::Base. It defaults to Theseus::Algorithms::RecursiveBacktracker.

:symmetry

The symmetry to be used when generating the maze. This defaults to :none, but may also be :x (to have the maze mirrored across the x-axis), :y (to mirror the maze across the y-axis), :xy (to mirror across both axes simultaneously), and :radial (to mirror the maze radially about the center). Some symmetry types may result in loops being added to the maze, regardless of the braid value (see the :braid parameter). (NOTE: not all maze types support symmetry equally.)

:randomness

An integer between 0 and 100 (inclusive) indicating how randomly the maze is generated. A 0 means that the maze passages will prefer to go straight whenever possible. A 100 means the passages will choose random directions as often as possible.

:mask

An instance of Theseus::Mask (or something that acts similarly). This can be used to constrain the maze so that it fills or avoids specific areas, so that shapes and patterns can be made. (NOTE: not all algorithms support masks.)

:weave

An integer between 0 and 100 (inclusive) indicating how frequently passages move under or over other passages. A 0 means the passages will never move over/under other passages, while a 100 means they will do so as often as possible. (NOTE: not all maze types and algorithms support weaving.)

:braid

An integer between 0 and 100 (inclusive) representing the percentage of dead-ends that should be removed after the maze has been generated. Dead-ends are removed by extending them in some direction until they join with another passage. This will introduce loops into the maze, making it “multiply-connected”. A braid value of 0 will always result in a “perfect” maze (with no loops), while a value of 100 will result in a maze with no dead-ends.

:wrap

Indicates which edges of the maze should wrap around. :x will cause the left and right edges to wrap, and :y will cause the top and bottom edges to wrap. You can specify :xy to wrap both left-to-right and top-to-bottom. The default is :none (for no wrapping).

:entrance

A 2-tuple indicating from where the maze is entered. By default, the maze’s entrance will be the upper-left-most point. Note that it may lie outside the bounds of the maze by one cell (e.g. [-1,0]), indicating that the entrance is on the very edge of the maze.

:exit

A 2-tuple indicating from where the maze is exited. By default, the maze’s entrance will be the lower-right-most point. Note that it may lie outside the bounds of the maze by one cell (e.g. [width,height-1]), indicating that the exit is on the very edge of the maze.

:prebuilt

Sometimes, you may want the new maze to be considered to be generated, but not actually have anything generated into it. You can set the :prebuilt parameter to true in this case, allowing you to then set the contents of the maze by hand, using the #[]= method.



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/theseus/maze.rb', line 176

def initialize(options={})
  @deadends = nil

  @width = (options[:width] || 10).to_i
  @height = (options[:height] || 10).to_i

  @symmetry = (options[:symmetry] || :none).to_sym
  configure_symmetry

  @randomness = options[:randomness] || 100
  @mask = options[:mask] || TransparentMask.new
  @weave = options[:weave].to_i
  @braid = options[:braid].to_i
  @wrap = options[:wrap] || :none

  @cells = setup_grid or raise "expected #setup_grid to return the new grid"

  @entrance = options[:entrance] || default_entrance
  @exit = options[:exit] || default_exit

  algorithm_class = options[:algorithm] || Algorithms::RecursiveBacktracker
  @algorithm = algorithm_class.new(self, options)

  @generated = options[:prebuilt]
end

Instance Attribute Details

#algorithmObject (readonly)

The algorithm object used to generate this maze. Defaults to an instance of Algorithms::RecursiveBacktracker.



41
42
43
# File 'lib/theseus/maze.rb', line 41

def algorithm
  @algorithm
end

#braidObject (readonly)

An integer between 0 and 100 (inclusive), signifying the percentage of deadends in the maze that will be extended in some direction until they join with an existing passage. This will create loops in the graph. Thus, 0 is a “perfect” maze (with no loops), and 100 is a maze that is totally multiply-connected, with no dead-ends.



70
71
72
# File 'lib/theseus/maze.rb', line 70

def braid
  @braid
end

#entranceObject (readonly)

A 2-tuple (array) indicating the x and y coordinates where the maze should be entered. This is used primarly when generating the solution to the maze, and generally defaults to the upper-left corner.



98
99
100
# File 'lib/theseus/maze.rb', line 98

def entrance
  @entrance
end

#exitObject (readonly)

A 2-tuple (array) indicating the x and y coordinates where the maze should be exited. This is used primarly when generating the solution to the maze, and generally defaults to the lower-right corner.



103
104
105
# File 'lib/theseus/maze.rb', line 103

def exit
  @exit
end

#heightObject (readonly)

The height of the maze (number of rows).



51
52
53
# File 'lib/theseus/maze.rb', line 51

def height
  @height
end

#maskObject (readonly)

A Theseus::Mask (or similar) instance, that is used by the algorithm to determine which cells in the space are allowed. This lets you create mazes that fill shapes, or flow around patterns.



85
86
87
# File 'lib/theseus/maze.rb', line 85

def mask
  @mask
end

#randomnessObject (readonly)

An integer between 0 and 100 (inclusive). 0 means passages will only change direction when they encounter a barrier they cannot move through (or under). 100 means that as passages are built, a new direction will always be randomly chosen for each step of the algorithm.



57
58
59
# File 'lib/theseus/maze.rb', line 57

def randomness
  @randomness
end

#symmetryObject (readonly)

One of :none, :x, :y, :xy, or :radial. Note that not all maze types support symmetry. The :x symmetry means the maze will be mirrored across the x axis. Similarly, :y symmetry means the maze will be mirrored across the y axis. :xy symmetry causes the maze to be mirrored across both axes, and :radial symmetry causes the maze to be mirrored radially about the center of the maze.



93
94
95
# File 'lib/theseus/maze.rb', line 93

def symmetry
  @symmetry
end

#weaveObject (readonly)

An integer between 0 and 100 (inclusive). 0 means passages will never move over or under existing passages. 100 means whenever possible, passages will move over or under existing passages. Note that not all maze types support weaving.



63
64
65
# File 'lib/theseus/maze.rb', line 63

def weave
  @weave
end

#widthObject (readonly)

The width of the maze (number of columns).

In general, it is safest to use the #row_length method for a particular row, since it is theoretically possible for a maze subclass to describe a different width for each row.



48
49
50
# File 'lib/theseus/maze.rb', line 48

def width
  @width
end

#wrapObject (readonly)

One of :none, :x, :y, or :xy, indicating which boundaries the maze should wrap around. The default is :none, indicating no wrapping. If :x, the maze will wrap around the left and right edges. If :y, the maze will wrap around the top and bottom edges. If :xy, the maze will wrap around both edges.

A maze that wraps in a single direction may be mapped onto a cylinder. A maze that wraps in both x and y may be mapped onto a torus.



80
81
82
# File 'lib/theseus/maze.rb', line 80

def wrap
  @wrap
end

Class Method Details

.generate(options = {}) ⇒ Object

A short-hand method for creating a new maze object and causing it to be generated, in one step. Returns the newly generated maze.



107
108
109
# File 'lib/theseus/maze.rb', line 107

def self.generate(options={})
  new(options).generate!
end

Instance Method Details

#[](x, y) ⇒ Object

Returns the bitfield for the cell at the given (x,y) coordinate.



257
258
259
# File 'lib/theseus/maze.rb', line 257

def [](x,y)
  @cells[y][x]
end

#[]=(x, y, value) ⇒ Object

Sets the bitfield for the cell at the given (x,y) coordinate.



262
263
264
# File 'lib/theseus/maze.rb', line 262

def []=(x,y,value)
  @cells[y][x] = value
end

#add_opening_from(point) ⇒ Object

If point is already located at a valid point within the maze, this does nothing. Otherwise, it examines the potential exits from the given point and looks for the first one that leads immediately to a valid point internal to the maze. When it finds one, it adds a passage to that cell leading to point. If no such adjacent cell exists, this method silently does nothing.



538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/theseus/maze.rb', line 538

def add_opening_from(point)
  x, y = point
  if valid?(x, y)
    # nothing to be done
  else
    potential_exits_at(x, y).each do |direction|
      nx, ny = move(x, y, direction)
      if valid?(nx, ny)
        @cells[ny][nx] |= opposite(direction)
        return
      end
    end
  end
end

#adjacent_point(point) ⇒ Object

If point is already located at a valid point withint he maze, this simply returns point. Otherwise, it examines the potential exits from the given point and looks for the first one that leads immediately to a valid point internal to the maze. When it finds one, it returns that point. If no such point exists, it returns nil.



558
559
560
561
562
563
564
565
566
567
568
# File 'lib/theseus/maze.rb', line 558

def adjacent_point(point)
  x, y = point
  if valid?(x, y)
    point
  else
    potential_exits_at(x, y).each do |direction|
      nx, ny = move(x, y, direction)
      return [nx, ny] if valid?(nx, ny)
    end
  end
end

#apply_move_at(x, y, direction) ⇒ Object

Applies a move in the given direction to the cell at (x,y). The direction parameter may also be :under, in which case the cell is left-shifted so as to move the existing passages to the UNDER plane.

This method also handles the application of symmetrical moves, in the case where #symmetry has been specified.

You’ll generally never call this method directly, except to construct grids yourself.



622
623
624
625
626
627
628
629
630
631
632
633
634
635
# File 'lib/theseus/maze.rb', line 622

def apply_move_at(x, y, direction)
  if direction == :under
    @cells[y][x] <<= UNDER_SHIFT
  else
    @cells[y][x] |= direction
  end

  case @symmetry
  when :x      then move_symmetrically_in_x(x, y, direction)
  when :y      then move_symmetrically_in_y(x, y, direction)
  when :xy     then move_symmetrically_in_xy(x, y, direction)
  when :radial then move_symmetrically_radially(x, y, direction)
  end
end

#clockwise(direction) ⇒ Object

Returns the direction that results by rotating the given direction 90 degrees in the clockwise direction. This will work even if the direction value is in the UNDER bitmask.



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/theseus/maze.rb', line 460

def clockwise(direction)
  if direction & UNDER != 0
    clockwise(direction >> UNDER_SHIFT) << UNDER_SHIFT
  else
    case direction
    when N  then E
    when E  then S
    when S  then W
    when W  then N
    when NW then NE
    when NE then SE
    when SE then SW
    when SW then NW
    end
  end
end

#counter_clockwise(direction) ⇒ Object

Returns the direction that results by rotating the given direction 90 degrees in the counter-clockwise direction. This will work even if the direction value is in the UNDER bitmask.



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/theseus/maze.rb', line 480

def counter_clockwise(direction)
  if direction & UNDER != 0
    counter_clockwise(direction >> UNDER_SHIFT) << UNDER_SHIFT
  else
    case direction
    when N  then W
    when W  then S
    when S  then E
    when E  then N
    when NW then SW
    when SW then SE
    when SE then NE
    when NE then NW
    end
  end
end

#dead?(cell) ⇒ Boolean

Returns true if the given cell is a dead-end. This considers only passages on the PRIMARY plane (the UNDER bits are ignored, because the current algorithm for generating mazes will never result in a dead-end that is underneath another passage).

Returns:

  • (Boolean)


526
527
528
529
530
# File 'lib/theseus/maze.rb', line 526

def dead?(cell)
  raw = cell & PRIMARY
  raw == N || raw == S || raw == E || raw == W ||
    raw == NE || raw == NW || raw == SE || raw == SW
end

#dead_endsObject

Returns a array of all dead-ends in the maze. Each element of the array is a 2-tuple containing the coordinates of a dead-end.



365
366
367
368
369
370
371
372
373
374
375
# File 'lib/theseus/maze.rb', line 365

def dead_ends
  dead_ends = []

  @cells.each_with_index do |row, y|
    row.each_with_index do |cell, x|
      dead_ends << [x, y] if dead?(cell)
    end
  end

  dead_ends
end

#dx(direction) ⇒ Object

Returns the change in x implied by the given direction.



498
499
500
501
502
503
504
# File 'lib/theseus/maze.rb', line 498

def dx(direction)
  case direction
  when E, NE, SE then 1
  when W, NW, SW then -1
  else 0
  end
end

#dy(direction) ⇒ Object

Returns the change in y implied by the given direction.



507
508
509
510
511
512
513
# File 'lib/theseus/maze.rb', line 507

def dy(direction)
  case direction
  when S, SE, SW then 1
  when N, NE, NW then -1
  else 0
  end
end

#finishObject

Since #exit may be external to the maze, #finish returns the cell adjacent to #exit that lies within the maze. If #exit is already internal to the maze, this method returns #exit. If #exit is not adjacent to any internal cell, this method returns nil.



304
305
306
# File 'lib/theseus/maze.rb', line 304

def finish
  adjacent_point(@exit)
end

#generate!Object

Generates the maze if it has not already been generated. This is essentially the same as calling #step repeatedly. If a block is given, it will be called after each step.



205
206
207
208
# File 'lib/theseus/maze.rb', line 205

def generate!
  yield if block_given? while step unless generated?
  self
end

#generated?Boolean

Returns true if the maze has been generated.

Returns:

  • (Boolean)


288
289
290
# File 'lib/theseus/maze.rb', line 288

def generated?
  @generated
end

#hmirror(direction) ⇒ Object

Returns the direction that is the horizontal mirror to the given direction. This will work even if the direction value is in the UNDER bitmask.



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/theseus/maze.rb', line 423

def hmirror(direction)
  if direction & UNDER != 0
    hmirror(direction >> UNDER_SHIFT) << UNDER_SHIFT
  else
    case direction
    when E  then W
    when W  then E
    when NW then NE
    when NE then NW
    when SW then SE
    when SE then SW
    else direction
    end
  end
end

#inspectObject

:nodoc:



664
665
666
667
668
# File 'lib/theseus/maze.rb', line 664

def inspect # :nodoc:
  "#<#{self.class.name}:0x%X %dx%d %s>" % [
    object_id, @width, @height,
    generated? ? "generated" : "not generated"]
end

#move(x, y, direction) ⇒ Object

Moves the given (x,y) coordinates a single step in the given direction. If wrapping in either x or y is active, the result will be mapped to the maze’s current bounds via modulo arithmetic. The resulting coordinates are returned as a 2-tuple.

Example:

x2, y2 = maze.move(x, y, Maze::W)


354
355
356
357
358
359
360
361
# File 'lib/theseus/maze.rb', line 354

def move(x, y, direction)
  nx, ny = x + dx(direction), y + dy(direction)

  ny %= height if wrap_y?
  nx %= row_length(ny) if wrap_x? && ny > 0 && ny < height

  [nx, ny]
end

#new_path(meta = {}) ⇒ Object

Creates a new Theseus::Path object based on this maze instance. This can be used to (for instance) create special areas of the maze or routes through the maze that you want to color specially. The following demonstrates setting a particular cell in the maze to a light-purple color:

path = maze.new_path(color: 0xff7fffff)
path.set([5,5])
maze.to(:png, paths: [path])


218
219
220
# File 'lib/theseus/maze.rb', line 218

def new_path(meta={})
  Path.new(self, meta)
end

#new_solver(options = {}) ⇒ Object

Instantiates and returns a new solver instance which encapsulates a solution algorithm. The options may contain the following keys:

:type

This defaults to :backtracker (for the Theseus::Solvers::Backtracker solver), but may also be set to :astar (for the Theseus::Solvers::Astar solver).

:a

A 2-tuple (defaulting to #start) that says where in the maze the solution should begin.

:b

A 2-tuple (defaulting to #finish) that says where in the maze the solution should finish.

The returned solver will not yet have generated the solution. Use Theseus::Solvers::Base#solve or Theseus::Solvers::Base#step to generate the solution.



236
237
238
239
240
241
242
243
244
245
246
# File 'lib/theseus/maze.rb', line 236

def new_solver(options={})
  type = options[:type] || :backtracker

  require "theseus/solvers/#{type}"
  klass = Theseus::Solvers.const_get(type.to_s.capitalize)

  a = options[:a] || start
  b = options[:b] || finish

  klass.new(self, a, b)
end

#opposite(direction) ⇒ Object

Returns the direction opposite to the given direction. This will work even if the direction value is in the UNDER bitmask.



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/theseus/maze.rb', line 404

def opposite(direction)
  if direction & UNDER != 0
    opposite(direction >> UNDER_SHIFT) << UNDER_SHIFT
  else
    case direction
    when N  then S
    when S  then N
    when E  then W
    when W  then E
    when NE then SW
    when NW then SE
    when SE then NW
    when SW then NE
    end
  end
end

#perform_weave(from_x, from_y, to_x, to_y, direction) ⇒ Object

:nodoc:



682
683
684
685
686
687
688
689
690
691
692
693
694
# File 'lib/theseus/maze.rb', line 682

def perform_weave(from_x, from_y, to_x, to_y, direction) #:nodoc:
  if rand(2) == 0 # move under existing passage
    apply_move_at(to_x, to_y, direction << UNDER_SHIFT)
    apply_move_at(to_x, to_y, opposite(direction) << UNDER_SHIFT)
  else # move over existing passage
    apply_move_at(to_x, to_y, :under)
    apply_move_at(to_x, to_y, direction)
    apply_move_at(to_x, to_y, opposite(direction))
  end

  nx, ny = move(to_x, to_y, direction)
  [nx, ny, direction]
end

#potential_exits_at(x, y) ⇒ Object

Returns an array of the possible exits for the cell at the given coordinates. Note that this does not take into account boundary conditions: a move in any of the returned directions may not actually be valid, and should be verified before being applied.

This is used primarily by subclasses to allow for different shaped cells (e.g. hexagonal cells for SigmaMaze, octagonal cells for UpsilonMaze).

Raises:

  • (NotImplementedError)


315
316
317
# File 'lib/theseus/maze.rb', line 315

def potential_exits_at(x, y)
  raise NotImplementedError, "subclasses must implement #potential_exits_at"
end

#relative_direction(from, to) ⇒ Object

Returns the direction of to relative to from. to and from are both points (2-tuples).



572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/theseus/maze.rb', line 572

def relative_direction(from, to)
  # first, look for the case where the maze wraps, and from and to
  # are on opposite sites of the grid.
  if wrap_x? && from[1] == to[1] && (from[0] == 0 || to[0] == 0) && (from[0] == @width-1 || to[0] == @width-1)
    if from[0] < to[0]
      W
    else
      E
    end
  elsif wrap_y? && from[0] == to[0] && (from[1] == 0 || to[1] == 0) && (from[1] == @height-1 || to[1] == @height-1)
    if from[1] < to[1]
      N
    else
      S
    end
  elsif from[0] < to[0]
    if from[1] < to[1]
      SE
    elsif from[1] > to[1]
      NE
    else
      E
    end
  elsif from[0] > to[0]
    if from[1] < to[1]
      SW
    elsif from[1] > to[1]
      NW
    else
      W
    end
  elsif from[1] < to[1]
    S
  elsif from[1] > to[1]
    N
  else
    # same point!
    nil
  end
end

#row_length(row) ⇒ Object

Returns the number of cells in the given row. This is generally safer than relying the #width method, since it is theoretically possible for a maze to have a different number of cells for each of its rows.



518
519
520
# File 'lib/theseus/maze.rb', line 518

def row_length(row)
  @cells[row].length
end

#solve(options = {}) ⇒ Object

Returns the solution for the maze as an array of 2-tuples, each indicating a cell (in sequence) leading from the start to the finish.

See #new_solver for a description of the supported options.



252
253
254
# File 'lib/theseus/maze.rb', line 252

def solve(options={})
  new_solver(options).solution
end

#sparsify!Object

Removes one cell from all dead-ends in the maze. Each call to this method removes another level of dead-ends, making the maze increasingly sparse.



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/theseus/maze.rb', line 379

def sparsify!
  dead_ends.each do |(x, y)|
    cell = @cells[y][x]
    direction = cell & PRIMARY
    nx, ny = move(x, y, direction)

    # if the cell includes UNDER codes, shifting it all UNDER_SHIFT bits to the right
    # will convert those UNDER codes to PRIMARY codes. Otherwise, it will
    # simply zero the cell, resulting in a blank spot.
    @cells[y][x] >>= UNDER_SHIFT

    # if it's a weave cell (that moves over or under another corridor),
    # nix it and move back one more, so we don't wind up with dead-ends
    # underneath another corridor.
    if @cells[ny][nx] & (opposite(direction) << UNDER_SHIFT) != 0
      @cells[ny][nx] &= ~((direction | opposite(direction)) << UNDER_SHIFT)
      nx, ny = move(nx, ny, direction)
    end

    @cells[ny][nx] &= ~opposite(direction)
  end
end

#startObject

Since #entrance may be external to the maze, #start returns the cell adjacent to #entrance that lies within the maze. If #entrance is already internal to the maze, this method returns #entrance. If #entrance is not adjacent to any internal cell, this method returns nil.



296
297
298
# File 'lib/theseus/maze.rb', line 296

def start
  adjacent_point(@entrance)
end

#stepObject

Completes a single iteration of the maze generation algorithm. Returns false if the method should not be called again (e.g., the maze has been completed), and true otherwise.



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/theseus/maze.rb', line 269

def step
  return false if @generated

  if @deadends && @deadends.any?
    dead_end = @deadends.pop
    braid_cell(dead_end[0], dead_end[1])

    @generated = @deadends.empty?
    return !@generated
  end

  if @algorithm.step
    return true
  else
    return finish!
  end
end

#to(format, options = {}) ⇒ Object

Returns the maze rendered to a particular format. Supported formats are currently :ascii and :png. The options hash is passed through to the formatter.



646
647
648
649
650
651
652
653
654
655
656
657
# File 'lib/theseus/maze.rb', line 646

def to(format, options={})
  case format
  when :ascii then
    require "theseus/formatters/ascii/#{type.downcase}"
    Formatters::ASCII.const_get(type).new(self, options)
  when :png then
    require "theseus/formatters/png/#{type.downcase}"
    Formatters::PNG.const_get(type).new(self, options).to_blob
  else
    raise ArgumentError, "unknown format: #{format.inspect}"
  end
end

#to_s(options = {}) ⇒ Object

Returns the maze rendered to a string.



660
661
662
# File 'lib/theseus/maze.rb', line 660

def to_s(options={})
  to(:ascii, options).to_s
end

#typeObject

Returns the type of the maze as a string. OrthogonalMaze, for instance, is reported as “orthogonal”.



639
640
641
# File 'lib/theseus/maze.rb', line 639

def type
  self.class.name[/::(.*?)Maze$/, 1]
end

#valid?(x, y) ⇒ Boolean

Returns true if the given coordinates are valid within the maze. This will be the case if:

  1. The coordinates lie within the maze’s bounds, and

  2. The current mask for the maze does not restrict the location.

If the maze wraps in x, the x coordinate is unconstrained and will be mapped (via modulo) to the bounds. Similarly, if the maze wraps in y, the y coordinate will be unconstrained.

Returns:

  • (Boolean)


338
339
340
341
342
343
344
# File 'lib/theseus/maze.rb', line 338

def valid?(x, y)
  return false if !wrap_y? && (y < 0 || y >= height)
  y %= height
  return false if !wrap_x? && (x < 0 || x >= row_length(y))
  x %= row_length(y)
  return @mask[x, y]
end

#vmirror(direction) ⇒ Object

Returns the direction that is the vertical mirror to the given direction. This will work even if the direction value is in the UNDER bitmask.



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/theseus/maze.rb', line 441

def vmirror(direction)
  if direction & UNDER != 0
    vmirror(direction >> UNDER_SHIFT) << UNDER_SHIFT
  else
    case direction
    when N  then S
    when S  then N
    when NE then SE
    when NW then SW
    when SE then NE
    when SW then NW
    else direction
    end
  end
end

#weave_allowed?(from_x, from_y, thru_x, thru_y, direction) ⇒ Boolean

Returns true if a weave may be applied at (thru_x,thru_y) when moving from (from_x,from_y) in direction. This will be true if the thru cell does not already have anything in its UNDER plane, and if the cell on the far side of thru is valid and blank.

Subclasses may need to override this method if special interpretations for direction need to be considered (see SigmaMaze).

Returns:

  • (Boolean)


677
678
679
680
# File 'lib/theseus/maze.rb', line 677

def weave_allowed?(from_x, from_y, thru_x, thru_y, direction) #:nodoc:
  nx2, ny2 = move(thru_x, thru_y, direction)
  return (@cells[thru_y][thru_x] & UNDER == 0) && valid?(nx2, ny2) && @cells[ny2][nx2] == 0
end

#wrap_x?Boolean

Returns true if the maze may be wrapped in the x direction (left-to-right).

Returns:

  • (Boolean)


320
321
322
# File 'lib/theseus/maze.rb', line 320

def wrap_x?
  @wrap == :x || @wrap == :xy
end

#wrap_y?Boolean

Returns true if the maze may be wrapped in the y direction (top-to-bottom).

Returns:

  • (Boolean)


325
326
327
# File 'lib/theseus/maze.rb', line 325

def wrap_y?
  @wrap == :y || @wrap == :xy
end