A simple implementation of the Fifteen game in Java

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

That's all,


Related Posts

4 thoughts on “A simple implementation of the Fifteen game in Java

  1. I'm not sure of this but the code is incorrect.  I was trying to fix my randomize for the tiles so I replaced mine with his however it still isn't solveble.  (not all combanations are solveble if you simply randomize the bord you will get combenations that do not work.)

  2. Ok i'm sure of it now, who the hell posts the code to there game without playing a few rounds first.  If I figure out how to fix the randomization i'll post the edited code on here.  (3/4 times you won't be able to solve the puzzle because of incorect permatations.)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.