DHTML Jigsaw Puzzle: NN4; Breaking the Puzzle | WebReference

DHTML Jigsaw Puzzle: NN4; Breaking the Puzzle


The DHTML Lab Jigsaw Puzzle, Part III: NN4
breaking the puzzle and dispersing the pieces

The breakUp() function starts the ball rolling. Since the puzzle may have been dragged to a new position by the user after initialization, breakUp() first establishes the present position of the puzzle and assigns it to the variables puzzLeft and puzzTop. After piece dispersion, the puzzle will be solved from scratch, so pieceToSolve becomes 1. If the user pressed the Break button after choosing a different piece count from the drop down menus (isNewPuzz is true) and the puzzle is already in a broken state (isBroken is true), then some cleanup is performed by calling allDone(). Next breakUp() checks to see if a new puzzle is called for, either because a new image has been loaded or the user has changed the piece count. In either case, isNewPuzz will be true, so createPuzzle() is called, starting the routines described on the previous page.

    function breakUp() { puzzLeft = elPuzzle.left + bordWidth; puzzTop = elPuzzle.top + bordWidth; pieceToSolve = 1; if (isNewPuzz && isBroken) { allDone(false) }; if (isNewPuzz) { createPuzzle() }; if (isPuzzDraggable) { elPuzzle.draggable = false } elBlank.clip.right = puzzWidth; elBlank.clip.bottom = puzzHeight; elBlank.visibility = "show"; startL = 0; startT = elPuzzle.top; endL = window.innerWidth - puzzWidth; endT = elPuzzle.top + elPuzzle.clip.height - puzzHeight; for (i=1; i<=puzzPieces; i++) { putL = getRandNums(startL,endL); putT = getRandNums(startT,endT); tempEl = eval("PIECE" + i); tempEl.left = putL; tempEl.top = putT; tempEl.draggable = true; tempEl.visibility = "show"; } if (isGrid) { elGrid.visibility = "show" } isBroken = true; solvedCount = 1; }

When we return from createPuzzle(), all our puzzle pieces have been created, and are properly loaded and clipped. At this point, the full puzzle drag is disabled to enable puzzle solving. The white elBlank element is clipped to the puzzle image size and made visible, hiding the image and creating a workspace for the user.

Unlike IE4, we cannot calculate the visible page coordinates, so we disperse our pieces between the top and bottom of the puzzle, vertically, and across the page, horizontally. We set variables that store the possible left and top, start and end page coordinates, and enter a loop that will return random numbers in the requested range. Each piece element is positioned in an acceptable location using the random coordinates, it is made draggable through its draggable property, and finally made visible to the user.

If grid use is enabled, the grid is displayed, isBroken and solvedCount are updated and the puzzle is ready for solving.

The random number generator function is reproduced below for reference.

    function getRandNums(from,to){ temp = parseInt((Math.random() * (to-from)) + (from)); while (isNaN(temp)) { temp = parseInt((Math.random() * (to - from)) + (from)) } return temp }

The Grid

You will recall, from the previous columns, that our grid is a GIF image of a... grid with nine horizontal and nine vertical divisions. Whenever we need to resize the grid, the setGrid() function is called. Here, we give it a new width and height and stretch it to provide the appropriate size divisions that correspond to the piece sizes.

    function setGrid() { gridWidth = (9/puzzAcross) * puzzWidth; gridHeight = (9/puzzDown) * puzzHeight; elGrid.document.write('<IMG NAME="imGrid" SRC="grid.gif" WIDTH='+gridWidth+' HEIGHT='+gridHeight+'>'); elGrid.document.close(); gridBut.value = "Grid ON"; if (isBroken) { elGrid.visibility = "show" } }

Once we establish what our new grid size should be (gridWidth/gridHeight), we reload the grid GIF into the elGrid element with the new size values. Since the grid image is cached, this is instantaneous. We alert the user to the fact that the grid is now "on," by changing the display of the grid toggle button. Finally, if the puzzle is broken, and solving is called for, we display the grid.

At this point, the user is faced with dispersed puzzle pieces, possibly a grid to assist in solving, and can begin dragging the puzzle pieces to their correct location.

Produced by Peter Belesis and

All Rights Reserved. Legal Notices.
Created: Nov. 27, 1997
Revised: Jan. 18, 1998

URL: http://www.webreference.com/dhtml/column10/puzzNSbreak.html