Dash & Slash is a small one-hand roguelite I build in Flutter. Every room is generated: a 13 by 21 tile grid, a door at the top, the player spawning at the bottom, and a few mirrored "stamps" of walls and pits in between. Shortly after version 1.0.0 went live on Google Play, a room turned up where the door opened and the player could never reach it.
The fix was three lines. The interesting part, at least for me as a QA engineer, is why the existing test could never have caught it.
How the generator works
The generator is deliberately boring. It fills the room with floor, puts walls on the border and a closed door in the middle of the top row:
for (var c = 5; c <= 7; c++) { map.set(c, 0, T.doorClosed); } Then it drops two to four wall stamps and a few pit stamps at random positions. Every stamp is mirrored onto the other half of the room, so layouts look designed even though they are random. Two areas are reserved so nothing can be stamped onto them: the spawn pocket at the bottom and the door approach at the top.
static bool _reserved(int c, int r) { // Player spawn pocket and the door approach stay free. if (r >= 15 && c >= 4 && c <= 8) return true; if (r <= 2 && c >= 4 && c <= 8) return true; return false; } After the stamps, a validator runs a breadth-first search from the spawn tile. Any floor tile the search does not reach is turned into a wall, because an unreachable pocket would be a place where an enemy could spawn and never get to you. If enough floor is left, the room is accepted. If not, the generator rerolls, up to 24 times, and falls back to an empty room.
for (var r = 1; r < map.rows - 1; r++) { for (var c = 1; c < map.cols - 1; c++) { if (map.at(c, r) == T.floor) { floor++; if (_seen[map.idx(c, r)] == 0) map.set(c, r, T.wall); } } } final interior = (map.cols - 2) * (map.rows - 2); return reached >= interior * 0.62 && floor > 0; The rule is "at least 62 percent of the interior must be reachable". That sounds safe. It is not.
The bug
The reserved areas protect the door approach from being stamped on directly. They do not protect it from being cut off. Walls and pits placed a few rows lower could, together with their mirrored copies, close an entire row across the room. Everything above that row, including the free pocket in front of the door, was now unreachable from the spawn.
The validator did exactly what it was told. It found the pocket, decided it was unreachable, and filled it with walls. Then it counted the reachable floor below the barrier, found more than 62 percent, and accepted the room.
So the player fought through the room, the enemies died, the portal opened, and there was a wall between the player and the door. No way out except closing the game.
The commit message from that day describes it in one paragraph:
Walls and pits could close an entire row below the door. The validator then walled off the unreachable pocket in front of the door, but still accepted the room because enough floor was reachable, so the player was locked in once the portal opened.
Why the test was green
There was a test for this generator, and it was not lazy. It generated rooms for 119 seeds and four room types, so 476 rooms per run, and for every room it checked two things: the spawn tile is walkable, and every walkable tile is reachable from the spawn.
for (var seed = 1; seed < 120; seed++) { a.rng.reseed(seed); for (final type in [RoomType.combat, RoomType.elite, RoomType.arena, RoomType.boss]) { RoomGenerator.generate(a.map, a.rng, type: type, zone: 1 + seed % 3); expect(a.map.walkable(RoomGenerator.spawnCol, RoomGenerator.spawnRow), isTrue); a.map.computeFlow(RoomGenerator.spawnCol, RoomGenerator.spawnRow); // every walkable cell must have a finite flow distance } } Look at that second check again with the validator in mind. The validator turns every unreachable floor tile into a wall. After that, "every walkable tile is reachable" is true by construction. The test asserted the postcondition of the code it was testing. It could not fail, no matter how broken the layout was, because the generator had already removed the evidence.
It is a trap I know well from QA work: a test that checks what the code does instead of what the user needs. The user does not need "all remaining floor is connected". The user needs "I can walk from where I start to where I have to go".
The fix
The fix is one condition in the validator, right after the search and before the cleanup:
// The door approach must be reachable, otherwise the pocket in front of // the door gets walled off below and the player is locked in the room. if (_seen[map.idx(spawnCol, 1)] == 0) return false; If the tile directly below the door was not reached, the room is rejected and the generator rolls a new one. No special repair logic, no attempt to carve a path. Rerolling is cheap on a grid this small, and a reroll produces a room that was designed by the same rules as every other room.
The test got two new assertions that describe the player's goal instead of the generator's internals:
// The tile in front of the door must be reachable, or the player is // locked in once the portal opens. expect(a.map.walkable(6, 1), isTrue, reason: 'door blocked ($type, seed $seed)'); expect(a.map.flow[a.map.idx(6, 1)], lessThan(32767), reason: 'door unreachable ($type, seed $seed)'); The reason strings matter more than they look. When a generated-content test fails, the first thing you need is the seed and the room type, so you can reproduce exactly that room. Without them you have a red test and 476 candidates.
The fix shipped as version 1.0.1. The game is now at 1.1.0 with a daily challenge and ten ascension levels, and the door check has been in every build since.
What I took from it
- A validator that repairs input makes its own output look valid. If your code cleans up a problem, a test that inspects the cleaned-up result will never see the problem. Test the invariant the user cares about, not the invariant the cleanup guarantees.
- Write down the player's path as an assertion. "Spawn reaches door" is one line. It should have been the first line of that test.
- Seeds in failure messages. For anything procedural, the seed is the bug report.
- Prefer rerolling over repairing. A repaired room is a room nobody designed. A rerolled room follows the same rules as all the others.
If you want to see the generator in action, Dash & Slash is free on the App Store and Google Play: https://dash-and-slash.pages.dev/?ref=devto
Dash & Slash by Felix Helleckes. More of my apps: https://felix-helleckes.github.io/apps/?ref=devto

