class Atoyrobot::Location

Constants

DIRICTIONS

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/atoyrobot/location.rb, line 17
def initialize(*args)
  super
  return if nil? || DIRICTIONS.include?(@facing)
  err = "Invalid facing direction, available are #{DIRICTIONS}"
  raise Atoyrobot::Exceptions::InvalidCommand, err
end

Public Instance Methods

next_move() click to toggle source
# File lib/atoyrobot/location.rb, line 41
def next_move
  validate!
  dx, dy = next_delta
  [@x + dx, @y + dy]
end
nil?() click to toggle source
# File lib/atoyrobot/location.rb, line 53
def nil?
  @x.nil? || @y.nil? || @facing.nil?
end
report() click to toggle source
# File lib/atoyrobot/location.rb, line 36
def report
  validate!
  [x, y, facing].join(', ')
end
rotate!(sign) click to toggle source
# File lib/atoyrobot/location.rb, line 30
def rotate!(sign)
  validate!
  index = (DIRICTIONS.index(facing) + sign) % 4
  @facing = DIRICTIONS[index]
end
set(x, y, facing = nil) click to toggle source
# File lib/atoyrobot/location.rb, line 47
def set(x, y, facing = nil)
  @x = x
  @y = y
  @facing = facing unless facing.nil?
end
validate!() click to toggle source
# File lib/atoyrobot/location.rb, line 24
def validate!
  return unless nil?
  err = 'location undefined, run place command to set robot location'
  raise Atoyrobot::Exceptions::LocationUndefined, err
end

Private Instance Methods

next_delta() click to toggle source
# File lib/atoyrobot/location.rb, line 59
def next_delta
  case facing
  when 'SOUTH' then [0, -1]
  when 'EAST' then [1, 0]
  when 'WEST' then [-1, 0]
  when 'NORTH' then [0, 1]
  end
end