Class: Maze

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

Direct Known Subclasses

MazeBTrace

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ Maze

Returns a new instance of Maze.



33
34
35
36
37
38
39
# File 'lib/maze/maze.rb', line 33

def initialize(width, height)
	@mirrored = false
	@height, @width = height.to_i, width.to_i
	@height_full, @width_full = (1 + 2*@height), (1 + 2*@width)
	initialize_matrix
	@hash = "#{@width}#{@height}".to_i # optimized pre-computed hash
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



31
32
33
# File 'lib/maze/maze.rb', line 31

def height
  @height
end

#height_fullObject (readonly)

Returns the value of attribute height_full.



31
32
33
# File 'lib/maze/maze.rb', line 31

def height_full
  @height_full
end

#matrixObject (readonly)

Returns the value of attribute matrix.



31
32
33
# File 'lib/maze/maze.rb', line 31

def matrix
  @matrix
end

#mirroredObject (readonly)

Returns the value of attribute mirrored.



31
32
33
# File 'lib/maze/maze.rb', line 31

def mirrored
  @mirrored
end

#widthObject (readonly)

Returns the value of attribute width.



31
32
33
# File 'lib/maze/maze.rb', line 31

def width
  @width
end

#width_fullObject (readonly)

Returns the value of attribute width_full.



31
32
33
# File 'lib/maze/maze.rb', line 31

def width_full
  @width_full
end

Instance Method Details

#cell(x, y) ⇒ Object



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

def cell(x, y)
	MazeCell.new(self, x, y)
end

#generateObject



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

def generate
	self
end

#hashObject



67
68
69
# File 'lib/maze/maze.rb', line 67

def hash
	@hash
end

#inspectObject



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

def inspect
	"#<#{self.class}: @width=#{@width}, @height=#{@height}>"
end


41
42
43
44
45
46
# File 'lib/maze/maze.rb', line 41

def print
	@matrix.each do |row|
		puts row.join(' ').gsub(/0/, ' ').gsub(/1/, '#').gsub(/2/, 'X')
	end
	self
end

#value(i, j) ⇒ Object



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

def value(i, j)
	@matrix[i][j]
end

#xy_to_ij(x, y) ⇒ Object



56
57
58
59
60
61
# File 'lib/maze/maze.rb', line 56

def xy_to_ij(x, y)
	x, y = x % @width, y % @height if @mirrored
	i = 1 + (2 * y)
	j = 1 + (2 * x)
	[i, j]
end