Class: Position

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x_coord: 0, y_coord: 0, direction: North) ⇒ Position

Returns a new instance of Position.



6
7
8
9
10
# File 'lib/another_toy_robot/position.rb', line 6

def initialize(x_coord: 0, y_coord: 0, direction: North)
  @x_coord   = x_coord
  @y_coord   = y_coord
  @direction = direction
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



4
5
6
# File 'lib/another_toy_robot/position.rb', line 4

def direction
  @direction
end

#x_coordObject (readonly)

Returns the value of attribute x_coord.



4
5
6
# File 'lib/another_toy_robot/position.rb', line 4

def x_coord
  @x_coord
end

#y_coordObject (readonly)

Returns the value of attribute y_coord.



4
5
6
# File 'lib/another_toy_robot/position.rb', line 4

def y_coord
  @y_coord
end

Instance Method Details

#advanceObject



12
13
14
15
# File 'lib/another_toy_robot/position.rb', line 12

def advance
  new_position(x_coord: @x_coord + @direction::X_DISPLACEMENT,
               y_coord: @y_coord + @direction::Y_DISPLACEMENT)
end

#leftObject



17
18
19
# File 'lib/another_toy_robot/position.rb', line 17

def left
  new_position direction: @direction.left
end

#rightObject



21
22
23
# File 'lib/another_toy_robot/position.rb', line 21

def right
  new_position direction: @direction.right
end

#to_sObject



25
26
27
# File 'lib/another_toy_robot/position.rb', line 25

def to_s
  "#{@x_coord}, #{@y_coord}, #{@direction}"
end