Some days (months??? years???) ago I began programming in Java language. I produced some simple games.
The 15-puzzle is a sliding puzzle that consists of a frame of numbered square tiles in random order with one tile missing. The puzzle also exists in other sizes, particularly the smaller 8-puzzle. If the size is 3×3 tiles, the puzzle is called the 8-puzzle or 9-puzzle, and if 4×4 tiles, the puzzle is called the 15-puzzle or 16-puzzle named, respectively, for the number of tiles and the number of spaces. The object of the puzzle is to place the tiles in order by making sliding moves that use the empty space.
As usual, at first there are some blablabla….
Now the import section, here I've included all the library needed to implement an applet:
And now the applet launcher
Until now there isn't anything interesting.
But from now on we are going to see the core of the game.
The class Board instantiates the Game, and handles the painting of the board and the mousepressed event. A mousepressed event can happen in the shuffle box or in the board, in the first case the Board class calls the shuffle method of the Fifteen class while in the second case the Board class calls the moveNumber of the Fifteen class.
The fifteen class is the engine of the game, this class:
- re-paint the board (pain method)
- understands if a move is valid (moveNumber method)
- moves the pieces (moveNumber method)
- checks for the win (checkWin method)
- scramble the pieces on the board