class Toy::Robot
Attributes
facing[R]
table[R]
Public Class Methods
new(table=Toy::Table.new, direction=Toy::Direction)
click to toggle source
# File lib/toy/robot.rb, line 7 def initialize(table=Toy::Table.new, direction=Toy::Direction) @table = table @direction = direction @facing = nil end
Public Instance Methods
move!()
click to toggle source
# File lib/toy/robot.rb, line 30 def move! raise Toy::Robot::Error::PlacementError unless placed? place(target.x, target.y, @facing) if target && target.available? end
place(x, y, facing)
click to toggle source
# File lib/toy/robot.rb, line 13 def place(x, y, facing) raise Toy::Direction::Error::DirectionError unless Toy::Direction::DIRECTION.include?(facing) @table.hold!(x, y, self) @facing = facing end
placed?()
click to toggle source
# File lib/toy/robot.rb, line 20 def placed? !!current_unit end
position()
click to toggle source
# File lib/toy/robot.rb, line 24 def position raise Toy::Robot::Error::PlacementError unless placed? [current_unit.x, current_unit.y, @facing] end
turn_left!()
click to toggle source
# File lib/toy/robot.rb, line 36 def turn_left! raise Toy::Robot::Error::PlacementError unless placed? @facing = @direction.rotate_left(@facing) end
turn_right!()
click to toggle source
# File lib/toy/robot.rb, line 42 def turn_right! raise Toy::Robot::Error::PlacementError unless placed? @facing = @direction.rotate_right(@facing) end
Private Instance Methods
current_unit()
click to toggle source
# File lib/toy/robot.rb, line 50 def current_unit @table.unit_collection.find_by_object(self) end
target()
click to toggle source
# File lib/toy/robot.rb, line 54 def target @table.unit_collection.find_by_direction_of(current_unit.x, current_unit.y, @facing) end