Class: Maze::Point

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#xObject

Returns the value of attribute x

Returns:

  • (Object)

    the current value of x



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

def x
  @x
end

#yObject

Returns the value of attribute y

Returns:

  • (Object)

    the current value of y



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

def y
  @y
end

Class Method Details

.random(maze) ⇒ Object



4
5
6
# File 'lib/maze/point.rb', line 4

def self.random(maze)
  new(rand(maze.width), rand(maze.height))
end

Instance Method Details

#+(other) ⇒ Object



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

def +(other)
  self.class.new(x + other.x, y + other.y)
end

#next(maze, direction) ⇒ Object



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

def next(maze, direction)
  next_x = x + maze.calculate_offset_x_axis(direction, self)
  next_y = y + maze.calculate_offset_y_axis(direction, self)
  next_point = self.class.new(next_x, next_y)

  next_point.out_of_bounds?(maze) ? nil : next_point
end

#out_of_bounds?(maze) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/maze/point.rb', line 8

def out_of_bounds?(maze)
  !(x.between?(0, maze.width  - 1) && y.between?(0, maze.height - 1))
end

#rawObject



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

def raw
  [x, y]
end

#unvisited?(maze) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/maze/point.rb', line 12

def unvisited?(maze)
  maze[self].nil?
end

#update {|other| ... } ⇒ Object

Yields:

  • (other)


24
25
26
27
28
# File 'lib/maze/point.rb', line 24

def update
  other = self.dup
  yield other if block_given?
  other
end