class RubyRobot::Robot
Constants
- VALID_DIRECTIONS
Directions are clockwise from north
Attributes
direction[R]
tabletop[R]
Public Class Methods
new(direction)
click to toggle source
# File lib/ruby_robot/robot.rb, line 10 def initialize(direction) orig_direction = direction err_msg = "New Robots must have direction value of one of the following symbols: #{VALID_DIRECTIONS.join(', ')}; invalid value '#{orig_direction}'" direction = direction.kind_of?(String) ? direction.downcase.to_sym : direction raise ConstructionError.new(err_msg) unless VALID_DIRECTIONS.include?(direction) @direction = direction @tabletop = nil end
Public Instance Methods
inspect()
click to toggle source
# File lib/ruby_robot/robot.rb, line 19 def inspect case @direction when :north then "^" when :south then "|" when :east then ">" # :west else "<" end end
left()
click to toggle source
# File lib/ruby_robot/robot.rb, line 29 def left # A little cheating here...index -1 will effectively wrap around and # return the last element @direction = VALID_DIRECTIONS[dir_idx - 1] end
move()
click to toggle source
TODO: Error checking for if @tabletop.nil? Also, @tabletop.move and @tabletop.move? together should be synchronized if multithreaded where > 1 Robot
are on a Tabletop
.
Return report
after call, whether it was successful or not (assuming it is in fact placed on a board).
# File lib/ruby_robot/robot.rb, line 59 def move @tabletop.move(self, direction) if @tabletop.move?(self, direction) report end
place(tabletop)
click to toggle source
Called by a Tabletop
where this has been placed
# File lib/ruby_robot/robot.rb, line 42 def place(tabletop) @tabletop = tabletop end
report()
click to toggle source
# File lib/ruby_robot/robot.rb, line 46 def report @tabletop.position(self).merge(direction: direction) end
right()
click to toggle source
# File lib/ruby_robot/robot.rb, line 35 def right @direction = VALID_DIRECTIONS[(dir_idx + 1) % VALID_DIRECTIONS.size] end
Private Instance Methods
dir_idx()
click to toggle source
# File lib/ruby_robot/robot.rb, line 66 def dir_idx VALID_DIRECTIONS.index(@direction) end