Determining if the puzzle has been solved
In this task we'll focus on determining whether the pieces have been put back into their correct locations, unscrambling and therefore solving the puzzle.
Prepare for Lift Off
The following steps will be covered in this task:
Checking the order of pieces to see if they match the starting order of the pieces
Stopping the timer
Displaying a congratulatory message
Engage Thrusters
First of all we need to decide when we should check whether the puzzle has been completed. A good place to do the check would be on the stop
event of the drag.
First add the following new variable directly after the existing current
variable at the top of the stop()
callback:
var current = getPosition(ui.helper), correctPieces = 0;
Don't forget to add a trailing comma after the first variable, as shown in the previous code sample. Next add the following code directly after the if
statement:
$.each(positions, function (i) { var currentPiece = $("#" + (i + 1)), currentPosition = getPosition(currentPiece); if (positions[i].top === currentPosition.top && positions[i].left === currentPosition.left) { correctPieces++; } }); if (correctPieces === positions.length) { clearInterval(timer); $("<p/>", { text: "Congratulations, you solved the puzzle!" }).appendTo("#ui"); }
Objective Complete - Mini Debriefing
First of all we defined a new variable called correctPieces
and set its value to 0
. We then used jQuery's each()
method to iterate the positions
array that we populated much earlier in the code, when we initially shuffled the pieces.
What we need to do at this point is get each piece from the puzzle and check whether the pieces are in the correct order. However, we can't just select the elements from the page using jQuery's children()
method, for example, or find()
, because jQuery does not return the elements in the order that they are found in the DOM, especially as we have already dragged them all around their parent container.
What we have to do instead is select each element by its id
attribute, and check to see what top
and left
CSS properties it has in its style
attribute. The length of the positions
array is the same as the number of pieces so we can iterate this array and use the index argument that jQuery automatically passes to the iterator function.
Within the iterator we first select the current element. The id
attributes for each piece will start at 1
instead of 0
because we already removed the first piece from the puzzle, so we add 1
to the index value when selecting each piece. We also get the position of the current element using our existing getPosition()
function, passing in the element we just selected.
Next we compare the current piece's top
and left
properties with the equivalent item from the positions
array, and if both the top
and left
properties match, we increment the correctPieces
variable.
Once each piece from the page and each item in the positions array have been compared and the each()
method has finished iterating, we then check whether the value of the correctPieces
variable is equal to the length of the positions
array. If it is, we know that each piece is in the correct place.
We can stop the timer at this point in the same way that we did before – using the clearInterval()
function, and then create the congratulatory message and append it to the element with an id
of ui
.