Class: ToyRobotSim::Robot

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

Constant Summary collapse

DIRECTIONS =
%w(NORTH EAST SOUTH WEST)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ Robot

Returns a new instance of Robot.



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

def initialize(table)
  @table = table if table.is_a?(ToyRobotSim::Table)
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



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

def direction
  @direction
end

#locationObject (readonly)

Returns the value of attribute location.



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

def location
  @location
end

#tableObject (readonly)

Returns the value of attribute table.



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

def table
  @table
end

Instance Method Details

#leftObject



29
30
31
# File 'lib/toy_robot_sim/robot.rb', line 29

def left
  turn(-1)
end

#moveObject



22
23
24
25
26
27
# File 'lib/toy_robot_sim/robot.rb', line 22

def move
  if placed?
    new_location = @location.send(@direction.downcase)
    @location    = new_location if valid_move?(new_location, @direction)
  end
end

#place(location, direction) ⇒ Object



15
16
17
18
19
20
# File 'lib/toy_robot_sim/robot.rb', line 15

def place(location, direction)
  if valid_move?(location, direction)
    @location  = location
    @direction = direction
  end
end

#reportObject



37
38
39
# File 'lib/toy_robot_sim/robot.rb', line 37

def report
  "#{@location.x},#{@location.y},#{@direction}" if placed?
end

#rightObject



33
34
35
# File 'lib/toy_robot_sim/robot.rb', line 33

def right
  turn(1)
end