Class: Atoyrobot::Location

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Defined in:
lib/atoyrobot/location.rb

Constant Summary collapse

DIRICTIONS =
%w(EAST NORTH WEST SOUTH).freeze

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Location

Returns a new instance of Location.



17
18
19
20
21
22
# 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

Instance Method Details

#next_moveObject



41
42
43
44
45
# File 'lib/atoyrobot/location.rb', line 41

def next_move
  validate!
  dx, dy = next_delta
  [@x + dx, @y + dy]
end

#nil?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/atoyrobot/location.rb', line 53

def nil?
  @x.nil? || @y.nil? || @facing.nil?
end

#reportObject



36
37
38
39
# File 'lib/atoyrobot/location.rb', line 36

def report
  validate!
  [x, y, facing].join(', ')
end

#rotate!(sign) ⇒ Object



30
31
32
33
34
# 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) ⇒ Object



47
48
49
50
51
# File 'lib/atoyrobot/location.rb', line 47

def set(x, y, facing = nil)
  @x = x
  @y = y
  @facing = facing unless facing.nil?
end

#validate!Object



24
25
26
27
28
# 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