Class: MazeCell
- Inherits:
-
Object
- Object
- MazeCell
- Defined in:
- lib/maze/maze_cell.rb
Instance Attribute Summary collapse
-
#maze ⇒ Object
readonly
Returns the value of attribute maze.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #!=(object) ⇒ Object
- #==(object) ⇒ Object
- #connected? ⇒ Boolean
- #connected_neighbours ⇒ Object
- #down ⇒ Object
- #eql?(object) ⇒ Boolean
- #has_wall_down? ⇒ Boolean
- #has_wall_left? ⇒ Boolean
- #has_wall_right? ⇒ Boolean
- #has_wall_up? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(maze, x, y) ⇒ MazeCell
constructor
A new instance of MazeCell.
- #inspect ⇒ Object
- #left ⇒ Object
- #neighbours ⇒ Object
- #print ⇒ Object
- #right ⇒ Object
- #up ⇒ Object
- #value ⇒ Object
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
#maze ⇒ Object (readonly)
Returns the value of attribute maze.
2 3 4 |
# File 'lib/maze/maze_cell.rb', line 2 def maze @maze end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
2 3 4 |
# File 'lib/maze/maze_cell.rb', line 2 def x @x end |
#y ⇒ Object (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
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_neighbours ⇒ Object
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 |
#down ⇒ Object
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
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
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
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
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
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 |
#hash ⇒ Object
76 77 78 |
# File 'lib/maze/maze_cell.rb', line 76 def hash @hash end |
#inspect ⇒ Object
96 97 98 |
# File 'lib/maze/maze_cell.rb', line 96 def inspect "#<#{self.class}: @maze=#{@maze.inspect}, @x=#{@x}, @y=#{@y}>" end |
#left ⇒ Object
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 |
#neighbours ⇒ Object
58 59 60 |
# File 'lib/maze/maze_cell.rb', line 58 def neighbours [left, up, right, down].compact end |
#print ⇒ Object
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 |
#right ⇒ Object
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 |
#up ⇒ Object
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 |
#value ⇒ Object
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 |