var Pic = new Array(); var folder = ""; //"Images"; //This is the path to the images folder var index = 0; var bScrambled = false; var nWhiteIndex; var bIsFilter = false; var PuzzlePath = "puzzleimg.asp?id=504&grp=153&" var StartTime var gTimerID //======================================================================================== //Before doing anything, Pic[] is populated with the paths of all the game piece graphics. //======================================================================================== for (var i = 0; i <= 16; i++) eval("Pic[" + i + "] = '" + PuzzlePath + "piece=" + i + "'"); //===================================================================================== //This function is run when the page first loads. It populates the game table with all //the pictures in their starting positions. //===================================================================================== function Load() { for (var i = 1; i <= 16; i++) { eval("Img" + i + ".src = Pic[" + i + "]"); //eval("Img" + i + ".style.filter = 'progid:DXImageTransform.Microsoft.Fade()'"); } //for } //Load //======================================================================================= //This function is called when the player clicks the "Scramble!" button. Its job is to //randomly arrange all the pieces of the game. If the game has not already been started //when this function is called, it inserts a white space in the bottom left corner of the //board before randomizing the pieces. To randomize the pieces, a series of legal moves //is performed at random to ensure that the game is solvable. //======================================================================================= function Scramble () { var nMove; var nNewIndex = -1; var sTemp; var nNumMoves = 200; //If the board has not already been scrambled, replace the bottom left corner of the //board with the white space if (!bScrambled) { nWhiteIndex = 16; eval("Pic[" + 16 + "] = '0.gif'"); bScrambled = true; } //if //This loop performs nNumMoves random moves in Pic for (var i = 0; i < nNumMoves; i++) { while (nNewIndex == -1) { nMove = Math.floor((Math.random()*4))+1; if (nMove == 1) nNewIndex = Up(nWhiteIndex); else if (nMove == 2) nNewIndex = Down(nWhiteIndex); else if (nMove == 3) nNewIndex = Left(nWhiteIndex); else nNewIndex = Right(nWhiteIndex); } //while //swap the images in the Pic array sTemp = Pic[nNewIndex]; Pic[nNewIndex] = Pic[nWhiteIndex]; Pic[nWhiteIndex] = sTemp; nWhiteIndex = nNewIndex; nNewIndex = -1; } //for //Now that Pic has been randomized, update the game board with its contents for (var i = 1; i <= 16; i++) eval("Img" + i + ".src = Pic[" + i + "]"); StartTime = new Date() gTimerID = setInterval("PuzzleTimer()", 1000) } //Scramble function PuzzleTimer() { var nMinute = Math.floor((new Date() - StartTime) / 60000) var nSeconds = Math.floor(((new Date() - StartTime) % 60000) / 1000); var sTmr = nMinute + ":" if (nSeconds > 9) sTmr += nSeconds; else sTmr += "0" + nSeconds; tmrPuzzle.innerHTML = sTmr; } //=========================================================================================== //This function moves nImg into the white space if that space is adjacent it it. The indexes //of both squares are swapped in Pic[], and the images of the two pieces are swapped on //the game board. //=========================================================================================== function Move(nImg) { var nIndexUp = Up(nImg); var nIndexDown = Down(nImg); var nIndexLeft = Left(nImg); var nIndexRight = Right(nImg); //The following series of if statements checks to see if the white space is adjacent to //nImg. If it is, their indexes are swapped, and the Swap() function is called to switch //the images on the game board if (nIndexUp != -1) if (Pic[nIndexUp] == "0.gif") Swap(nImg, nIndexUp); if (nIndexDown != -1) if (Pic[nIndexDown] == "0.gif") Swap(nImg, nIndexDown); if (nIndexLeft != -1) if (Pic[nIndexLeft] == "0.gif") Swap(nImg, nIndexLeft); if (nIndexRight != -1) if (Pic[nIndexRight] == "0.gif") Swap(nImg, nIndexRight); //After a move has been made, check to see if the game has been completed. If it has, //replace the white space with the final image and alert the player that the game //has been completed if (IsComplete()) { //Img13.filters[0].apply(); Img16.src = (PuzzlePath + "piece=16"); Pic[16] = (PuzzlePath + "piece=16"); Pic[0] = ("0.gif"); //Img13.filters[0].play(); bScrambled = false; clearInterval(gTimerID) alert("Great Job! To play again, just push the 'Scramble!' button"); } //if } //Move //=================================================================================== //This function tests to see whether or not the game has been successfully completed. //=================================================================================== function IsComplete () { if ((Pic[1] == (PuzzlePath + "piece=1")) && (Pic[2] == (PuzzlePath + "piece=2")) && (Pic[3] == (PuzzlePath + "piece=3")) && (Pic[4] == (PuzzlePath + "piece=4")) && (Pic[5] == (PuzzlePath + "piece=5")) && (Pic[6] == (PuzzlePath + "piece=6")) && (Pic[7] == (PuzzlePath + "piece=7")) && (Pic[8] == (PuzzlePath + "piece=8")) && (Pic[9] == (PuzzlePath + "piece=9")) && (Pic[10] == (PuzzlePath + "piece=10")) && (Pic[11] == (PuzzlePath + "piece=11")) && (Pic[12] == (PuzzlePath + "piece=12")) && (Pic[13] == (PuzzlePath + "piece=13")) && (Pic[14] == (PuzzlePath + "piece=14")) && (Pic[15] == (PuzzlePath + "piece=15")) && (Pic[16] == ("0.gif"))) return true; else return false; } //IsComplete //==================================================================================== //This function switches the images in the nSource and nDestination indexes of the Pic //array. If bIsFilter is false, then the player has indicated that they do not want //animations to run. //==================================================================================== function Swap (nSource, nDestination) { if (bIsFilter) { eval("Img" + nSource + ".filters[0].apply()"); eval("Img" + nDestination + ".filters[0].apply()"); } //if var sTemp = Pic[nSource]; Pic[nSource] = Pic[nDestination]; Pic[nDestination] = sTemp; eval("Img" + nSource + ".src = Pic[" + nSource + "]"); eval("Img" + nDestination + ".src = Pic[" + nDestination + "]"); if (bIsFilter) { eval("Img" + nSource + ".filters[0].play()"); eval("Img" + nDestination + ".filters[0].play()"); } //if } //Swap //========================================================================================== //When the player chooses a different animation style, this function applies the appropriate //filter to all the game pieces. If the player selects "None", the bIsFilter variable is //set to false so that no filters will be used in the Swap function. //========================================================================================== function Animate (nStyle) { return var sFilter = ".style.filter = \'progid:DXImageTransform.Microsoft." switch (nStyle.value) { case "Barn" : sFilter += "Barn()\'"; break case "Blinds" : sFilter += "Blinds()\'"; break case "Checker" : sFilter += "Checkerboard()\'"; break case "Fade" : sFilter += "Fade()\'"; break case "Gradient" : sFilter += "GradientWipe(GradientSize=0.25,wipestyle=0,motion=forward)\'"; break case "Inset" : sFilter += "Inset()\'"; break case "Iris" : sFilter += "Iris()\'"; break case "Radial" : sFilter += "RadialWipe()\'"; break case "Bars" : sFilter += "RandomBars()\'"; break case "Dissolve" : sFilter += "RandomDissolve()\'"; break case "Spiral" : sFilter += "Spiral()\'"; break case "Stretch" : sFilter += "Stretch()\'"; break case "Strips" : sFilter += "Strips()\'"; break case "Wheel" : sFilter += "Wheel()\'"; break } //switch if (nStyle.value == "None") bIsFilter = false; else { bIsFilter = true; for (var i = 1; i <= 16; i++) eval("Img" + i + sFilter); } //else } //Animate //========================================================================= //This function returns the index in the Pic array of the square above nImg //========================================================================= function Up (nImg) { if ((nImg == 1) || (nImg == 2) || (nImg == 3) || (nImg == 4)) return -1; else return (nImg - 4); } //Up //========================================================================= //This function returns the index in the Pic array of the square below nImg //========================================================================= function Down (nImg) { if ((nImg == 13) || (nImg == 14) || (nImg == 15) || (nImg == 16)) return -1; else return (nImg + 4); } //Down //================================================================================== //This function returns the index in the Pic array of the square to the left of nImg //================================================================================== function Left (nImg) { if ((nImg == 1) || (nImg == 5) || (nImg == 9) || (nImg == 13)) return -1; else return (nImg - 1); } //Left //=================================================================================== //This function returns the index in the Pic array of the square to the right of nImg //=================================================================================== function Right (nImg) { if ((nImg == 4) || (nImg == 8) || (nImg == 12) || (nImg == 16)) return -1; else return (nImg + 1); } //Right