Most mahjong solitaire apps I played had the same two problems: ads everywhere, and boards you could lose without knowing you had lost. You clear tiles for five minutes, then discover the last six tiles are stacked in a way that can never be matched.
So when I built my own, Four Seasons Mahjong for iPhone, iPad and Mac, the main rule was: no dead ends. Every board in the game, under every legal move order, always clearable.
This post is about how that guarantee works. It turned out to be simpler than I expected.
"Solvable" is not enough
The common fix is to run a solver when generating a board and keep it only if some order of moves clears it. That proves the board is winnable. It does not stop the player from picking a different pair and getting stuck three moves later.
I wanted something stronger: whatever legal pair the player picks, the board stays clearable. Checking every possible move order directly is hopeless, because the number of orders explodes with the number of tiles.
Two facts that make it easy
The rules in my game: two tiles match if they have the same face and both are free: nothing on top of them, and at least one of their four sides open. Two properties follow from that:
- Removing tiles never makes another tile less free, or harder to see and tap. Taking tiles away can only uncover things.
- So I can peel the empty layout into waves: wave 1 is the slots that are free at the start. Remove them; wave 2 is the slots that are free now. Repeat until the layout is empty. (If a wave has an odd number of slots, one of them waits for the next wave, so every wave is even.)
Then I place the faces with one constraint: every face appears 2 or 4 times, and all copies of a face sit in the same wave.
Why that is enough: at any moment, look at the lowest wave that still has tiles. Every wave below it is empty, so by property 1, every remaining tile in that wave is free. Each face in that wave still has an even number of copies, because pairs always come off in twos. So there is at least one matching free pair. The player can never be stuck, and every move removes two tiles, so the board always ends empty.
That is the whole proof. Each board is checked with it before it goes into the game, and in code it is a check, not a search:
// A sketch of the check. The real one also reports why a board fails // and that each face maps to exactly one match key. static func isSafe(_ board: Board) -> Bool { guard board.remainingTileCount == board.rows * board.columns, // every slot filled let waves = peelingWaves(rows: board.rows, columns: board.columns) else { return false } var waveOf: [BoardPosition: Int] = [:] for (i, slots) in waves.enumerated() { for p in slots { waveOf[p] = i } } let tiles = board.visibleCells.filter { $0.tile != nil } for (_, copies) in Dictionary(grouping: tiles, by: { $0.tile!.matchKey }) { guard copies.count == 2 || copies.count == 4 else { return false } guard Set(copies.compactMap { waveOf[$0.position] }).count == 1 else { return false } } return true } Belt and braces
A proof is only as good as the rules it assumes, so the boards are not generated on the phone. An offline tool builds a catalog of 100 boards, five layouts from 72 to 120 tiles, and for each one it:
- runs the proof above,
- then plays 12 random legal games to the end with a seeded random number generator, and fails on any dead end.
That is 1,200 complete random playthroughs, and every one cleared. The app just loads the static catalog. The hint button only ever points at a pair the proof allows.
This paid off once in a way I did not expect. I found a bug where tiles on upper layers were drawn slightly higher than the match rule thought they were, so a tile could look free but be blocked. After I made drawing and rules share one geometry, the stricter rule broke the proof for 80 of the 100 boards. The tool caught it right away, those 80 got new face layouts, and the first 20 stayed byte-for-byte the same.
A daily board with one line of math
There is also a "Today's Game". I did not want to store a schedule, so:
let level = (daysSince2026_01_01 * 37) % 100 + 1 Because 37 and 100 share no common factor, this visits all 100 boards before repeating, and two days in a row never look alike. A debug audit runs 3,650 days of dates through it to make sure every one maps to a valid board.
How I build: one person and an AI team
I am a solo developer, and the code is written by AI agents. I decide what to build and what is good enough. A planning model breaks work into written tasks and reviews every change, other models write the code and run the tests, and nothing merges or ships without my OK.
A few rules came out of real mistakes:
- Written briefs, every time. Agents can't see our earlier conversation, so each task says the goal, the files, the limits, and what "done" means. That habit helped more than any model upgrade.
- No regressions, stop and tell me. During a visual cleanup, an agent removed a frame and quietly broke the tile shapes and a toolbar along with it. Now a new problem means stop and report, not fix silently.
- Look at the real app, not the report. In the simulator, the hint button seemed to do nothing. The highlight existed, but it was a pale glow you could barely see on some tile styles. Now every style gets a clear gold frame.
- A test must fail when the feature breaks. If I make the function return nothing and the test still passes, the test is useless.
Where it is now
The game is free, with no ads, no tracking and no account, and it works offline. It is in 18 languages, Arabic shown right-to-left, built with SwiftUI and running on the Mac through Mac Catalyst.
If you want to see the no-dead-ends idea in practice: Four Seasons Mahjong on the App Store.
I'd love to hear how you handle solvability in puzzle games, especially if you know a case my wave argument misses.
This article was drafted by AI agents from my project notes and code, and reviewed by me before posting.

