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(*params) ⇒ MazeCell

Returns a new instance of MazeCell.



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

def initialize(*params)
	params = params.clone
	@maze = params.shift
	@coords = params.map { |c| c.to_i }
	@hash = "#{@maze.hash}#{ @coords.reduce(""){ |accum, c| "#{accum}#{c}" }.to_i }".to_i
end

Instance Attribute Details

#coordsObject (readonly)

Returns the value of attribute coords.



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

def coords
  @coords
end

#mazeObject (readonly)

Returns the value of attribute maze.



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

def maze
  @maze
end

Instance Method Details

#!=(object) ⇒ Object



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

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

#==(object) ⇒ Object



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

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

#[](dimension_index) ⇒ Object



81
82
83
# File 'lib/maze/maze_cell.rb', line 81

def [](dimension_index)
	@coords[dimension_index]
end

#backward(dimension_index) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/maze/maze_cell.rb', line 15

def backward(dimension_index)
	coords = @coords.clone
	coords[dimension_index] -= 1
	params = coords.clone
	params.unshift @maze
	MazeCell.new *params if @coords[dimension_index] > 0
end

#connected?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
# File 'lib/maze/maze_cell.rb', line 43

def connected?
	has_wall = true
	(0...@maze.dimensions.length).each do |d|
		has_wall = has_wall && has_wall_forward(d) && has_wall_backward(d)
		break unless has_wall
	end
	not has_wall
end

#connected_neighboursObject



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

def connected_neighbours
	neighbours = []
	(0...@maze.dimensions.length).each do |d|
		neighbours << forward(d) unless has_wall_forward(d)
		neighbours << backward(d) unless has_wall_backward(d)
	end
	neighbours
end

#eql?(object) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/maze/maze_cell.rb', line 69

def eql?(object)
	if (object.class == self.class)
		eq = (@maze == object.maze)
		@coords.each_with_index do |c, c_idx|
			eq = eq && c == object[c_idx]
		end
		eq
	elsif
		super(object)
	end
end

#forward(dimension_index) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/maze/maze_cell.rb', line 23

def forward(dimension_index)
	coords = @coords.clone
	coords[dimension_index] += 1
	params = coords.clone
	params.unshift @maze
	MazeCell.new *params if @coords[dimension_index] < (@maze.dimensions[dimension_index] - 1)
end

#has_wall_backward(dimension_index) ⇒ Object



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

def has_wall_backward(dimension_index)
	indices = @maze.coords_to_indices *@coords
	indices[dimension_index] -= 1
	@coords[dimension_index] <= 0 or @maze.get_raw_value(*indices) == 1
end

#has_wall_forward(dimension_index) ⇒ Object



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

def has_wall_forward(dimension_index)
	indices = @maze.coords_to_indices *@coords
	indices[dimension_index] += 1
	@coords[dimension_index] >= @maze.dimensions[dimension_index] - 1 or @maze.get_raw_value(*indices) == 1
end

#hashObject



65
66
67
# File 'lib/maze/maze_cell.rb', line 65

def hash
	@hash
end

#inspectObject



93
94
95
96
97
98
99
100
# File 'lib/maze/maze_cell.rb', line 93

def inspect
	unless @inspect
		c_index = 0
		coord_list = @coords.map { |c| "@c#{c_index+=1}=#{c}" }
		@inspect = "#<#{self.class}: @maze=#{@maze.inspect}, #{coord_list.join(', ')}>"
	end
	@inspect
end

#neighboursObject



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

def neighbours
	(0...@maze.dimensions.length).map { |d| [forward(d), backward(d)] }.flatten.compact
end

#valueObject



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

def value
	@maze.get_value *@coords
end