Problem 15 puzzle game
Tags: webdev, javascript, gamedev, html
Published: 2021-10-17 09:34:51

I had nothing to do, I decided to test my skills and tried to create a 15 puzzle game for the browser using only JavaScript and CSS. During development, I did not read how this could be implemented, and did not use the Internet. In the end, everything went smoothly, as it seemed to me, but I often noticed that the last two puzzle squares could not be placed in the right places. At first it seemed to me that something was wrong with me, but the situation where the puzzle could not be completed was stable, and I began to figure out what went wrong.

[![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozdtnksqlgikabtiedou.png)](https://kirilllive.github.io/Fifteen_puzzle_maker/example.html)

After reading several articles on the mathematical analysis of this game, I learned that exactly half of all possible combinations of the initial position of the squares (20.922.789.888.000 (= 16!)) Will lead to the impossibility of solving the puzzle. Elsewhere I found out that this problem occurs if swap squares 14 and 15.
In such a situation, in my game, I used mixing squares, just replacing the random block with another random block, it was a very wrong decision. So I decided to replace this part of the game with a natural shuffle, the program just randomly shifted an empty block vertically or horizontally by one square. This option eliminated combinations of blocks that would get in the way of solving the puzzle.

About ten years ago I was making a similar game for Android, but then I did not face this problem, because the terms of reference said: "the movement of the squares when mixing should be visible." And then I remembered about one more item of the same task "The ability to move several blocks towards an empty cell." Oddly enough, when I tackled the previous problem, almost all versions of this game that I have seen did not use this feature. Then I decided to add the ability to move several blocks to my game, and the game became more interesting, the puzzle began to be assembled faster and new tactical possibilities appeared, but, most importantly, it became easier to collect large puzzles with 100 chips or more.

This is how a simple game became an interesting experience for me with new discoveries, despite the fact that I had done it before, with the only difference that at first I did the game on a task, and now for the sake of testing my skills.

But I didn't stop there and made an editor for this game "[Fifteen Sliding Puzzle maker](https://github.com/Kirilllive/Fifteen_puzzle_maker)". You just need to add your own image and the editor will automatically split it into proportional blocks and adapt the CSS style. You can export the result to an HTML file for integration into your project, or simply use FrameWork "fifteen_puzzle.js", which can be easily modified to suit your needs.

[Editor](https://kirilllive.github.io/Fifteen_puzzle_maker/)  |  [Demo](https://kirilllive.github.io/Fifteen_puzzle_maker/example.html)  |  [itch.io](https://kirill-live.itch.io/fifteen-puzzle)  |  [GitHub](https://github.com/Kirilllive/Fifteen_puzzle_maker)


[![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d1l3mbnl3laxd02b8frg.jpg)](https://kirilllive.github.io/Fifteen_puzzle_maker/)