← Nesim Apps

How We Guarantee a Mahjong Solitaire Board Is Solvable

By Nesim Razon, independent Android developer · 2026-11-10

A Mahjong Solitaire board that cannot be finished is a small betrayal. You spend ten minutes thinking, and then learn that nothing you did could have worked. In Tilelings every deal has a solution, and we do not hope for it: we build it in. This post explains the method in plain language, including where the guarantee stops.

The problem with random deals

The obvious way to make a board is to take the tiles, shuffle them and lay them on the layout. It is quick, and it is what many simple games do. The trouble is that Mahjong Solitaire is a game of blocking. Tiles sit on top of each other and beside each other, and a tile can only leave when it is free. If two identical tiles end up in the wrong places, for example one directly on top of the other, neither can ever be removed. A random deal can contain such traps without anyone noticing.

You could shuffle and then test whether the board can be solved, and shuffle again if not. That works, but testing is a search problem that can be slow on a phone, and it gives you no control over how hard the result is. We chose a different route.

The idea: play the game before the tiles exist

Instead of placing pictures first and checking afterwards, we start with a layout that has no pictures at all, only empty positions. Then we play a pretend game of solitaire on it. At every step we look at which positions are free, pick two of them at random, and take them off the board. We write the two positions down. We repeat this until the layout is empty.

The list we wrote down is a complete sequence of legal moves: each pair was free at the moment it was taken. Now comes the clever part. We hand out the pictures along that list. Pair number one gets two tiles with the same picture, pair number two gets another matching pair, and so on. When you play the finished board, the very same sequence of moves is available to you, because every pair of positions was free in turn and every pair now matches. A solution exists by construction.

What happens at a dead end

Choosing pairs at random can paint us into a corner. If two positions are taken and only one free position is left while many tiles remain, the pretend game cannot continue. So the builder keeps a rule: after taking a pair, at least two free positions must remain, unless the board is empty. If the rule fails, the builder steps back and tries a different pair. This is called backtracking.

To keep it fast on a phone, the search has a limit of a few thousand steps. If it hits the limit, it throws the attempt away and starts again with a fresh random choice. In practice a good sequence is found quickly, and the player never waits.

A tiny example

Picture a layout of just six positions in one row, with nothing stacked. A tile in a row is free when its left or right side is open, so only the two end positions are free at the start. We take both ends and note them down as step one. Now the next two positions are the new ends, so we take them as step two, and the last two positions become step three.

Next we hand out pictures along those steps. Step one gets a sun and a sun, step two a moon and a moon, step three a star and a star. The finished row reads sun, moon, star, star, moon, sun. Play it and you take the suns, then the moons, then the stars: the same order we built. A random shuffle of the same six tiles could produce a row such as sun, star, moon, sun, star, moon, where the suns are never free together. Real layouts have towers and many rows, so the traps are far less obvious, but the idea is exactly the same.

Handing out the pictures

Most pictures in the set appear four times. Flowers are four different tiles that all match each other, and so are the seasons. When we assign tiles, we group them into packages: each ordinary picture contributes two pairs, which together make its four tiles. Packages are shuffled and handed out to the steps of the solution.

On harder levels there is an extra twist. The two pairs of the same picture are spread between the first half and the second half of the solution. That means identical tiles tend to become free at different times, so you cannot simply grab matches that are available together. The board feels tighter and asks for more planning.

Choosing the difficulty

For every level we build several candidate deals, six in the current version. Then we measure each one by replaying its solution and counting, at every step, how many matching pairs were available to the player. The average is a good proxy for difficulty. A high average means many choices and a forgiving board. A low average means few choices and a demanding one.

Easy takes the candidate with the most choices, Hard takes the one with the fewest, and Normal picks the middle one. All three are solvable. The difference is how many ways you have to go wrong.

The same seed, the same board

The random choices come from a seeded generator, so a given seed always produces the same board. That is why the daily challenge can be identical for everyone and why a half-finished game can be restored: the game saves only your moves and the seed, and rebuilds the board by replaying them.

Shuffle, hints and the honest limit

The guarantee is about the deal you start with. It does not say that every sequence of moves leads to a win. A player can still take a pair that buries the last two copies of a tile, and the board becomes impossible from that point. That is part of the puzzle.

For those moments the game has helpers. Hint uses a real solver. It searches the current position, tries the moves that free the most tiles first, remembers positions it has already shown to be dead ends, and has a limit on the number of positions it will look at. If it finds a full solution, the hint is the first move of it. If it hits the limit, it falls back to the move that frees the most tiles. Shuffle runs the same backward building process again, this time on the remaining positions, using the remaining pictures grouped into pairs. The shuffled board is solvable again. Very rarely the remaining positions themselves are the problem, for example when two tiles are stacked on each other. Then no shuffle can help, and the game says so, so that you can undo or restart.

Connect mode uses the same trick

Connect, the mode where you link tiles with a path of at most two turns, is built the same way. We start with a full grid, repeatedly choose two tiles that can currently be connected, remove them, apply the sliding rule for that level, and record the pair. Pictures are handed out along the recorded list. The board is solvable, and so is the shuffled one.

Why we care

Honest difficulty matters. When a puzzle is hard because it asks for planning, it feels fair and you want to try again. When it is hard because the deal was broken, it feels like a trick. Building boards backward lets us promise the first kind. If you ever meet a board that looks impossible, use Undo, take a hint or shuffle. There is always a way through from the start.

Frequently asked questions

Is every Mahjong Solitaire deal in Tilelings solvable?

Yes. Each deal is built from a legal sequence of moves, so at least one solution exists when you start.

Then how can I get stuck?

Because some sequences of your own moves can bury matching tiles. The guarantee covers the starting deal, not every path.

How does Shuffle stay solvable?

It rebuilds the remaining positions with the same backward method and the remaining pictures.

Are hints always correct?

A hint comes from a search of the current position. If the search finishes, it follows a real solution. If not, it suggests the move that frees the most tiles.

More from the blog