← Nesim Apps

Ice Sliding Puzzles: Solving with BFS and Generating Levels

By Nesim Razon, independent mobile developer · 2026-11-23

#Algorithms#Level generation#Game design

A penguin on ice cannot stop where it wants. It slides until something blocks it. That one rule makes a deep puzzle, and also one that a computer solves with very little code. In Snowlings, my winter calendar app, the ice game finds the shortest solution with a breadth-first search, and the level generator uses the same search to throw bad boards away. Here is how it works, with the real numbers from the code.

One rule: slide until something stops you

A move is a direction: up, down, left or right. The penguin keeps going that way until it cannot. Nothing else is a choice. Every cell of a level is one character:

The board edge blocks too. So from any spot there are at most four moves, one per direction, and each one ends next to a wall, on a snow pile, on the gift or in front of a broken crack. That small number of choices is what keeps the search cheap.

State: where you are plus what is broken

To search a puzzle you must define its state: everything that can change the future. Position alone is not enough because of the cracks. Standing on a cell while a crack is still whole is a different situation from standing there after the crack has broken. So the code defines a state as the penguin’s x and y plus a bit mask of broken cracks, one bit per crack cell, numbered in reading order.

The crack rule has one detail. A crack breaks when the penguin leaves its cell, not when it enters. A penguin can stop on a crack, for example with a rock right behind it, and the crack stays whole until the next move.

For the search, a state is packed into one number: the mask shifted left by 16 bits, then y shifted by 8 bits, then x. A set of these numbers remembers which states were already seen. One more guard: a slide that has taken more than width times height times four steps is treated as an arrow loop and counts as no move.

BFS: the shortest solution falls out

Breadth-first search explores in layers: every state reachable in one move, then in two, then in three. The first time a move wins, the move count is the minimum, because nothing shorter was left unexplored. The solver keeps a queue of states, each with the moves that led to it.

The loop is short:

Take this 4 by 3 board, where S is the start: row one is S . . #, row two is four dots, row three is # . . G. Right slides to the third column and stops in front of the rock. Down slides to the bottom edge. Right again slides onto the gift. The shortest solution is three moves: right, down, right. No two-move line exists, and the unit test checks that the solver’s answer has the same length as the stored minimum.

How big can the search get? The generator uses at most 4 cracks and boards of at most 9 by 9. That gives at most 81 positions times 16 crack combinations, which is 1,296 states. Real levels visit far fewer.

Generating levels: random board, then ask the solver

The generator takes a level number and a difficulty from 1 to 10 and builds a random board. The difficulty sets four things:

The start and the gift go on free ice cells. Then the board is parsed, and parsing runs the BFS from the start and stores the answer as the level’s minimum move count. A board with no solution gets a minimum of 0 and is thrown away. A board whose shortest solution is outside the target range is thrown away too, but the generator remembers the closest one. After about 400 attempts it returns that closest board instead of looping forever.

The random numbers come from a seeded generator, with the seed built from the level number and the attempt number. The same level is therefore identical on every phone, and a test checks that level 77 at difficulty 6 produces the same cells twice.

Star thresholds come from the minimum

Because the solver knows the true minimum, stars need no hand tuning. Finishing in the minimum or fewer moves gives 3 stars, up to two extra moves gives 2 stars, and anything more gives 1. With a minimum of 6, six moves earn 3 stars, seven or eight earn 2, and nine or more earn 1.

The same solver powers hints. It runs from the current state, not from the start, so the hint still works after a few wrong moves. The game shows the first move of that solution.

Snowlings has 60 ice levels in free play, plus ice puzzles behind some calendar doors in December. All of them come from this one generator and this one solver.

Tests that prove every board is solvable

A generator that has never been tested will eventually ship an impossible board. These are the checks in the project:

The solver is the generator’s quality check and also the test’s oracle, so one small function guards three things: levels, stars and hints.

Frequently asked questions

Why use BFS instead of depth-first search for an ice puzzle?

BFS finds the shortest solution first. Depth-first search finds some solution, not necessarily the shortest, and the shortest length is needed for the star thresholds.

Why is the position alone not enough as the state?

Cracks change the board. The same cell with an intact crack and with a broken crack leads to different moves, so broken cracks must be part of the state.

How does the generator know a level is solvable?

It runs the solver. A board with no solution is rejected, and the solver’s shortest length also checks the difficulty range.

Do all players get the same levels?

Yes. Levels come from a seeded random generator, so the same level number produces the same board on every device.

More from the blog