Visualizations
366 / 500

366 · Algorithms

Rebound Robots

Robots slide until they hit something. A* finds the fewest moves to the target.

Four robots sit on a walled 16x16 board, and a robot only stops when it hits a wall or another robot, so reaching a target usually means parking the others as stoppers first. A position is the four robot cells packed into one 32-bit number, with the three helper robots sorted because they are interchangeable. The solver is A*: a backward breadth-first search from the target, letting a robot stop anywhere along a slide, gives a lower bound on moves for every cell (the numbers that flash on the board), so the first solution found is the shortest one. Plain breadth-first search then runs on the same puzzle to show how many more positions it needs, and the radial tree replays A*'s expansions ring by ring, colored by which robot moved.

Try it. Click a robot to select it, then click a cell it can slide to, drag it in a direction, or use the arrow keys (1 to 4 or Tab picks a robot). Solve (Space) plays the shortest solution from where you are, Hint (H) draws the next optimal move, Reset (R), Z to undo, New target (T) and New board (B).

  • A* with a lower-bound table
  • 32-bit state packing with symmetry
  • Open-addressing hash set

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build a sliding robots puzzle with an optimal solver, using JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Make a 16x16 board on a canvas that fills the window and stays sharp on high-DPI screens. Store walls as four bits per cell (north, east, south, west), keep both sides of every wall in sync, and wall off the border and a 2x2 block in the middle.
- Add a few L-shaped wall corners in each quarter of the board and mark some of them as colored targets.
- Place four colored robots. A robot moves in one of four directions and keeps sliding until the next step would cross a wall or hit another robot. Let me click a robot and then press an arrow key to move it, with a quick eased slide.
- Pick a target and a robot that must reach it. Write a breadth-first search over positions: a position is the four robot cells, which fit in one 32-bit number (8 bits each). Every robot in every direction is a move. Keep a Set of visited positions and parent links, then play back the shortest solution.
- Show how many positions the search visited.

Once that works, make it beautiful and faster:
- Give the board dark tiles with bright wall bars, and draw the robots as glossy domes with eyes that look where they are heading, a motion trail and a little squash when they stop.
- The three helper robots are interchangeable, so sort their cells before packing. Count how many fewer positions that visits.
- Precompute, for every cell, the fewest moves the goal robot would need if it could stop anywhere along a slide (a backward BFS from the target). Use it as the heuristic for A* (when a queued position is reached again by a shorter path, lower its cost and requeue it, or the answer can come out a move too long), and compare A*'s count with plain breadth-first search.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as drawing the search tree, a hint button, or a generator that keeps only targets needing six or more moves.
PreviousSiteswap JugglerJuggling notation made physical: true parabolas, a ladder diagram and the state graph. NextTree RingsA pine grows for a century under a simulated climate, one sanded ring per year.

Related visualizations

  • Sokoban ForgeAlgorithms Crate puzzles forged by playing backward from the goals, then solved by A*.
  • Gridlock SolverAlgorithms Map every position of a sliding-car puzzle, solve it, and generate harder ones.
  • Particle Filter RobotAlgorithms Thousands of pose guesses condense out of fog onto a lost robot's true position.
  • Blocks World PlannerAlgorithms A robot arm plans with A* over STRIPS states, then stacks the blocks to match your goal.
  • Sliding Puzzle SolverGames IDA* with pattern databases solves the 15-puzzle in the fewest possible moves.
  • Maze SolverAlgorithms A maze carves itself, then A* hunts for the way out.
  • Dancing LinksAlgorithms Knuth's Algorithm X tiles boards with wooden pentominoes while its links visibly dance.
  • Lights Out AlgebraMath The toggle-tile puzzle solved instantly at any size by linear algebra over GF(2).
  • RRT* ParkingAlgorithms A self-driving car grows a tree of curved manoeuvres, then reverses into its spot.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs