I'm working alone on my code, trying to learn Ruby as I go.
Class Robot
is supposed to be instantiated with the position on a map, and whether it is facing toward a side of the map. It has a couple of moves: left or right, where the face of the robot changes direction, and a move action, one step forward.
class Robot
def initialize(pos_X, pos_Y, facing)
@pos_X, @pos_Y, @facing = pos_X, pos_Y, facing
end
def move
world_switch(Proc.new { @pos_X += 1}, Proc.new { @pos_X -= 1},
Proc.new { @pos_Y += 1}, Proc.new { @pos_Y -= 1})
end
def left
world_switch(Proc.new {@facing = 'WEST'}, Proc.new {@facing = 'EAST'},
Proc.new {@facing = 'NORTH'}, Proc.new {@facing = 'SOUTH'})
end
def right
world_switch(Proc.new {@facing = 'EAST'}, Proc.new {@facing = 'WEST'},
Proc.new {@facing = 'SOUTH'}, Proc.new {@facing = 'NORTH'})
end
def report
puts "Output: " << @pos_X.to_s << ',' << @pos_Y.to_s << ',' << @facing
end
def world_switch(do_on_north, do_on_south, do_on_east, do_on_west)
case @facing
when 'NORTH'
do_on_north.call
when 'SOUTH'
do_on_south.call
when 'EAST'
do_on_east.call
when 'WEST'
do_on_west.call
end
end
end