DEV Community

A 40-Line Route Checker for a Word Ladder (No Backend)

I wanted a daily word ladder that could validate a route in the browser without shipping a dictionary service or calling an API. Here is the shape that worked.

The three rules

The game has three rules:

  • Same length
  • exactly one letter changed
  • and every intermediate word must be in the accepted set.

The first two are cheap to check. The third is the only one that needs data, and it is also the only one that can be encoded compactly.

Encoding the dictionary

Instead of a full dictionary, keep a set of accepted four-letter words on the client as a sorted list. Four letters give 26^4 combinations, but only a small fraction are valid English words, so ~2,000 strings cost roughly 8KB after gzip and load before the first interaction. The check becomes a Set lookup, fast enough to run on every keystroke.

Validating the route

Route validation is a fold over the route. For each consecutive pair, assert equal length, then count differing positions. If the count is not one, the move is illegal. Then assert the resulting word is in the set.

Validate the prefix on submit and the current move live; users only ever see the difference between a legal and an illegal move.

Instant feedback

The part that surprised me was how much better the experience got once validation was instant. People stop treating the game as a form and start treating it as a board. Errors appear at the moment of the mistake rather than on submission, and the retry loop becomes part of play.

One engine for daily and practice

There is a second benefit: the same checker runs practice. The daily round and the unlimited practice mode share the rule engine and differ only in seeding. That kept the code small and the two modes consistent.

If you build something similar, make the rule engine the smallest and most tested part of the codebase. The interface will change; the rule will not. You can see the result on the daily puzzle and the unrestricted version behind the solver.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.