//=====================================================================
// All Out by Snowdog Software                             (AO-v106.js)
//=====================================================================
//     Copyright © 1999-2001 by Robert Carl Brown (dad@katieb.com)
//=====================================================================
//
//                         Change Log
//	                       ==========
//
//	Ver   Init  Date        Description
//	----  ----  ----------  -------------------------------------------
//	1.00  RCB   1999-01-15  Original Version
//	1.01  RCB   1999-01-16  Add 'iCols' and 'iRows'
//	1.02  RCB   1999-01-16  Added 'Z-Shape'
//	1.03  RCB   1999-01-19  Moved Script to Top
//	                        Added '/-Slash' and '\-BackSlash'
//	1.04  RCB   1999-01-23  Move Recorder
//	                        Added 'Checkers', 'HLines', and 'VLines'
//							Added Border to Images
//	1.05  RCB   1999-01-24  Layout Changes
//	1.06  RCB   2001-02-01  Layout Changes for RobertCarlBrown.com
//							Migrated JavaScript to 'AO-v106.js'
//
//=====================================================================

//	Global Variables
//	================
	var iGame       = 0;
	var iMinTurns   = 5;
	var iTurn       = 0;
	var bBoard      = new Array();
	var bMove       = new Array();
	var bGameOver   = false;

//	Event Handlers
//	==============
function window_onLoad() {
	iGame  = 0;
	
	ResetBoard();
}

function cboBoard_onChange() {
	oCombo = document.frmAllOut.cboBoard; 
	iGame  = oCombo.options[oCombo.options.selectedIndex].value;
	iGame  = Number(iGame);
	
	iTurn  = 0;

	ResetBoard();
	
	return true;
}

function cmdReset_onclick() {
	ResetBoard();
}

function cmdMoves_onclick() {
    RePaintMoves();

	setTimeout("RePaint();", 2000);
}

function img_onClick(x,y) {
 	var index = x + (y*iCols);
	
	if (bGameOver) {
		alert("Game Over\r\nTry a Different Board or Reset and Try this one Again");
	}
	else {
		bBoard[index] = !bBoard[index];
		bMove[index]  = !bMove[index];

		if (x > 0) bBoard[index-1] = !bBoard[index-1];
		if (x < (iCols-1)) bBoard[index+1] = !bBoard[index+1];
		if (y > 0) bBoard[index-iCols] = !bBoard[index-iCols];
		if (y < (iRows-1)) bBoard[index+iCols] = !bBoard[index+iCols];

		iTurn++;

		DisplayMessage('Turn ' + iTurn);
	
		RePaint();
	
		if (GameOver()) {
			if (iTurn == iMinTurns) {
				DisplayMessage("Congrats, Game Over in Minimum Turns (" + iTurn  + ")");
			}
			else {
				DisplayMessage("Game Over in " + iTurn  + " Turns");
			}
			
			bGameOver = true;
		}
	}
};

//	Public Functions
//	================
function DisplayMessage(msg) {
	document.frmAllOut.txtMessage.value = msg;
}

function GameOver() {
	var iCell;
	var iCount = 0;
	
	for (iCell=0; (iCell < CellCount()); iCell++) {
		if (bBoard[iCell]) iCount++;
	}
	
	return (iCount == 0);
}

function RePaint() {
	var iCell;
	
/*	
	for (iCell=0; (iCell < CellCount()); iCell++) {
		if (bBoard[iCell]) 
			document.images[iCell].src = imgOn.src;
		else
			document.images[iCell].src = imgOff.src;
	}
*/

	var iCol;
	var iRow;
	
	iCell=0;
	for (iRow=0; (iRow < iRows); iRow++) {
		for (iCol=0; (iCol < iCols); iCol++) {
			if (bBoard[iCell]) 
				document.images['img' + iCol + iRow].src = imgOn.src;
			else
				document.images['img' + iCol + iRow].src = imgOff.src;
		
			iCell++;
		}
	}
	
	return true;
}

function RePaintMoves() {
	var iCell;

/*	
	for (iCell=0; (iCell < CellCount()); iCell++) {
		if (bMove[iCell]) 
			document.images[iCell].src = imgMove.src;
		else
			document.images[iCell].src = imgOff.src;
	}
*/

	var iCol;
	var iRow;
	
	iCell=0;
	for (iRow=0; (iRow < iRows); iRow++) {
		for (iCol=0; (iCol < iCols); iCol++) {
			if (bMove[iCell]) 
				document.images['img' + iCol + iRow].src = imgMove.src;
			else
				document.images['img' + iCol + iRow].src = imgOff.src;
		
			iCell++;
		}
	}
	
	return true;
}

function ResetBoard() {
	var iCell = 0;

	for (iCell=0; (iCell < CellCount()); iCell++) {
		bBoard[iCell] = false;
		bMove[iCell]  = false;
	}

 	iMinTurns = 0;

	if (iGame == 0) {
	//	ALL ON
	//	------
		for (iCell=0; (iCell < CellCount()); iCell++)
			bBoard[iCell] = true;
		iMinTurns = 5;	// Only True for 3x3
	}
	else if (iGame == 1) {
	//	Square
	//	------	
		for (iCol=0; (iCol < iCols); iCol++) {
		//	Top, Bottom
			setBoard(iCol, 0, true);
			setBoard(iCol, iRows-1, true);
		}
		for (iRow=0; (iRow < iRows); iRow++) {
		//	Left, Right
			setBoard(0, iRow, true);
			setBoard(iCols-1, iRow, true);
		}
		iMinTurns = 8;	// Only True for 3x3
	}			
	else if (iGame == 2) {
	//	Plus
	//	----
		iCol=Math.round((iCols-1) / 2);
		for (iRow=0; (iRow < iRows); iRow++) {
		//	Vertical
			setBoard(iCol, iRow, true);
		}
		iRow=Math.round((iRows-1) / 2);
		for (iCol=0; (iCol < iCols); iCol++) {
		//	Horizontal
			setBoard(iCol, iRow, true);
		}
		iMinTurns = 1;	// Only True for 3x3
	}
	else if (iGame == 3) {
	//	Corners
	//	-------
		setBoard(0, 0, true);
		setBoard(iCols-1, 0, true);
		setBoard(0, iRows-1, true);
		setBoard(iCols-1, iRows-1, true);
		iMinTurns = 4;	// Only True for 3x3
	}
	else if (iGame == 4) {
	//	Diamond
	//	-------
		var iDX      = 1;
		var iDY      = 1;
		var iColDone = false;
		var iRowDone = false;
				
		iCol = Math.round((iCols-1)/2);
		iRow = 0;
				
		for (; (!(iColDone && iRowDone)); ) {
			setBoard(iCol, iRow, true);
				
			iCol = iCol + iDX;
			if (iCol < 0) {
				iCol     = 1;
				iDX      = 1;
				iColDone = true;
			}
					
			if (iCol >= iCols) {
				iCol = iCols-2;
				iDX  = -1;
			}
					
			iRow = iRow + iDY;
			if (iRow < 0) {
				iDY      = 1;
				iRow     = 1;
				iRowDone = true;
			}
					
			if (iRow >= iRows) {
				iRow = iRows-2;
				iDY  = -1;
			}
		}
				
		iMinTurns = 4;
	}
	else if (iGame == 5) {			
	//	Cross
	//	-----
		iCol=0;
		for (iRow=0; (iRow < iRows); iRow++) {
			setBoard(iCol, iRow, true);
			iCol++;
			if (iCol >= iCols) iCol = iCols-1;
		}
		iCol=iCols-1;
		for (iRow=0; (iRow < iRows); iRow++) {
			setBoard(iCol, iRow, true);
			iCol--;
			if (iCol < 0) iCol = 0;
		}
		iMinTurns = 9;	// Only True for 3x3
	}
	else if (iGame == 6) {
	//	Z-Shape
	//	-------	
		for (iCol=0; (iCol < iCols); iCol++) {
		//	Top, Bottom
			setBoard(iCol, 0, true);
			setBoard(iCol, iRows-1, true);
		}
		iCol=0;
		for (iRow=0; (iRow < iRows); iRow++) {
		//	Left, Right
			setBoard(iCol, iRow, true);
			iCol++;
			if (iCol >= iCols) iCol = (iCols-1);
		}
		iMinTurns = 0;	// Only True for 3x3
	}
	else if (iGame == 7) {
	//	/-Slash
	//	-------	
		iCol=iCols-1;
		for (iRow=0; (iRow < iRows); iRow++) {
		//	Left, Right
			setBoard(iCol, iRow, true);
			iCol--;
			if (iCol < 0) iCol = 0;
		}
		iMinTurns = 0;	// Only True for 3x3
	}
	else if (iGame == 8) {
	//	/-BackSlash
	//	-----------	
		iCol=0;
		for (iRow=0; (iRow < iRows); iRow++) {
		//	Left, Right
			setBoard(iCol, iRow, true);
			iCol++;
			if (iCol >= iCols) iCol = (iCols-1);
		}
		iMinTurns = 0;	// Only True for 3x3
	}
	else if (iGame == 9) {
	//	Checkboards
	//	-----------	
		for (iRow=0; (iRow < iRows); iRow+=2) {
			for (iCol=0; (iCol < iCols); iCol+=2) {
				setBoard(iCol, iRow, true);
				setBoard(iCol+1, iRow+1, true);
			}
		}
		iMinTurns = 0;	// Only True for 3x3
	}
	else if (iGame == 10) {
	//	H Lines
	//	-------
		for (iRow=0; (iRow < iRows); iRow+=2) {
			for (iCol=0; (iCol < iCols); iCol++) {
				setBoard(iCol, iRow, true);
			}
		}
		iMinTurns = 0;	// Only True for 3x3
	}
	else if (iGame == 11) {
	//	V Lines
	//	-------
		for (iCol=0; (iCol < iCols); iCol+=2) {
			for (iRow=0; (iRow < iRows); iRow++) {
				setBoard(iCol, iRow, true);
			}
		}
		iMinTurns = 0;	// Only True for 3x3
	}
	else if (iGame == 99) {			
	//	Random
	//	------
		for (iCell=0; (iCell < (iCols * iRows)); iCell++)
			bBoard[iCell] = randBool();
		iMinTurns = 0;	// Only True for 3x3
	}
	else {
	//	BAD
	//	----
		bBoard[0] = true;
		bBoard[1] = true;
		bBoard[2] = true;
		bBoard[6] = true;
		bBoard[7] = true;
		bBoard[8] = true;
		iMinTurns = 0;
	}

	RePaint();

	iTurn     = 0;
 	iMinTurns = 0;
	bGameOver = false;
	
//	DisplayMessage("Try for MINIMUM Turns of " + iMinTurns);
	DisplayMessage("Try for MINIMUM Turns");

	return true;
}

function CellCount() {
	return (iCols * iRows);
}
	
function setBoard(iCol, iRow, bVal) {
	if ((iCol >= 0) && (iCol < iCols)) {
		if ((iRow >= 0) && (iRow < iRows)) {
			bBoard[iCol + (iRow * iCols)] = bVal;
		}
	}
}

function randBool() {
	return (Math.random() < .5)
}
