class Robojora::Robot

Constants

NOT_IN_BOARD

Attributes

board[R]
facing[RW]
name[R]

Public Class Methods

new(name) click to toggle source
# File lib/robojora/robot.rb, line 9
def initialize(name)
  @name = name
  @board = board
end

Public Instance Methods

left() click to toggle source
# File lib/robojora/robot.rb, line 34
def left
  self.facing = rotator.left
end
move() click to toggle source
# File lib/robojora/robot.rb, line 38
def move
  raise Robojora::RobotNotOnBoardError if not_movable?

  mover.move
end
place(x_coordinate, y_coordinate, facing) click to toggle source
# File lib/robojora/robot.rb, line 18
def place(x_coordinate, y_coordinate, facing)
  raise ArgumentError, 'x_coordinate and y_coordinate must be integers' unless x_coordinate.is_a?(Integer) && y_coordinate.is_a?(Integer)
  raise CardinalDirectionError, 'facing should be a cardinal point string' unless Robojora::Settings.cardinal_points.include?(facing) || !select_square_by_name.nil?

  square = select_square_by_coordinates(x_coordinate, y_coordinate)
  robot_removing_and_placing(facing, square) if square
end
play_board(board) click to toggle source
# File lib/robojora/robot.rb, line 14
def play_board(board)
  @board = board
end
report() click to toggle source
# File lib/robojora/robot.rb, line 26
def report
  select_square_by_name && facing ? location_report : NOT_IN_BOARD
end
right() click to toggle source
# File lib/robojora/robot.rb, line 30
def right
  self.facing = rotator.right
end
robot_removing_and_placing(facing, square) click to toggle source
# File lib/robojora/robot.rb, line 52
def robot_removing_and_placing(facing, square)
  remove_robot_from_current_square
  self.facing = facing
  square.presence = name
  true
end
select_square_by_coordinates(x_coordinate, y_coordinate) click to toggle source
# File lib/robojora/robot.rb, line 48
def select_square_by_coordinates(x_coordinate, y_coordinate)
  board.select { |square| square.coordinates == [x_coordinate, y_coordinate] }.first
end
select_square_by_name() click to toggle source
# File lib/robojora/robot.rb, line 44
def select_square_by_name
  board.select { |square| square.presence == name }.first
end

Private Instance Methods

location_report() click to toggle source
# File lib/robojora/robot.rb, line 61
def location_report
  "#{select_square_by_name.coordinates.join(', ')}, #{facing}"
end
mover() click to toggle source
# File lib/robojora/robot.rb, line 74
def mover
  Robojora::Movers.const_get(facing.capitalize).new(self)
end
not_movable?() click to toggle source
# File lib/robojora/robot.rb, line 78
def not_movable?
  select_square_by_name.nil? || select_square_by_name.borders.include?(facing)
end
remove_robot_from_current_square() click to toggle source
# File lib/robojora/robot.rb, line 65
def remove_robot_from_current_square
  current_square = select_square_by_name
  current_square.presence = nil if current_square
end
rotator() click to toggle source
# File lib/robojora/robot.rb, line 70
def rotator
  Robojora::Rotator.new(facing)
end