HTML5 game publishing

Undo and Hint Systems for Puzzle Games, Built on One Solver

A plain-data game state makes undo trivial and a solver makes hints correct from wherever the player is. Snapshots versus commands, hint ladders and the edge cases that break both.

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

Players of logic puzzles will forgive a hard level. They will not forgive a mistake they cannot take back. Undo and hint are two of the most requested features in puzzle games, and two of the easiest to implement badly. A naive undo breaks on the third level. A hint that gives away the answer ruins the puzzle. This post shows a clean way to build both, so they share one piece of infrastructure: a game state you can copy and a solver you can ask.

Start with a state you can copy

Both features rest on one principle: the whole puzzle state must be plain data, separate from rendering. If your game state lives partly in sprite positions, DOM elements and animation timers, undo is almost impossible. If it lives in one object, undo is nearly trivial.

const state = {
  grid: [...],        // cells, numbers, positions
  moves: 0,
  lives: 3,
  won: false,
};

Rendering reads from state and never writes to it. Input changes the state through one function, and that function is the only place state changes. When this is true, undo and hints are small additions instead of rewrites.

Undo by snapshot

For small puzzles, the simplest undo is to save a copy of the state before each move and restore it on demand:

const history = [];

function doMove(move) {
  history.push(structuredClone(state));
  if (history.length > 200) history.shift();   // cap memory
  applyMove(state, move);
  render(state);
}

function undo() {
  const prev = history.pop();
  if (!prev) return;
  Object.assign(state, prev);
  render(state);
}

structuredClone is available in current browsers and copies nested data correctly. A JSON round trip also works if your state has only plain values. Avoid shallow copies like {...state}, which share the nested arrays, so undo restores the very same arrays you just changed.

For big boards, storing a full copy per move is wasteful. Use the command pattern instead: store each move together with its inverse, and undo by running the inverse. That is more code but uses far less memory, and it makes a replay feature possible.

Decide what undo does to your scoring

This is a design choice, not a technical one, and the choice matters:

  • Free undo. Best for relaxing puzzles. Players explore without fear.
  • Limited undo. A few per level adds tension. Show the remaining count clearly.
  • Undo costs something. Some games charge coins for undo, as Vials does for its optional helpers. If you do this, say what it costs before the player taps.

Whatever you pick, decide whether undoing a mistake restores the lost life or time. In a game where a wrong tap costs a heart, undo that does not return the heart feels like a bug. Include lives and timer in the state you snapshot, or explicitly document otherwise.

Make hints use the solver

A hint button that shows a hard-coded answer per level only works for authored levels and breaks the moment the player has made a move that is not on your solution path. A solver-based hint works from the player's current state:

  1. Run your solver on the current state.
  2. If it finds a solution, take the first move of that solution.
  3. Show that move.

Because the hint is computed from where the player actually is, it stays correct after mistakes. If the current state is unsolvable (the player made a move that cannot be recovered from), the solver says so, and you can offer undo instead. That is a much better experience than a hint that suggests an impossible move.

The same solver you used for generating levels often doubles as the hint engine. The general approach is in solver-checked procedural levels, and a simple example for a genre where any free move is safe is in generating arrow puzzles that are always solvable.

Make hints teach rather than tell

Showing the exact move is the strongest hint, but a puzzle played that way loses its point. A ladder of hints keeps the player thinking:

  1. Highlight the region. "Look near here."
  2. Name the idea. "One of these pieces cannot move yet."
  3. Show the move. The full answer.

With a solver you can produce all three levels from the same computed move: the area around it, then a description, then the move itself. For text hints, write templates per puzzle type and fill in coordinates or colours.

Limit hints in a way that fits the game. A few free hints per level, or earned through play, work well. If hints cost a rewarded ad, the ad must only grant the hint in the reward callback, and the game must work when no ad is available. See granting rewards only in onReward.

Keep the solver fast enough

A hint that freezes the game for two seconds is worse than no hint. Options for slow solvers:

  • Run the solver in a Web Worker so the interface stays responsive.
  • Limit the search depth and return the best partial answer.
  • Precompute a solution when the level loads and recompute only after a move that leaves the stored path.

Show a small thinking indicator if a hint takes more than a few hundred milliseconds.

Edge cases that break both features

  • Animations in flight. Do not accept undo or hint taps mid-animation, or finish the animation first. Otherwise state and visuals disagree.
  • Randomness during play. If a move triggers a random event, store the random result in the history, or undo will not reproduce it. A seeded generator, as in a daily puzzle without a server, handles this.
  • Saved games. Decide whether history is saved. Usually it should not be, but the current state must be, with a version number. See save data versioning.
  • Level complete. Disable undo after the win screen starts, or you can undo into a state where the game says you won but the board is not finished.

Test both features by playing a level and spamming undo and hint in random order. If the game stays consistent, you have done it right.

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