Skip to content

Commit 5240942

Browse files
committed
Move action only updates when new coordinates are valid against the table area
1 parent 133c780 commit 5240942

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

lib/handle_input.rb

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,32 @@ def interpret(command)
6262
if move.match?(command)
6363
case robot.position.f
6464
when 'NORTH'
65-
robot.position.y += 1
65+
y = robot.position.y
66+
y += 1
67+
position = Position.new(robot.position.x, y, robot.position.f)
68+
69+
robot.update_robot(position) if table.valid_position?(robot.position.x, position.y)
6670
when 'EAST'
67-
robot.position.x += 1
71+
x = robot.position.x
72+
x += 1
73+
74+
position = Position.new(x, robot.position.y, robot.position.f)
75+
76+
robot.update_robot(position) if table.valid_position?(position.x, robot.position.y)
6877
when 'SOUTH'
69-
robot.position.y -= 1
78+
y = robot.position.y
79+
y -= 1
80+
position = Position.new(robot.position.x, y, robot.position.f)
81+
82+
robot.update_robot(position) if table.valid_position?(robot.position.x, position.y)
7083
when 'WEST'
71-
robot.position.x -= 1
72-
end
84+
x = robot.position.x
85+
x -= 1
7386

74-
robot.update_robot(robot.position)
75-
puts "New position #{robot.position.x},#{robot.position.y},#{robot.position.f}"
87+
position = Position.new(x, robot.position.y, robot.position.f)
88+
89+
robot.update_robot(position) if table.valid_position?(position.x, robot.position.y)
90+
end
7691
end
7792

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

0 commit comments

Comments
 (0)