-
Notifications
You must be signed in to change notification settings - Fork 0
RobotSquaresville
class Squaresville include Robot
def tick events
@x_start ||= x
@y_start ||= y
@direction ||= :right # :left, :right, :up, :down
@distance ||= 1
@moved ||= 0
say @direction.to_s case @direction when :left if pointing_left? go else turn_left if stopped? end when :right if pointing_right? go else turn_right if stopped? end when :up if pointing_up? go else turn_up if stopped? end when :down if pointing_down? go else turn_down if stopped? end else raise 'unknown direction' end
turn_gun(-1)
fire 3 unless events['robot_scanned'].empty?
end
def stopped? speed == 0 end
def next_direction! @direction = case @direction when :left :up when :up :right when :right :down when :down :left end @moved = 0 @distance += 1 end
def go case @direction when :left if @moved < @distance accelerate(1) @moved += 1 else stop next_direction! if stopped? end when :right if @moved < @distance accelerate(1) @moved += 1 else stop next_direction! if stopped? end when :up if @moved < @distance accelerate(1) @moved += 1 else stop next_direction! if stopped? end when :down if @moved < @distance accelerate(1) @moved += 1 else stop next_direction! if stopped? end else raise 'unknown direction' end end
def pointing_right? heading == 0 end
def pointing_left? heading == 180 end
def pointing_up? heading == 90 end
def pointing_down? heading == 270 end
def turn_right new_heading = heading > 10 ? heading - 10 : heading turn(-new_heading) end
def turn_left return if pointing_left? new_heading = if heading > 180 turn(-1) else turn(1) end end
def turn_up return if pointing_up? new_heading = if heading > 90 turn(-1) else turn(1) end end
def turn_down return if pointing_down? say heading new_heading = if heading < 180 turn(-1) else if heading > 270 turn(-1) else turn(1) end end end end