Introduction

This is a simple puzzle game developed using pure HTML and jQuery. This puzzle consists of an image which is divided into fifteen small parts or we could call them segments. These parts are randomly arranged on the puzzle board. Your task isthat you have to rearrange the image segments in proper order. There are no limitations for moves or time; you can take your own time to complete this puzzle.

Background

This puzzle game is mainly based on swapping of divs on puzzle board, Puzzle board consist s of a total of 16 small divs, 15 image divs and one empty div. Div position is set on the puzzle board on random basis. When you click on any div adjacent to the empty div position of selected div and empty div gets changed.

Check Live Demo here.

Code

To swap clicked square and the current empty square, I have written the below function which will swap the position of these divs or segments using left and top co-ordinates.

  1. function Move(clicked_square, square_size) {
  2. var ismovable = false;
  3. // Swap x/y between the clicked square and the currently empty square
  4. var oldx = $('#board').children("div:nth-child(" + EmptySquare + ")").css('left');
  5. var oldy = $('#board').children("div:nth-child(" + EmptySquare + ")").css('top');
  6. var newx = $(clicked_square).css('left');
  7. var newy = $(clicked_square).css('top');
  8. // The clicked square is north of the empty square
  9. if (oldx == newx && newy == (parseInt(oldy) - square_size) + 'px')
  10. ismovable = true;
  11. // The clicked square is south of the empty square
  12. if (oldx == newx && newy == (parseInt(oldy) + square_size) + 'px')
  13. ismovable = true;
  14. // The clicked square is west of the empty square
  15. if ((parseInt(oldx) - square_size) + 'px' == newx && newy == oldy)
  16. ismovable = true;
  17. // The clicked square is east of the empty square
  18. if ((parseInt(oldx) + square_size) + 'px' == newx && newy == oldy)
  19. ismovable = true;
  20. if (ismovable) {
  21. // Increment zindex so the new tile is always on top of all others
  22. $(clicked_square).css('z-index', zi++);
  23. // Swap squares... Animate new square into old square position
  24. $(clicked_square).animate({ left: oldx, top: oldy }, 200, function() {
  25. //Move old square into new square position
  26. $('#board').children("div:nth-child(" + EmptySquare + ")").css('left', newx);
  27. $('#board').children("div:nth-child(" + EmptySquare + ")").css('top', newy);
  28. });
  29. }
  30. }
HTML page code is below :
  1. <html>
  2. <head>
  3. <title>jQuery Puzzle Game Demo</title>
  4. <link href="main.css" rel="stylesheet" type="text/css" />
  5. <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
  6. <script src="main.js"></script>
  7. </head>
  8. <body>
  9. <div id="PuzzleDiv" style="border:1px solid black">
  10. <div id="board"></div>
  11. </div> <input id="button" type="button" value="Start Again"></input>
  12. <script>
  13. $('#button').click(function ()
  14. {
  15. $("#board").randomize();
  16. });
  17. </script>
  18. </body>
  19. </html>
CSS used for this puzzle:
  1. #game_object {
  2. background-color: #ffffff;
  3. }
  4. #PuzzleDiv {
  5. position:fixed;
  6. top: 15%;
  7. left: 35%;
  8. height: 403px;
  9. width: 403px;
  10. padding:2px;
  11. }
I hope you enjoyed reading this article. So keep reading and don’t forget to post your valuable feedback.