Skip to content

Commit

Permalink
Move action only updates when new coordinates are valid against the t…
Browse files Browse the repository at this point in the history
…able area
  • Loading branch information
SelenaSmall committed Aug 26, 2017
1 parent 133c780 commit 5240942
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions lib/handle_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5240942

Please sign in to comment.