Ice Sliding Puzzles: Solving with BFS and Generating Levels
By Nesim Razon, independent mobile developer · 2026-11-23
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:
- . is plain ice. The penguin slides over it.
- # is a rock or a snowman. It blocks the way.
- G is the gift. Reaching it stops the penguin and wins the level.
- O is a snow pile. The penguin stops on it and play continues from there.
- C is cracked ice. You can cross it once. After that it turns into water and acts like a wall.
- The arrows ^ v < > turn the slide the moment the penguin enters them.
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:
- Put the start state in the queue with an empty move list.
- Take the oldest state and try up, down, left and right.
- Skip any move that goes nowhere.
- If a move wins, return the move list plus that move.
- Otherwise, if the new state is not in the seen set, add it to the queue.
- If the queue runs dry, the level has no solution and the solver returns nothing.
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:
- Board size: 6 + (difficulty + 1) / 3 with whole-number division, so 6 to 9 cells per side.
- Rocks: about 0.16 + 0.012 times the difficulty of all cells, dropped at random. That is up to 6 rocks at difficulty 1 and up to 22 at difficulty 10.
- Special cells: difficulty minus 2, so none at the two easiest levels and eight at the hardest. They are snow piles, cracks (at most 4 per board) and, from difficulty 5, arrows.
- Target length: the shortest solution must fall between 2 + difficulty and 5 + difficulty moves, so 3 to 6 at difficulty 1 and 12 to 15 at difficulty 10.
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:
- A handmade board is solved, the solution is replayed move by move, the last move wins, and its length equals the stored minimum.
- A one-row board with a crack: sliding right crosses the crack and ends on the gift at column 5.
- Generated levels: difficulties 1 to 10, six levels each, 60 boards. Each must be solvable, have a stored minimum that equals the solution length, have at least 2 moves, and win when the solution is replayed.
- Determinism: the same level number and difficulty give identical cells.
- A second test builds every puzzle the app can serve, calendar doors 1 to 31 and every free-play level, and requires a solution for each ice board.
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
Same Seed, Same Level on Android and iOS in Kotlin Multiplatform
Is Your Nonogram Unique? Line Solvers and No-Guess Puzzles
A Photo Picker for Android and iOS Without Asking for Permission
Cloud Save on Android and iOS: The Merge Rule That Never Loses Progress
How an Advent Calendar App Decides Which Door Opens Today
How to Run an Online Secret Santa Draw (Free, No Sign-Up)