class Robulator::Simulator

Constants

PERFORMABLE_COMMANDS

Public Class Methods

new(robot=Robot.new, tabletop=Tabletop.new(5)) click to toggle source
# File lib/robulator/simulator.rb, line 21
def initialize(robot=Robot.new, tabletop=Tabletop.new(5))
  @robot    = robot
  @tabletop = tabletop
end

Public Instance Methods

perform(command) click to toggle source
# File lib/robulator/simulator.rb, line 26
def perform(command)
  command, *args = command.split(/\s|,/).reject(&:empty?)

  case command
  when 'PLACE';         place(*args) if args.size == 3 && safe_place?(*args)
  when 'MOVE';          move         if safe_to_move?
  when 'RIGHT', 'LEFT'; turn(command.downcase.to_sym)
  when 'REPORT';        send(command.downcase)
  end
end

Private Instance Methods

report() click to toggle source
# File lib/robulator/simulator.rb, line 38
def report
  return unless placed?
  puts "#{position.values.join(',')},#{direction.upcase}"
end
safe_place?(x, y, _direction) click to toggle source
# File lib/robulator/simulator.rb, line 43
def safe_place?(x, y, _direction)
  placeable_spot?(x, y)
end
safe_to_move?() click to toggle source
# File lib/robulator/simulator.rb, line 47
def safe_to_move?
  placeable_spot?(next_position[:x], next_position[:y])
end