Skip to content
Permalink
Browse files

Move action only updates when new coordinates are valid against the t…

…able area
  • Loading branch information...
SelenaSmall committed Aug 26, 2017
1 parent 133c780 commit 524094260e69e0aab67b0de29ac4b8165b44f239
Showing with 22 additions and 7 deletions.
  1. +22 −7 lib/handle_input.rb
@@ -62,17 +62,32 @@ def interpret(command)
if move.match?(command)
case robot.position.f
when 'NORTH'
robot.position.y += 1
y = robot.position.y
y += 1
position = Position.new(robot.position.x, y, robot.position.f)

robot.update_robot(position) if table.valid_position?(robot.position.x, position.y)
when 'EAST'
robot.position.x += 1
x = robot.position.x
x += 1

position = Position.new(x, robot.position.y, robot.position.f)

robot.update_robot(position) if table.valid_position?(position.x, robot.position.y)
when 'SOUTH'
robot.position.y -= 1
y = robot.position.y
y -= 1
position = Position.new(robot.position.x, y, robot.position.f)

robot.update_robot(position) if table.valid_position?(robot.position.x, position.y)
when 'WEST'
robot.position.x -= 1
end
x = robot.position.x
x -= 1

robot.update_robot(robot.position)
puts "New position #{robot.position.x},#{robot.position.y},#{robot.position.f}"
position = Position.new(x, robot.position.y, robot.position.f)

robot.update_robot(position) if table.valid_position?(position.x, robot.position.y)
end
end

# puts 'reporting' if report.match?(command)

0 comments on commit 5240942

Please sign in to comment.
You can’t perform that action at this time.