Science

The Math Behind SortStack: Guaranteeing Every Puzzle is Solvable

March 29, 2026 · 6 min read

There is no worse feeling in logic gaming than staring intensely at a puzzle for thirty minutes, only to slowly realize that the scenario presented to you is mathematically impossible to complete. This is notoriously common in heavily randomized color-sorting games available on mobile app stores, where naive algorithms simply scatter colorful blocks completely at random across tubes or stacks.

When we built SortStack for PuzzleVault, we made a strict developmental promise: Every single puzzle generated to our players must be 100% mathematically solvable. Period. To uphold this promise, we couldn't rely on random generation. Instead, we implemented a sophisticated procedural generation technique known in computer science as the "Reverse-Moves Algorithm."

The Flaw of Naive Randomization

If you were to design a sorting game by simply taking 20 blocks composed of 5 different colors and randomly distributing them into 6 empty stacks, the odds are terrifyingly high that you will generate an unsolvable layout. Certain color combinations might trap essential blocks beneath others inherently creating a permanent deadlock. An unsolvable puzzle immediately ruins player trust.

The Reverse-Moves Paradigm

To eliminate this fundamentally broken issue, SortStack generates its puzzles backwards. Our underlying JavaScript engine begins by creating the ultimate, perfect winning state. Imagine a board where every single stack contains exactly four blocks of the exact same color, perfectly separated. By definition, this state is perfectly "solved."

From here, our algorithmic generator begins playing the game in reverse. It selects the top block of a correctly sorted stack and artificially "un-solves" it by moving it to an entirely different stack (assuming there is vertical space available). This reverse-move constitutes one step deeper into chaos. The algorithm systematically performs hundreds—sometimes thousands—of these completely valid reverse moves, effectively shuffling the board into an extremely complex, scrambled mess.

Ensuring Complexity and Playability

By tracing the path natively backwards from a pure success state, mathematical certainty guarantees that there inherently must be at least one direct path forwards to solve it—following the exact footprints back to the origin. But simply scrambling randomly backward isn't enough to make a fun puzzle.

Our algorithm implements advanced heuristic checks during the scrambling phase. If a reverse move accidentally creates a scenario that is "too obvious" (such as accidentally sorting a stack prematurely before the player has begun), the algorithm rejects that branch and tries a different permutation. This enforces a high skill ceiling for Hard and Expert difficulties, while guaranteeing the puzzle remains inherently fair.

Why Mathematical Fairness Matters

Puzzle games are ultimately a delicate contract of trust heavily relying on psychological motivation. When a player fails a level in SortStack and runs abruptly out of moves on the final turn, they need to feel that it was due strictly to their own flawed strategic planning, rather than an unfair, randomly generated trap.

By implementing strict algorithmic reverse-shuffling, we ensure that the "Aha!" moment of finally locking the last color stack is purely the result of your own intellectual perseverance, unhindered by algorithmic impossibilities. The next time you find yourself stuck on an Expert level of SortStack, rest easy knowing that a confirmed, mathematical solution absolutely exists. You just have to find it.