Robot Eats Candies on a Grid
A grid is already rendered, with walls, candies and a robot, and helper functions for cells are provided. Write the JavaScript that moves the robot and removes candies as it reaches them.
Requirements
- Arrow keys move the robot one cell; walls and the grid edge block the move. A candy on the destination is eaten and the status line counts it.
- The Eat the nearest candy button finds the shortest path to the closest reachable candy and walks it one cell at a time, eating on arrival.
- When no candy is reachable the button does nothing.
- Re-rendering only toggles classes on the existing cells; the grid is not rebuilt.
Clarify first, as the reports differ: does the player drive the robot or does it move by itself? Here it is both.
Hints
Hint 1
step is a direction lookup plus the inBounds check; Set.delete returns whether a candy was there, so it doubles as the eat check.
Hint 2
Breadth-first search from the robot with a predecessor map; the first candy dequeued is the nearest, and walking the map back from it gives the path.
Hint 3
Use an index pointer over the queue array instead of shift(), and a setInterval for the walk so each step renders.