← Nesim Apps

300 Maze Levels With Monsters, Checked by a Solver

By Nesim Razon, independent mobile developer · 2026-11-25

#Algorithms#Level generation#Game design

Grumblings is a turn-based maze where grumpy monsters chase you, and every one of its 300 levels was generated on a computer and accepted only if a solver found a way out. This post is about the part between the rules and the shipped file: why a plain breadth-first search is enough, how the solver acts as a gate, where the star thresholds come from, and how the same rules run in JavaScript for the demo on the website without drifting from the app.

The rules, exactly

The hero takes one step in one of four directions, or waits. Then every monster moves one step, and then every monster moves one more step. With two monsters the order is monster A, monster B, monster A, monster B. A monster has a preferred axis, horizontal first or vertical first. It steps one cell toward the hero along that axis. If that step is blocked, it tries the other axis. If that is blocked too, it stays where it is. A blocked step means a wall or a cell that another monster occupies.

You are caught the moment any monster enters your cell, and the monster steps that were still to come are skipped. If your own move reaches the exit, you win right there and the monsters do not move at all. A move into a wall or off the board is simply not allowed and does not count as a turn.

Why the state is only (hero, monsters)

My first plan was a state of hero position, monster positions and a turn counter. The counter turned out to be unnecessary. Nothing in the rules depends on how many moves have been played: monsters have no memory, no random numbers and no timers. The same hero and monster positions always lead to the same result for the same move. So a position is fully described by where the hero is and where each monster is, and two paths that arrive at the same positions are the same state, whatever turn it is.

That makes the state space small. With n cells and k monsters there are at most n to the power of (1 plus k) states. From the pack's own settings the largest case is a 7 by 7 maze with two monsters: 49 cubed, which is 117,649 states. An 8 by 8 maze with one monster has 4,096. Each state has five possible actions: up, down, left, right and wait.

A breadth-first search is all it takes

The solver packs a state into one integer, hero cell first and then each monster cell, and keeps two flat arrays indexed by that integer: where it came from and which move led there. It then runs an ordinary breadth-first search from the start. For every state it tries the five actions through the same move function the game uses. A caught result is thrown away. A new position is queued if it has not been seen. The first time a move wins, the search stops and the path is rebuilt backwards from the arrays.

Because breadth-first search expands states in order of how many moves it took to reach them, that first win is a shortest solution, counted in moves where a wait is a move. This matters more than it looks. The solver is not a separate model of the game. It calls the real rules, so it cannot disagree with them, and when a rule changes the solver changes with it for free.

Generated offline, gated by the solver

A candidate level starts as a perfect maze made with a depth-first backtracker. Then a few extra walls are removed, about a sixth of the cell count plus up to two, so that loops exist. My first experiment used perfect mazes only and the shortest escape on a 5 by 5 board was 31 moves, far too long, because a tree-shaped maze slows the monsters down more than it slows you. Loops fixed that.

The exit goes on the border, the hero somewhere random, and each monster at least three cells away from the hero by Manhattan distance and never on the exit. Then the analyzer runs a full breadth-first search over everything reachable. If no winning move exists, it returns nothing and the candidate is dropped. That is the gate: an unsolvable board cannot enter the pool, whatever the generator did.

Surviving candidates must also have a minimum solution length, between 4 and 14 depending on the level, and no more than 45 moves. Level 6 has to contain at least one wait, because that is where the game teaches it. Each level gets a pool of 400 candidates for one monster, 110 for two. The pool is sorted by a difficulty score, the solution length plus six per wait, one and a half per move that ends within two cells of a monster, and thirty times the share of reachable states from which winning is no longer possible. A target percentile picks the level. It rises across the game and follows a sawtooth inside each group of ten, easy first and a harder finale. If the chosen candidate was already used, the next nearest one is taken, so no level repeats.

Stars come from the best solution

Because the solver returns the shortest solution, the number of stars needs no hand tuning. Three stars if you finish in no more moves than the stored best. Two stars if you are within the larger of two moves or a quarter of the best length, rounded down. Otherwise one star. For a best of 8 the limits are 8 and 10. For a best of 20 they are 20 and 25.

The move count is the length of the history, waits included, and undo removes the last entry, so a mistake you take back costs nothing. Hints are computed live: the hint button runs the solver from the current position and shows the first move of its answer. If no solution exists from where you stand, it shows a question mark instead, because levels can contain positions where the hero is already lost, and undo is the way out. The stored solution is therefore only used for stars and for tests, never for hints.

Why the shipped file is plain text

The pack is one text file with 300 lines, one level per line: size, hero cell, exit cell, monster axes, monster cells, then the vertical walls and the horizontal walls as strings of zeros and ones, and finally the best solution. The first level reads 5;4,3;3,0;H;1,4; followed by two 25-character wall strings and LLUL, a four-move escape.

Plain text has practical advantages for a small project. It diffs in version control, so a regenerated pack shows exactly which levels changed. A unit test can read it without Android. The website demo embeds the very same lines, and a test can compare them character by character. And the solution at the end makes the file its own answer key, which is what the validation test replays.

The same rules in JavaScript, kept honest by a golden test

The demo on the website needs the rules and the solver in the browser, so I ported the move function, the monster step, the breadth-first search and the star formula to JavaScript. A port is a promise that two implementations agree, and promises need a test. The test file first repeats the rule cases from the Kotlin tests: the preferred axis, the fall back to the other axis, being caught on the first monster step so the second is skipped, winning before the monsters move, and the two-monster order.

The golden test works on the 30 levels embedded in the demo. For each one it replays the stored best solution through the JavaScript rules, which must end in a win without ever being caught. Then it runs the JavaScript solver from the start and requires the same length as the stored solution. So the best solution found by the solver wins the level and is as short as the one in the file, in a second implementation of the rules. A last test checks that the 30 embedded lines equal the first 30 lines of the app's file, so a regenerated pack breaks the build instead of silently showing different levels.

As a one-off check for this article I ran the JavaScript solver over all 300 lines of the file. Every stored solution had exactly the length the solver found.

Numbers from the pack

A small script over the file gives these facts. There are 300 levels: 20 on a 5 by 5 board, 50 on 6 by 6, 166 on 7 by 7 and 64 on 8 by 8. 180 have one monster and 120 have two. Solutions run from 4 to 37 moves, with a median of 16 and a mean of 16.4. The 30 levels in the browser demo range from 4 to 13 moves.

What I would do differently

Waiting is a core rule, yet only 57 of the 300 solutions contain a wait, and none contains more than two. The difficulty score rewards waits but the pool rarely offers them. Next time I would generate with a wait target for part of the pack instead of hoping the random mazes provide it.

The solver returns one shortest path, not how many exist. A level with a single narrow solution feels different from one with five equivalent ones, and counting shortest paths would be a cheap extra measurement. I would also share one file of test cases between Kotlin and JavaScript. Today the JavaScript tests were written by hand from the Kotlin ones, which is fine until someone adds a case to only one side.

Grumblings is my maze game for Android, and the browser demo with its first 30 levels is on its page at nesimapps.com if you want to try the rules before reading more.

Frequently asked questions

What does being caught mean in a turn-based maze with monsters?

A monster enters the hero's cell during its steps. The remaining steps are skipped and the move ends in a loss, which the player can undo.

Why is a breadth-first search enough to solve it?

The rules are deterministic and have no memory, so a state is just the hero and monster positions. The state count stays small and the first win found is a shortest solution.

How are the three stars decided?

Three stars at or below the stored best length, two within the larger of two moves or a quarter of it, otherwise one star.

Can a level be unsolvable?

Not in the pack. A candidate without a winning move is dropped by the analyzer before it can enter, and a test replays every stored solution through the game rules.

How is the website demo kept in sync with the app?

A golden test replays each embedded level's best solution and requires the JavaScript solver to find the same length, and the embedded lines must equal the app's file.

More from the blog