Class: Robot

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

Constant Summary collapse

UNPLACED_NOTICE =
"Robot should be placed first."

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grid) ⇒ Robot

Returns a new instance of Robot.



9
10
11
12
# File 'lib/robot.rb', line 9

def initialize(grid)
  @grid     = grid
  @position = Position.new(nil, nil, nil)
end

Instance Attribute Details

#positionObject (readonly)

Returns the value of attribute position.



5
6
7
# File 'lib/robot.rb', line 5

def position
  @position
end

Instance Method Details

#moveObject



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

def move
  update_position @position.advance
end

#place(position) ⇒ Object



14
15
16
# File 'lib/robot.rb', line 14

def place(position)
  update_position position
end

#placed?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/robot.rb', line 35

def placed?
  @position.valid? @grid
end

#reportObject



30
31
32
33
# File 'lib/robot.rb', line 30

def report
  return UNPLACED_NOTICE unless placed?
  @position.to_s
end

#rotate_leftObject



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

def rotate_left
  update_position @position.left
end

#rotate_rightObject



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

def rotate_right
  update_position @position.right
end