class Robot

Constants

MOVEMENT_DIRECTIONS
VERSION

Attributes

direction[R]
table[R]

Public Class Methods

new() click to toggle source
# File lib/robot.rb, line 18
def initialize
  @table = Table.instance
end

Public Instance Methods

left() click to toggle source
# File lib/robot.rb, line 38
def left
  rotate(:left)
end
move() click to toggle source

trys to move in current direction one place

# File lib/robot.rb, line 24
def move
  postition = new_position
  return if table.place(postition[:x], postition[:y])
  puts 'Cannot move off the table'
  CommandCentre.instance.say('Does not compute, I will fall to my death')
end
place(args) click to toggle source
# File lib/robot.rb, line 54
def place(args)
  return unless args
  x, y, face = args.split(',')
  return unless x && y && face
  return unless table.place(x, y)
  self.direction = face.downcase.to_sym
end
report() click to toggle source

outputs the x,y,position of the robot also includes a position matrix

# File lib/robot.rb, line 44
def report
  puts to_s
  puts Matrix.new(table.position, direction).to_s
  say_position
end
right() click to toggle source

rotates compass faces and uses current face as index to retrieve new direction

# File lib/robot.rb, line 34
def right
  rotate(:right)
end
to_s() click to toggle source
# File lib/robot.rb, line 50
def to_s
  "#{table.position[:x]}, #{table.position[:y]} #{direction.upcase}"
end

Private Instance Methods

direction=(face) click to toggle source
# File lib/robot.rb, line 79
def direction=(face)
  @direction = face if MOVEMENT_DIRECTIONS.keys.include?(face)
end
face_index() click to toggle source
# File lib/robot.rb, line 92
def face_index
  faces.index(direction)
end
faces() click to toggle source
# File lib/robot.rb, line 88
def faces
  MOVEMENT_DIRECTIONS.keys
end
move_coordinates() click to toggle source
# File lib/robot.rb, line 75
def move_coordinates
  MOVEMENT_DIRECTIONS[direction]
end
new_position() click to toggle source

fetches new coordinates and adds them to existing coords

# File lib/robot.rb, line 71
def new_position
  table.position.merge(move_coordinates) { |_key, memo, value| memo + value }
end
rotate(direction) click to toggle source
# File lib/robot.rb, line 83
def rotate(direction)
  index = direction == :left ? -1 : 1
  self.direction = faces.rotate(index)[face_index]
end
say_position() click to toggle source
# File lib/robot.rb, line 64
def say_position
  CommandCentre.instance.say("My position is currently #{table.position[:x]}/
   by #{table.position[:y]} and facing #{direction}")
end