HTML and CSS Reference
In-Depth Information
if (dragPiece && === "DIV" &&
isValidMove(dragPiece,, false)) {'drop');
For the drop() function, wrap the code that performs the drop inside an if
statement that validates the move by adding the code shown in bold. This time, the
code is passing true for the third parameter to the isValidMove() function.
if (droppedPiece && === "DIV" &&
isValidMove(droppedPiece,, true)) {
// Create a new img on the target location
var newPiece = document.createElement("img");
newPiece.src = droppedPiece.src; =, 1) +;
newPiece.draggable = true;
newPiece.addEventListener("dragstart", dragStart, false);
newPiece.addEventListener("dragend", dragEnd, false);;
// Remove the previous image
// Remove the drop effect from the target element'drop');
With these changes now in place, try running the application. You should only be
allowed to make legal moves. If you jump a checker it should be removed from the
Promoting to King
In Checkers, when a piece moves all the way to the last row, it is promoted to a king. A king works just like a
regular piece except that it can move backwards. You'll now add code to check if a piece needs to be promoted.
To promote a piece you'll change the image that is displayed to indicate it is a king. You'll also change the prefix,
making it a capital “B” or “W”. Then you can allow different rules for kings.
You'll put all this logic in a single function called kingMe() and you'll call this every time a drop occurs. If the
piece is already a king or if it's not on the last row, the function just returns. Otherwise it performs the promotion.
Search WWH ::

Custom Search