I represent an implementation of the Fifteen Puzzle game based on two examples from the slint and pathfinding crates.
You can select the automatic mode of finding a solution - the Win button.
It is better to run it in the release
profile, because in dev
the algorithm for finding the shortest path A* works much slower (approximately 10 times).
cargo run --release
Finding a solution with the least number of moves is a combinatorial problem with a huge number of possible variants. It may require a large amount of RAM and time. Although usually the game is solved in a short time (a few seconds).
But there are also difficult cases:
For example, such placement:
9, 14, 5, 13, 3, , 11, 6, 4, 1, 10, 15, 2, 8, 7, 12
Solvable in 109.324s, and requires 16.125 GiB of memory.
And this:11, 3, 1, 13, 15, 5, 9, 8, 12, 7, 10, , 6, 4, 14, 2
Solvable in 105.009s with 32.250 GiB of memory.
And this is a crash:4, 3, 13, 9, 15, 11, 8, 2, 7, 5, , 10, 12, 1, 14, 6
however if you make several moves of the piece 1 to its proper place, the game is solved quickly.
You can copy the numeric sequence of your game to the clipboard (use 0 instead of a hole)
and paste it into the game window. For example: 4 3 13 9 15 11 8 2 7 5 0 10 12 1 14 6
from the last difficult example.
Enjoy your time! 🤗
This code is released under a dual Apache 2.0 / MIT free software license.
Thanks to the rust community for such wonderful crates.