In the first year of university, at the fourth computer lab of the "Informatica A" course, one of the exercises asked to implement in C the game of 15, the famous one with the 4 by 4 grid, an empty cell, and the numbers inside to be reordered. But, since I was already there messing around, I took the opportunity of having some little more fun and I implemented the game in its generic dimension, \(N\) by \(N\). In this way, the "15 game" extended into the "\((N^{2}-1)\) game".
The original code was written in C, but here you can find an equivalent and playable version which uses html and javascript.
About the game, albeit simple it involves some interesting math concepts. For example, it is worth noting that the initial board cannot be simply initialized with random numbers, as otherwise one could get unsolvable configurations as this one
\[ \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ \bf{8} & \bf{7} & \square \end{bmatrix} \]where numbers 8 and 7 cannot be exchanged. Therefore the initialization of the board can either be made by starting from the solved state and repeatedly performing random moves, so that we surely derive a final configuration which is solvable, or by really shuffling all the tiles but with a final check, as explained e.g. here, on the solvability of the board. In the provided implementation, the code employs the first approach so that it can also be easily tuned the desired complexity of the game.
click on the image to play it!