/** Specialised Game Functions** &copy;Copyright Mike Brockington 2000 - 2009 All rights reserved.* Build Number:10:* This version first released: 14/01/2009*/
/** Display child window* @return Nothing*/
function small_window(myurl){var newWindow;var props='scrollBars=yes,resizable=yes,toolbar=no,menubar=no,location=no,directories=no,width=550,height=350';newWindow=window.open(myurl, "Rules", props);}/** Close child window* @return Nothing*/
function closeWindow(){window.close();}/** Setup of the board etc.* @return Nothing*/
function Clear_Board(){document.puzzle.txtMoves.value=0;setTimeout("Image_Swap(0,'yellow')", 1);setTimeout("Image_Swap(1,'yellow')", 50);setTimeout("Image_Swap(2,'yellow')", 100);setTimeout("Image_Swap(3,'empty')", 150);setTimeout("Image_Swap(4,'black')", 200);setTimeout("Image_Swap(5,'black')", 250);setTimeout("Image_Swap(6,'black')", 300);setTimeout("Image_Swap(9,'yellow')", 400);setTimeout("Image_Swap(10,'yellow')", 450);setTimeout("Image_Swap(11,'yellow')", 500);setTimeout("Image_Swap(12,'yellow')", 550);setTimeout("Image_Swap(13,'empty')", 600);setTimeout("Image_Swap(14,'black')", 650);setTimeout("Image_Swap(15,'black')", 700);setTimeout("Image_Swap(16,'black')", 750);setTimeout("Image_Swap(17,'black')", 800);setTimeout("Image_Swap(20,'yellow')", 900);setTimeout("Image_Swap(21,'yellow')", 950);setTimeout("Image_Swap(22,'yellow')", 1000);setTimeout("Image_Swap(23,'yellow')", 1050);setTimeout("Image_Swap(24,'yellow')", 1100);setTimeout("Image_Swap(25,'empty')", 1150);setTimeout("Image_Swap(26,'black')", 1200);setTimeout("Image_Swap(27,'black')", 1250);setTimeout("Image_Swap(28,'black')", 1300);setTimeout("Image_Swap(29,'black')", 1350);setTimeout("Image_Swap(30,'black')", 1400);return(false);}/** Initial setup* @return Nothing*/
function Start(){alert('Click any button, or one of the game pieces.\nIt will then move to the vacant space.');Clear_Board();return(false);}/** Only used by the manual version of the game* @return Nothing*/
function Try_Move(intPiece,intBoard){if (Check_Move(intPiece,intBoard)){Move_Piece(intPiece,intBoard);}else{alert("Sorry, that piece cannot be moved at the moment.\nCheck the rules for more info.")}if (Check_Complete(intBoard)){alert('Congratulations!\n\nYou have completed the puzzle.')}return(false);}/** Checks whether the piece is within two places of the empty position* @return Nothing*/
function Check_Move(intPiece,intBoard){return(Math.abs(Find_Space(intBoard) - intPiece) < 3)}/** Reveals the location of the space on the board* @return Nothing*/
function Find_Space(intBoard){var intSpace;if (intBoard==1){for(i=0;i < 7;i++) {if (document.images[('piece'+i)].src.indexOf('empty',0) > 0){intSpace=i};};};if (intBoard==2){for(i=9;i < 18;i++) {if (document.images[('piece'+i)].src.indexOf('empty',0) > 0){intSpace=i};};};if (intBoard==3){for(i=20;i < 31;i++) {if (document.images[('piece'+i)].src.indexOf('empty',0) > 0){intSpace=i};};};return(intSpace);}/** Moves the piece which is at the position given* @return Nothing*/
function Move_Piece(intPiece,intBoard){var intNewPlace=Find_Space(intBoard);if (!(intNewPlace==intPiece)) {document.puzzle.txtMoves.value++;var boolImageFound=(document.images[('piece'+intPiece)].src.indexOf('black',0) > 0);Image_Swap(intPiece,'empty');if (boolImageFound){Image_Swap(intNewPlace,'black');}else{Image_Swap(intNewPlace,'yellow');};}return(false);}/** Provides a slight 'animation'* @return Nothing*/
function Image_Swap(intPosition, strColour){var functRef1=Image_Swap_Inner(intPosition, "red.gif");var functRef2=Image_Swap_Inner(intPosition, strColour+'.gif');setTimeout(functRef1, 1);setTimeout(functRef2, 250);return(false);}/** Provides a closure so the variables don't get lost and/or confused when used with setTimeout()* @return anonymous function*/
function Image_Swap_Inner(intPosition, strColour){/* Return a reference to an anonymous inner function created with a function expression:- */
return (function(){/* This inner function is to be executed with - setTimeout and when it is executed it can read, and act upon, the parameters passed to the outer function:- */
document.images[('piece'+intPosition)].src=strColour;});}/*** @return Boolean value indicating whether puzzle has been completed*/
function Check_Complete(intBoard){if (intBoard==1){return(Check_Complete1())};if (intBoard==2){return(Check_Complete2())};if (intBoard==3){return(Check_Complete3())};}/** No need to check whether the centre space is blank, (if we assume that the rest of the code works corectly!)* @return Nothing*/
function Check_Complete1(){if ((document.images['piece0'].src.indexOf('black',0) > 0) && (document.images['piece1'].src.indexOf('black',0) > 0) && (document.images['piece2'].src.indexOf('black',0) > 0) && (document.images['piece4'].src.indexOf('yellow',0) > 0) && (document.images['piece5'].src.indexOf('yellow',0) > 0) && (document.images['piece6'].src.indexOf('yellow',0) > 0)){return(true)}else{return(false)};}/*** @return Nothing*/
function Check_Complete2(){if ((document.images['piece9'].src.indexOf('black',0) > 0) && (document.images['piece10'].src.indexOf('black',0) > 0) && (document.images['piece11'].src.indexOf('black',0) > 0) && (document.images['piece12'].src.indexOf('black',0) > 0) && (document.images['piece13'].src.indexOf('yellow',0) > 0) && (document.images['piece15'].src.indexOf('yellow',0) > 0) && (document.images['piece16'].src.indexOf('yellow',0) > 0) && (document.images['piece17'].src.indexOf('yellow',0) > 0)){return(true)}else{return(false)};}/*** @return Nothing*/
function Check_Complete3(){if ((document.images['piece20'].src.indexOf('black',0) > 0) && (document.images['piece21'].src.indexOf('black',0) > 0) && (document.images['piece22'].src.indexOf('black',0) > 0) && (document.images['piece23'].src.indexOf('black',0) > 0) && (document.images['piece24'].src.indexOf('black',0) > 0) && (document.images['piece26'].src.indexOf('yellow',0) > 0) && (document.images['piece27'].src.indexOf('yellow',0) > 0) && (document.images['piece28'].src.indexOf('yellow',0) > 0) && (document.images['piece29'].src.indexOf('yellow',0) > 0) && (document.images['piece30'].src.indexOf('yellow',0) > 0)){return(true)}else{return(false)};}/** (Very simple algorithm)* @return Nothing*/
function Random_Solution(){Clear_Board();var intPiece;while (!(Check_Complete(1))){intPiece=(Math.round(Math.random()*6));if (Check_Move(intPiece,1)){Move_Piece(intPiece,1);}}return(false);}/** (More complex algorithm)* @return Nothing*/
function Random_Solution2()/** Display child window* @return Nothing*/
{Clear_Board();var intPiece;var intLastPiece=3;var intSpace;while (!(Check_Complete(1))){intSpace=Find_Space(1);intPiece=(Math.round(Math.random()*6));if (!(intSpace==intPiece)) {if (intPiece!=intLastPiece) {if (Check_Move(intPiece)) {intLastPiece=intSpace;Move_Piece(intPiece,1);}}}}return(false);}/** Don't think this is the optimum solution* @return Nothing*/
function Calculated_Solution(){Clear_Board();setTimeout("Move_Piece(2,1)", 2300 );setTimeout("Move_Piece(4,1)", 3000 );setTimeout("Move_Piece(3,1)", 3800 );setTimeout("Move_Piece(5,1)", 4650 );setTimeout("Move_Piece(4,1)", 5500 );setTimeout("Move_Piece(3,1)", 6350 );setTimeout("Move_Piece(1,1)", 7200 );setTimeout("Move_Piece(2,1)", 8050 );setTimeout("Move_Piece(0,1)", 8900 );setTimeout("Move_Piece(1,1)", 9750 );setTimeout("Move_Piece(2,1)", 10600);setTimeout("Move_Piece(4,1)", 11450);setTimeout("Move_Piece(6,1)", 12300);setTimeout("Move_Piece(5,1)", 13150);setTimeout("Move_Piece(3,1)", 14000);setTimeout("Move_Piece(1,1)", 14850);setTimeout("Move_Piece(2,1)", 15700);setTimeout("Move_Piece(4,1)", 16550);setTimeout("Move_Piece(3,1)", 17400);return(false);}