DiceToSeven

Building puzzles backwards

How every level of DiceToSeven is guaranteed to have a solution, and how I check that the guarantee holds.

Luís Cunha · 10 November 2026 · 9 min read
I built DiceToSeven with AI assistance, and wrote this article with an AI assistant too. Every number in it comes from the game’s code and tests.

The game in one paragraph

DiceToSeven is a puzzle about dice faces. You tap pieces that touch, edge to edge, until they add up to exactly 7, and they vanish. The pieces above fall down, and when a column empties, the columns to its right slide left to close the gap. You win when the board is empty. That’s the whole game. It’s free on iPhone, iPad and Android, with no ads and no purchases.

One rule turned out to matter more than I expected: groups that form after a move don’t clear on their own. If they did, the board would follow a path you didn’t choose, and an automatic clear can walk a solvable board into a dead end. Every move in DiceToSeven is a move you made.

Why it needs a guarantee

It’s easy to get stuck. Clear the wrong 3+4 and the 4 you needed for a 1+2+4 is gone. Three moves later there’s a 5 left with no 2 anywhere near it. When that happens, the game has to be able to say, honestly: there was a way. Undo, restart, or ask for a hint.

That promise is worthless if even one level is impossible. A player who hits an unwinnable board learns that being stuck might not be their fault, and stops trusting every other level. So the whole project is built around one property: no published level is impossible.

Generate, then solve? No.

The obvious approach is to deal random dice and keep the boards a solver can clear. The sum gives a quick filter: every move removes exactly 7, so a solvable board must add up to a multiple of 7. But that’s necessary, not sufficient, and beyond it you’re searching.

I do have a solver. It’s a depth-first search that remembers every state it has proved hopeless. That memory isn’t an optimisation: moves in different parts of the board commute, so the same state is reached in many different orders, and without it the search explodes. It works, and the game uses it for hints. But if the guarantee rested on it, the guarantee would only be as good as the solver, and only for the boards it finishes within its time budget.

I wanted the guarantee to come from how a level is made, not from a check afterwards.

Start from the empty board

So the generator plays the game in reverse. It starts with nothing and inserts one group that adds up to 7, then another, and another, until the board is the size it wants. There are 14 ways to make 7 from faces 1 to 6, from 1+6 up to seven 1s, and the mix of them is one of the dials for difficulty.

Inserting is the opposite of clearing. A die inserted into a column pushes the dice above it up by one; a die in a new column pushes the columns to its right one place along. If every insertion is the reverse of a legal move, the steps read backwards are a solution. Nobody has to ask whether the level can be solved: it ships with the proof.

Here is a real Beginner level. Clear it with the solution it ships with, then watch the generator build it again from nothing.

    Level inicio-000134 and the solution stored with it. “Solve it” clears the groups in the order the solution gives; “Build it” inserts them again from the empty board, in the order the generator did. The ringed group is the last one inserted, which is also the next one to clear. Watch move 5: clearing 2+1+4 empties the first column, and the columns to its right slide left.

    The catch: gravity doesn’t run backwards

    Inserting looks like the mirror image of clearing, but it isn’t quite. Gravity and column collapse throw information away: after a clear, nothing on the board says where the removed dice were. So an insertion can look right and still not be the reverse of any legal move.

    Here’s the kind of thing that goes wrong. The generator is placing the group 1+2+4. It puts the 1 and the 2 side by side, then puts the 4 in a new column, and that new column slides in between them.

    The 1 and the 2 touch
    The 4 arrives: rejected
    First, the 1 and the 2 share an edge. Then the 4 arrives in a new column and pushes the 6 and the 2 one place to the right. Now no two of the three dice share an edge; the 4 touches the 5 and the 6 instead, which aren’t in the group. Clearing these three isn’t a legal move, so this can’t be a step of a solution.

    Each step looked reasonable on its own. The only reliable way to know whether an insertion is the reverse of a legal move is to play that move forwards and look at what comes out:

    // The inserted group must be a legal
    // move on the new board…
    if (!isValidGroup(next, group)) reject();
    
    // …and playing it must give back
    // exactly the board we had before.
    if (boardKey(applyMove(next, group))
        !== boardKey(board)) reject();

    The first check covers the sum and the connection. The second simulates gravity and collapse with the same applyMove the game uses, and compares the result with the previous board, character for character. If anything about the insertion was off, the boards differ and the candidate is thrown away.

    What the check actually catches

    I measured it, because I wanted to know rather than assume. Across 3,500 generated levels in seven different parameter profiles, the simulation never rejected a candidate that the validity check hadn’t already rejected. Connectivity, as in the figure, is the real filter: it turns down between a quarter and a half of all attempts.

    That isn’t luck. The generator builds on a board where every die knows whether it was inserted in the current step, and only reads coordinates once the step is done. Removing exactly the marked dice gives every column back its original order, because gravity is just a filter that keeps order. And a new column made only of inserted dice disappears in the collapse. With that representation, the inversion is structural.

    The check stays anyway. What it protects isn’t the generator but applyMove. If I ever add a piece that stops things falling, gravity gets a second stopping rule, the collapse becomes ambiguous, and the inversion stops being structural. That’s exactly where impossible levels would come from, and the check costs one applyMove per accepted step.

    A board that is just lists

    Most of this is easy because of how a board is stored: a list of columns, each listed from the bottom up.

    // A die face; 0 is the joker.
    type Cell = 0 | 1 | 2 | 3 | 4 | 5 | 6;
    // One column, from the bottom up.
    type Column = readonly Cell[];
    // The columns, from left to right.
    type Board = readonly Column[];

    In this shape, gravity is removing items from a list (the ones above simply move down), and column collapse is removing empty lists. Neither needs index arithmetic, so neither has an off-by-one to get wrong. A board is also valid JSON, so the level files need no serialiser and tests are written as readable literals. And board.map(col => col.join("")).join("|") is a canonical key, used both for the check above and to memoise the solver.

    The one subtle rule is adjacency. Columns have different heights, so a die only has a neighbour to the side if that column is tall enough:

    (c, r) touches (c - 1, r)
        only if r < height(c - 1)
    (c, r) touches (c + 1, r)
        only if r < height(c + 1)

    Forget that condition and every test on a rectangular board still passes. It fails on the first pyramid.

    Trust, but replay

    The generator is designed to be right. The test suite assumes it isn’t.

    The most valuable test in the project is the round trip: generate a level, then play its stored solution from the start, one move at a time. Every move must be legal on the board at that moment, and the last one must leave the board empty. It runs on 200 levels in each of ten parameter profiles (small, large, tall, wide, with a joker, pairs only, big groups only, pyramid-shaped and so on), plus 2,000 more that check every level adds up to a multiple of 7 and has exactly the size it asked for. There’s no tolerance: a single failure would mean impossible levels in the stores.

    The engine that runs these tests is the same code that runs in the game and in the generator. It has no dependencies and no access to the browser or to Node, so there’s no second copy to drift out of sync. Nearly a thousand automated tests run before every release.

    The stored solution ships with the level, too. While you’re still on its path, the hint button just reads the next step. Once you’ve strayed from it, the game runs the solver from wherever you are.

    Extra rules that keep the solution legal

    The later chapters add two rules on top: welded pairs, which leave together or not at all, and ice, which only lets a die leave in a pair. Both fix something I measured. On the biggest boards there were about 35 legal moves in a typical position, but only 6 different combinations; the other 29 were the same sums in other places. The real decision was which of several 5+2s to take, and nothing on the board made that visible.

    Neither rule is allowed to touch the guarantee. A weld only goes on two stacked dice that the stored solution already clears in the same move. Ice only goes on a die that the solution already clears as part of a pair. So the stored solution stays legal word for word, and the tests replay it again with the rules applied.

    Welds are only ever vertical, and that isn’t a scoping decision. Two stacked dice fall the same distance after any move, because gravity keeps order, so they stay together for good. A sideways weld would come apart the first time one column fell and the other didn’t. The game would then need a rule for breaking it, and a state that changes mid-level is one more thing the reverse construction would have to undo.

    Solvable isn’t the same as good

    A level with a solution can still be dull, or unfair from the first move. So every candidate is played at random at least a thousand times: each random game picks any legal group and carries on until the board is empty or stuck. The share of games that clear the board, the survival rate, is the main measure of difficulty. Beginner keeps boards that random play clears more than half the time; Expert keeps boards it clears 3 to 22 times in a hundred.

    Two more checks sit on top:

    • The fairness floor. Whatever two moves you open with, the board can still be solved. No level punishes you before you’ve had a chance to read it. (The joker chapters make one exception: spending the joker early is the trap they’re built around.)
    • Greedy-safe boards. For the early Time Attack boards, the generator searches every reachable state and confirms none is a dead end. With the clock running, you can’t get stuck there, whatever you tap.

    None of this uses Math.random(). All randomness comes from an explicit seed, which a lint rule enforces, and each random game gets its own seed, base + index. So the results are the same whatever order the worker threads finish in. Each level stores both its seed and the board itself, so a future change to the generator can’t quietly change a level that’s already out.

    The bug no metric caught

    One mistake passed every check I had. An early version chose the column for each insertion with a weight of (height + 1)^bias, to favour deep placements. That’s a feedback loop: tall columns attract more dice and get taller. With a bias of 3, a column ten dice high was 1,331 times more likely to be picked than a new one.

    Survival rate, branching and solution length all looked fine. I only saw it when I drew the boards in a console renderer: the Expert candidates averaged 11 columns and 32 rows, with columns up to 42 dice tall. Towers. The fix was to keep the column choice uniform, capped at a target height, and apply the depth bias only to the row within a column.

    Measure, and then look at what you measured.

    If you’re building a puzzle generator

    • Make the guarantee come from how a level is built, not from a check afterwards.
    • Verify each step by running it forwards with the real game rules, not by reasoning about why it should work.
    • Write the round-trip test first. It’s cheap, and it’s the one that matters.
    • Measure difficulty instead of guessing it, and seed everything so you can reproduce it.
    • Draw your boards.

    Try it

    DiceToSeven is free on iPhone, iPad and Android: 133 levels, Time Attack, Survival and a new challenge every day. No ads, no purchases, no data collected.

    App Store Google Play

    Questions, or a level you’re sure is impossible? Write to dicetoseven@gmail.com.