HTML5 game publishing

Generating Arrow Puzzle Levels That Are Always Solvable

Removing an arrow only frees space, so a trivial greedy solver checks any board, and building boards in reverse guarantees solvability. Code and difficulty knobs.

PlayKovo Team5 min read
Games currently available on PlayKovo. Open a title to see its current listing.

Arrow-escape puzzles look like they need a hand-crafted level designer, but the genre has a property that makes generating levels easy: removing an arrow can only ever free up space for other arrows, never take it away. That one fact lets you generate boards that are guaranteed solvable with about thirty lines of code, and lets you check any board, however it was made, with a trivial solver. This post walks through both.

The rules we are modelling

A board is a grid. Each arrow occupies one or more cells and has a head pointing in one of four directions. When the player taps an arrow it slides in its direction and leaves the board, but only if every cell between its head and the edge is empty. If another arrow is in the way, the tap is a mistake. Games like Arrow Escape, Arrow Exit and Hexa Go on PlayKovo all use a variation of this.

The "path" of an arrow is the straight line of cells from just past its head to the board edge. An arrow is free when no remaining arrow occupies a cell on its path.

Why solvability is easy to check

Because removing an arrow only empties cells, a free arrow stays free for the rest of the level. So the order in which you remove free arrows does not matter. Any order of free arrows leads to the same result. That means a board is solvable if and only if repeating "remove any free arrow" empties the board.

function isSolvable(arrows, W, H) {
  const remaining = new Set(arrows);
  let progress = true;
  while (remaining.size && progress) {
    progress = false;
    for (const a of [...remaining]) {
      if (isFree(a, remaining, W, H)) {
        remaining.delete(a);
        progress = true;
      }
    }
  }
  return remaining.size === 0;
}

There is no search, no backtracking and no exponential blow-up. A board with a few hundred arrows solves in a blink. If the loop stalls with arrows left, the remaining arrows form a cycle: A is blocked by B, B by C and C by A. That is an unsolvable board.

Generating a board that is solvable by construction

You do not need to generate random boards and filter them. Build the board in the reverse of the solution order:

  1. Start with an empty board.
  2. Pick a random free cell and a random direction.
  3. Check that the new arrow's path to the edge crosses no occupied cell.
  4. If it does, try another position or direction. If it does not, place the arrow.
  5. Repeat until the board is as full as you want.

The arrow you place last is the first one the player will remove, since nothing was placed after it to block it. The arrow you place first is removed last. Every arrow you place is clear of everything already on the board in its own direction, which means that in play it can only be blocked by arrows placed later, and those get removed first. The placement order reversed is a valid solution order.

function generate(W, H, count, rng) {
  const arrows = [];
  const occ = new Map(); // "x,y" -> arrow
  let tries = 0;
  while (arrows.length < count && tries++ < 5000) {
    const x = Math.floor(rng() * W), y = Math.floor(rng() * H);
    if (occ.has(x + ',' + y)) continue;
    const dir = DIRS[Math.floor(rng() * 4)];
    if (pathHitsOccupied(x, y, dir, occ, W, H)) continue;
    const a = { x, y, dir };
    arrows.push(a);
    occ.set(x + ',' + y, a);
  }
  return arrows;
}

Notice the check only looks at arrows already placed. That is the whole trick. Later arrows are allowed to sit in the path of earlier ones.

Controlling difficulty

A board that is solvable is not necessarily interesting. If every arrow points straight at an edge with nothing in front of it, the level solves in any order. Difficulty comes from dependency chains, not from the number of arrows. A few measurable knobs:

  • Density. Fill more of the board and paths cross more cells, so more arrows depend on others.
  • Chain depth. Compute the number of rounds the greedy solver needs. Remove all currently free arrows in one round, repeat. A board that takes 2 rounds is flat. One that takes 12 rounds has long chains.
  • Free arrows at the start. Fewer free arrows means the player has fewer obvious moves. Count them at generation time and reject boards outside your range.
  • Decoys. Arrows that look blocked but are free, or arrows that look free but have a long chain behind them, make the board feel tricky without being unfair.

Generate many boards per difficulty tier, compute rounds and starting free count, and keep boards that land in the target band. That gives you a smooth curve without hand-tuning each level. Our post on solver-checked procedural levels covers the general method.

Variations that keep the guarantee

  • Winding arrows. If an arrow is a line of several cells, the arrow's path starts at its head and goes in the head's direction. The construction rule is the same: check the head's path only. Make sure the body cells do not overlap occupied cells at placement time.
  • Obstacles. Fixed obstacles block paths permanently. Treat them as always occupied when checking a placement, and note that they are never removed, so no arrow's path may cross one.
  • Layers. For stacked boards, a path is a line through the stack. The same logic works with a third coordinate.
  • Cubes and 3D. On a cube, an arrow's path wraps over an edge to the next face. Define the path on the surface and the rest is unchanged.

Testing the generator

  1. Run the greedy solver on every generated board in a test and fail the build if any is unsolvable.
  2. Seed the random number generator so a failing board can be reproduced exactly.
  3. Print the rounds histogram per tier to confirm the curve rises.
  4. Play five boards yourself per tier. Numbers do not tell you whether a board feels fair.

For the player-facing side, a hint button is easy once you have the solver: pick any free arrow and highlight it. If you want to compare with how existing games handle mistakes, our guide to arrow puzzle games lists lives, timers and mistake counters across the genre.

We use optional cookies for site analytics and site advertising. You can change your choice later. Privacy policy.