Class: MazeCell

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(maze, x, y) ⇒ MazeCell

Returns a new instance of MazeCell.



4
5
6
7
8
9
# File 'lib/maze/maze_cell.rb', line 4

def initialize(maze, x, y)
	@maze = maze
	@x, @y = x.to_i, y.to_i
	@x, @y = x % @maze.width, y % @maze.height if @maze.mirrored
	@hash = "#{@maze.hash}#{@x}#{@y}".to_i
end

Instance Attribute Details

#mazeObject (readonly)

Returns the value of attribute maze.



2
3
4
# File 'lib/maze/maze_cell.rb', line 2

def maze
  @maze
end

#xObject (readonly)

Returns the value of attribute x.



2
3
4
# File 'lib/maze/maze_cell.rb', line 2

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



2
3
4
# File 'lib/maze/maze_cell.rb', line 2

def y
  @y
end

Instance Method Details

#!=(object) ⇒ Object



92
93
94
# File 'lib/maze/maze_cell.rb', line 92

def !=(object)
	!(self.eql? object)
end

#==(object) ⇒ Object



88
89
90
# File 'lib/maze/maze_cell.rb', line 88

def ==(object)
	self.eql? object
end

#connected?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/maze/maze_cell.rb', line 54

def connected?
	!(has_wall_left? && has_wall_up? && has_wall_right? && has_wall_down?)
end

#connected_neighboursObject



62
63
64
65
66
67
68
69
# File 'lib/maze/maze_cell.rb', line 62

def connected_neighbours
	neighbours = []
	neighbours << left unless has_wall_left?
	neighbours << up unless has_wall_up?
	neighbours << right unless has_wall_right?
	neighbours << down unless has_wall_down?
	neighbours
end

#downObject



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

def down
	MazeCell.new(@maze, @x, @y + 1) if @maze.mirrored || @y < (@maze.height - 1)
end

#eql?(object) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
# File 'lib/maze/maze_cell.rb', line 80

def eql?(object)
	if (object.class == self.class)
		 @x == object.x && @y == object.y && @maze == object.maze
	elsif
		super(object)
	end
end

#has_wall_down?Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/maze/maze_cell.rb', line 49

def has_wall_down?
	i, j = @maze.xy_to_ij(@x, @y)
	@maze.value(i + 1, j) != 0
end

#has_wall_left?Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/maze/maze_cell.rb', line 34

def has_wall_left?
	i, j = @maze.xy_to_ij(@x, @y)
	@maze.value(i, j - 1) != 0
end

#has_wall_right?Boolean

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/maze/maze_cell.rb', line 44

def has_wall_right?
	i, j = @maze.xy_to_ij(@x, @y)
	@maze.value(i, j + 1) != 0
end

#has_wall_up?Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/maze/maze_cell.rb', line 39

def has_wall_up?
	i, j = @maze.xy_to_ij(@x, @y)
	@maze.value(i - 1, j) != 0
end

#hashObject



76
77
78
# File 'lib/maze/maze_cell.rb', line 76

def hash
	@hash
end

#inspectObject



96
97
98
# File 'lib/maze/maze_cell.rb', line 96

def inspect
	"#<#{self.class}: @maze=#{@maze.inspect}, @x=#{@x}, @y=#{@y}>"
end

#leftObject



18
19
20
# File 'lib/maze/maze_cell.rb', line 18

def left
	MazeCell.new(@maze, @x - 1, @y) if @maze.mirrored || @x > 0
end

#neighboursObject



58
59
60
# File 'lib/maze/maze_cell.rb', line 58

def neighbours
	[left, up, right, down].compact
end


11
12
13
14
15
16
# File 'lib/maze/maze_cell.rb', line 11

def print
	i, j = @maze.xy_to_ij(@x, @y)
	puts "  #{@maze.value(i - 1, j)}  "
	puts "#{@maze.value(i, j - 1)} #{@maze.value(i, j)} #{@maze.value(i, j + 1)}"
	puts "  #{@maze.value(i + 1, j)}  "
end

#rightObject



26
27
28
# File 'lib/maze/maze_cell.rb', line 26

def right
	MazeCell.new(@maze, @x + 1, @y) if @maze.mirrored || @x < (@maze.width - 1)
end

#upObject



22
23
24
# File 'lib/maze/maze_cell.rb', line 22

def up
	MazeCell.new(@maze, @x, @y - 1) if @maze.mirrored || @y > 0
end

#valueObject



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

def value
	i, j = xy_to_ij(@x, @y)
	@maze.value(i, j)
end