class Robot::Simulator::Robot

Robot class corresponds to robot which can move around on the table.

Attributes

current_track[R]
table[R]

Public Class Methods

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

Public Instance Methods

move() click to toggle source
# File lib/robot/simulator/robot.rb, line 17
def move
  return false unless is_on_table?

  newX = current_track.coordinate.x
  newX += 1 if current_track.facing == Direction::EAST
  newX -= 1 if current_track.facing == Direction::WEST

  newY = current_track.coordinate.y
  newY += 1 if current_track.facing == Direction::NORTH
  newY -= 1 if current_track.facing == Direction::SOUTH

  new_coordinate = Coordinate.new(newX, newY)
  return false unless is_valid_coordinate? new_coordinate

  self.current_track = Track.new new_coordinate, current_track.facing
end
place(coordinate, direction) click to toggle source
# File lib/robot/simulator/robot.rb, line 11
def place(coordinate, direction)
  return false unless is_valid_coordinate?(coordinate)

  self.current_track = Track.new coordinate, direction
end
turn_left() click to toggle source
# File lib/robot/simulator/robot.rb, line 34
def turn_left
  return false unless is_on_table?

  self.current_track = Track.new current_track.coordinate, Direction.left(current_track.facing)
end
turn_right() click to toggle source
# File lib/robot/simulator/robot.rb, line 40
def turn_right
  return false unless is_on_table?

  self.current_track = Track.new current_track.coordinate, Direction.right(current_track.facing)
end

Private Instance Methods

current_track=(current_track) click to toggle source
# File lib/robot/simulator/robot.rb, line 47
def current_track=(current_track)
  @current_track = current_track
end
is_on_table?() click to toggle source
# File lib/robot/simulator/robot.rb, line 55
def is_on_table?
  self.current_track != nil
end
is_valid_coordinate?(coordinate) click to toggle source
# File lib/robot/simulator/robot.rb, line 51
def is_valid_coordinate?(coordinate)
  table.is_valid_coordinate? coordinate
end