// pentplacer for main pagevar gImages = preLoadImages("images/Pentplacer/wall.gif", "images/Pentplacer/empty.gif", "images/Pentplacer/f.gif", "images/Pentplacer/l.gif", "images/Pentplacer/i.gif", "images/Pentplacer/p.gif", "images/Pentplacer/n.gif", "images/Pentplacer/t.gif", "images/Pentplacer/u.gif", "images/Pentplacer/v.gif", "images/Pentplacer/w.gif", "images/Pentplacer/x.gif", "images/Pentplacer/y.gif", "images/Pentplacer/z.gif");function preLoadImages() {	var tmp = new Array();			for( var i = 0; i < preLoadImages.arguments.length; i++) {		tmp[i] = new Image();		tmp[i].src = preLoadImages.arguments[i] ;	}	return tmp;}var gDImages = document.images;var gPieces = new Array(12); 				// the 12 pentominoes inof given is place on board of the// info given is the place an the board of the other 4 squares// after the upper-left most piece is placed// the data was based on my first working with an 8x8 board with a border// some work is needing in the piece placing now that the board size isi different// it was easier(??) than a new style of data storage	gPieces[0] = new Array (9,10,11,21, 1,9,10,20, 10,11,12,21, 10,11,19,20, 8,9,10,19, 1,11,12,21, 9,10,11,19, 9,10,20,21); //F	gPieces[1] = new Array (10,11,12,13, 10,20,29,30, 1,2,3,13, 1,10,20,30, 1,11,21,31, 1,2,3,10, 10,20,30,31, 7,8,9,10); //L	gPieces[2] = new Array (1,2,3,4, 10,20,30,40); //I	gPieces[3] = new Array (1,10,11,21, 1,2,10,11, 10,11,20,21, 1,9,10,11, 1,10,11,12, 9,10,19,20, 1,2,11,12, 1,10,11,20); //P	gPieces[4] = new Array (1,8,9,10, 10,11,21,31, 1,2,9,10, 10,20,21,31, 1,11,12,13, 10,19,20,29, 1,2,12,13, 9,10,19,29); //N	gPieces[5] = new Array (1,2,11,21, 8,9,10,20, 10,19,20,21, 10,11,12,20); //T	gPieces[6] = new Array (1,2,10,12, 1,11,20,21, 2,10,11,12); //U	gPieces[7] = new Array (10,20,21,22, 1,2,10,20, 10,18,19,20, 1,2,12,22); //V	gPieces[8] = new Array (10,11,21,22, 9,10,18,19, 1,11,12,22, 1,9,10,19); //W	gPieces[9] = new Array (9,10,11,20); //X	gPieces[10] = new Array (8,9,10,11, 9,10,20,30, 1,2,3,11, 10,20,21,30, 1,2,3,12, 10,11,20,30, 9,10,11,12, 10,19,20,30); //Y	gPieces[11] = new Array (1,10,19,20, 10,11,12,22, 1,11,21,22, 8,9,10,18); //Zvar gNumCols = 8;var gNumRows = 8;var gMaxCols = 20;var gMaxRows = 8;var gBrdLen = (gMaxCols+2) * (gMaxRows+2);var gRowOff, gColOff;var gNumBlocked = 0;var gEightByEight = false;var gBlocking = false;var gOffs = new Array(-(gMaxCols+2),gMaxCols+2, -1, 1);  // UP, DN, LT, RTvar gBrd = new Array(gBrdLen);var gPlaced = new Array(12);var gIntervalIDs = new Array();var gNumIntervals = (navigator.appName=="Microsoft Internet Explorer") ? 10 : 1;var gPNumList = new Array(12);var gOkToGo = false;function okPOrder() {	var i, OK = true;	var pStr = 'FLIPNTUVWXYZ';	var used = new Array(12);		for(i = 0; i<12; i++) {		used[i] = false;	}	for(i = 0; i < 12; i++) {		gPNumList[i] = pStr.indexOf( (eval('document.pentPlacerForm.p'+i).value).toUpperCase() ) ;		if (gPNumList[i] < 0 || gPNumList[i] > 11 || used[gPNumList[i]]) {			OK = false;			gPNumList[i] = -1;		}		else {			used[gPNumList[i]] = true;		}	}	if (!OK) {		alert('gotta fix some of those letters');	}	return OK;}function setPieceOrder() {	var i,tmp, a, b, which;	var pNames = new Array('F','L','I','P','N','T','U','V','W','X','Y','Z');	var F=0,L=1,I=2,P=3,N=4,T=5,U=6,V=7,W=8,X=9,Y=10,Z=11;	var how = document.pentPlacerForm.pOrder.options[document.pentPlacerForm.pOrder.selectedIndex].value ;			gOkToGo = false;	if (how == 'standard' || how == 'random') {		for (i = 0; i < 12; i++) {			gPNumList[i] = i;		}	}	if (how == 'standard') {		gOkToGo = true;	}		if (how == 'random') {		for (i = 0; i < 100; i++) {			a = Math.floor(12*Math.random());			b = Math.floor(12*Math.random());			tmp = gPNumList[a]			gPNumList[a] = gPNumList[b];			gPNumList[b] = tmp;		}		gOkToGo = true;	}		if (how =='specific') {		gOkToGo = okPOrder()	}		for (i = 0; i<12; i++) {		which = 'document.pentPlacerForm.p'+i; 		eval(which).value = pNames[gPNumList[i]] ;	}}	function initBoard() {	gRowOff = Math.floor( (gMaxRows+2 - gNumRows)/2 );	gColOff = Math.floor( (gMaxCols+2 - gNumCols)/2 );	// black it all out	for (var i = 0; i < gBrdLen; i++) {		gBrd[i] = -1;	}	// open board up	for (var row = 0; row < gNumRows; row++) {		for(var col = 0; col < gNumCols; col++) {			gBrd[(gMaxCols+2)*(row+gRowOff) + col + gColOff] = 0;		}	}	for (i = 0; i < 12; i++) {		gPlaced[i] = false;	}	document.pentPlacerForm.numPlaced.value = 0;	gNumBlocked = 0;	gEightByEight = (gNumRows == 8);	gBlocking = false;	return;}		function displayBoard() {	for (var i = 0; i < gBrdLen; i++) {		gDImages[i].src = gImages[(gBrd[i]+1)].src;	}	return;}	function clickAt(spot) { 	if (gBlocking && (gNumBlocked < 4) ) { 		if (!gBrd[spot]) { 			gNumBlocked++; 			gBrd[spot] = -1; 			gDImages[spot].src = gImages[0].src; 		} 		gBlocking = (gNumBlocked != 4); 	}	return;}function resetDoIt() {	// set state variables		doIt.spot = 0;	doIt.pnumIndex = -1;	doIt.orient = 0;	doIt.stack = new Array();	// set action words	doIt.orienting = false;	doIt.removing = false;	doIt.findingPiece = false;	doIt.findingSpot = false;		// set status words	doIt.foundSpot = false;	doIt.foundPiece = false;		return;}function startStop() { 	if (document.pentPlacerForm.goButt.value == 'Start') {		if (doIt.spot > 0) {			setPieceOrder();			initBoard();			displayBoard();		}		if (!gOkToGo) {				return;		}		resetDoIt();		if (gEightByEight) {			alert('please pick 4 stops to block out!!');			gBlocking = true;		}		for( var i = 0 ; i<gNumIntervals; i++) {			gIntervalIDs[i] = setInterval('doIt()', 1);		}		document.pentPlacerForm.goButt.value = 'Stop';		document.pentPlacerForm.pauseButt.value = 'Pause';	}	else {		for (var i = 0; i < gNumIntervals; i++ ) {			clearInterval(gIntervalIDs[i]);		}		document.pentPlacerForm.goButt.value = 'Start';		document.pentPlacerForm.pauseButt.value = 'Pause';	}		return;	}function pauseResume() {	if (document.pentPlacerForm.pauseButt.value == 'Pause') {		for (var i = 0; i < gNumIntervals; i++ ) {			clearInterval(gIntervalIDs[i]);		}				document.pentPlacerForm.pauseButt.value = 'Resume';	}	else {		for( var i = 0 ; i<gNumIntervals; i++) {			gIntervalIDs[i] = setInterval('doIt()', 1);		}				document.pentPlacerForm.pauseButt.value = 'Pause';	}	return;}/* so, the idea here is that the normal recursive thing did not 	work since browsers in general do not let go of events until 	they have finished so no screen display show up....	this way,-- doing things in little bits 		and checking back thru the setInterval 		is a way to allow the screen to be updated	no recursion, just putting on and popping off a stack	not as elegant but it will display well*/function doIt() { 	if (gBlocking || doIt.findingPiece || doIt.findingSpot || doIt.removing) { // in the middle of a process... come back later		return;	}		if (!doIt.foundSpot) { // find next spot to try		doIt.findingSpot = true;		doIt.foundSpot = false;		while( doIt.spot< gBrd.length && (gBrd[doIt.spot]!=0) ) {  // move past walls and already filled in spots			doIt.spot++;		}		if (doIt.spot == gBrd.length) { // made it to the end			setTimeout('alert("got one");', 5);			for( var i = 0 ; i<gNumIntervals; i++) {				gIntervalIDs[i] = setInterval('doIt()', 0.5);			}			return ;		}		doIt.pnumIndex = -1;		doIt.orient = 0;		doIt.foundSpot = true;		doIt.findingSpot = false;		return;	}		if (doIt.foundSpot && !doIt.foundPiece) {// found spot now find piece to try		 doIt.findingPiece = true;		 doIt.foundPiece = false;		 do {		 	doIt.pnumIndex++;		 	doIt.pnum = gPNumList[doIt.pnumIndex];		 } while ( (doIt.pnumIndex < 12) && gPlaced[doIt.pnum]);		if (doIt.pnumIndex > 11) { // nothing left to try pop state			doIt.removing = true;			doRemove();			doIt.removing = false;			doIt.findingPiece = false;			doIt.foundPiece = true;   // continue with last piece trying			return;		}		 doIt.orient = 0;		 doIt.foundPiece = true;		 doIt.findingPiece = false;		 return;	}		if (doIt.foundPiece) { // ok got piece now rotate it around....		if (!doIt.orienting) { 			if (doIt.orient < gPieces[doIt.pnum].length/4) {				doIt.orienting = true;				doIt.pieceList = buildList(doIt.spot, doIt.pnum, doIt.orient);				if (canPlace(doIt.pieceList) ) { // found orientation that works now place it (on stack and on board)					doIt.stack[doIt.stack.length] = new Array(doIt.spot, doIt.pnumIndex, doIt.orient, doIt.pieceList);					placePiece(doIt.pnum, doIt.pieceList);					doIt.foundSpot = false;					doIt.orienting = false;					doIt.foundPiece = false;					return;				}				else { // done with this orientation 					doIt.orient++;					doIt.orienting =false;					return;				} 			}			else { // done with this piece				doIt.orient = 0; 				doIt.foundPiece = false;				return;			}		}		return;	}	return;}function buildList(spot, piece, orient) {	var off, hor;	var index = 4*orient;	var okToPlace = true;	var list = new Array();		list[0] = spot;	for (var i = 0; i < 4; i++) {		off = (gMaxCols+2) * Math.floor(gPieces[piece][index+i] / 10 ) // take care of vertical stuff		hor = (gPieces[piece][index+i] % 10)		off += (hor<5) ? hor : (gMaxCols+2 - (10-hor) );		list[i+1] = spot+off; 	}	return list;}function canPlace(plist) { 	var okToPlace = true;		for (var i = 0; i < 5; i++) {		okToPlace &= (gBrd[plist[i]]==0); 	}	if (okToPlace && document.pentPlacerForm.smart.checked) {		okToPlace = noHolesP(plist);	}	return okToPlace;}function noHolesP(checkAround) {	var noHoles = true;	var numNeigh = 0;	var i,j,k, toCheck;	var theEmpty = 0;		// temporarily put mark on board	for (i = 0; i < 5; i++) {		gBrd[checkAround[i]] = 100;	}		for (i = 0; i < 5; i++) { 		for ( j = 0; j < 4; j++) { 			toCheck = checkAround[i] + gOffs[j];			if ( !gBrd[toCheck] ) {				numNeigh = 0;				for(k = 0; k <4; k++) { 					if (gBrd[toCheck + gOffs[k] ]) {						numNeigh++;					}					else {						theEmpty = toCheck + gOffs[k];					}				}				if (numNeigh > 3) {					noHoles = false;				}				if (numNeigh == 3) { // check for isolated 2 spot as well - since its easy					numNeigh = 0;					for(k = 0; k <4; k++) { 						if (gBrd[theEmpty + gOffs[k] ]) {							numNeigh++;						}					}					if (numNeigh == 3) {						noHoles = false;					}				}			}		}	}						// remove from board		for (i = 0; i < 5; i++) {		gBrd[checkAround[i]] = 0;	}			return noHoles;}function placePiece(piece, plist) { 		for(var i = 0; i < 5; i++) {		gBrd[plist[i]] = piece+1;		gDImages[plist[i] ].src = gImages[(piece+2 )].src;	}	document.pentPlacerForm.numPlaced.value++;	gPlaced[piece] = true;	return;}function doRemove() {			// pop stack then remove last piece placed	doIt.spot = doIt.stack[doIt.stack.length-1][0];	doIt.pnumIndex = doIt.stack[doIt.stack.length-1][1];	doIt.pnum = gPNumList[doIt.pnumIndex];	doIt.orient = doIt.stack[doIt.stack.length-1][2];	removePiece(doIt.pnum, doIt.stack[doIt.stack.length-1][3]);	doIt.stack.length--;	doIt.orient++;	return;}function removePiece(piece, plist) {  		for (var i = 0; i < 5; i++){		gBrd[plist[i]] = 0;		gDImages[plist[i]].src = gImages[1].src;	}	gPlaced[piece] = false;	return;}	function setNewBoard() {	for( var i = 0 ; i<gNumIntervals; i++) {		clearInterval(gIntervalIDs[i]);	}	var num = document.pentPlacerForm.brdSize.options[document.pentPlacerForm.brdSize.selectedIndex].value ;	gNumCols = Math.floor(num/10);	gNumRows = num % 10;	setPieceOrder();	initBoard();	displayBoard();	resetDoIt();	document.pentPlacerForm.goButt.value = 'Start';	document.pentPlacerForm.pauseButt.value = 'Pause';	return;}	
