class ToyRobotSim::Robot

Constants

DIRECTIONS

Attributes

direction[R]
location[R]
table[R]

Public Class Methods

new(table) click to toggle source
# File lib/toy_robot_sim/robot.rb, line 9
def initialize(table)
  @table = table if table.is_a?(ToyRobotSim::Table)
end

Public Instance Methods

left() click to toggle source
# File lib/toy_robot_sim/robot.rb, line 29
def left
  turn(-1)
end
move() click to toggle source
# 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) click to toggle source
# File lib/toy_robot_sim/robot.rb, line 15
def place(location, direction)
  if valid_move?(location, direction)
    @location  = location
    @direction = direction
  end
end
report() click to toggle source
# File lib/toy_robot_sim/robot.rb, line 37
def report
  "#{@location.x},#{@location.y},#{@direction}" if placed?
end
right() click to toggle source
# File lib/toy_robot_sim/robot.rb, line 33
def right
  turn(1)
end

Private Instance Methods

placed?() click to toggle source
# File lib/toy_robot_sim/robot.rb, line 58
def placed?
  (@location.nil? || @direction.nil?) ? false : true
end
turn(left_or_right) click to toggle source
# File lib/toy_robot_sim/robot.rb, line 45
def turn(left_or_right)
  if placed?
    current_index = DIRECTIONS.index(@direction)
    @direction    = DIRECTIONS.rotate(left_or_right)[current_index]
  end
end
valid_move?(location, direction) click to toggle source
# File lib/toy_robot_sim/robot.rb, line 52
def valid_move?(location, direction)
  table.is_a?(ToyRobotSim::Table) &&
  table.in_range?(location) &&
  DIRECTIONS.include?(direction)
end