var hasAnyOneWon=0;
var colour=1;
var xcolour=2;
var turns=0;
var domain="http://www.aurochs.org/games/cowthello/";
var blank=domain+"cowthelloBlank.gif";
var black=domain+"cowthelloPiebald.gif";
var white=domain+"cowthelloBrown.gif";
var cango=domain+"cowthelloWhite.gif";
var blank2=domain+"cowthelloWhite.gif"; //duplicate of blank:computerGo changes blank2 when computer is thinking

var board=new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
var boardBackUp=new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
var boardBackUpReverse=new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,1,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
var xboard=new Array(64);
var corner=100;
var edge=10;
var helpCorner=25;
var helpHelp=50
var nextCorner=-25;
var normal=1;
var helpEdge=5;
var nextNext=-50;
var boardValues=new Array(corner,nextCorner,helpCorner,edge,edge,helpCorner,nextCorner,corner,nextCorner,nextNext,normal,normal,normal,normal,nextNext,nextCorner,helpCorner,normal,helpHelp,helpEdge,helpEdge,helpHelp,normal,helpCorner,edge,normal,helpEdge,normal,normal,helpEdge,normal,edge,edge,normal,helpEdge,normal,normal,helpEdge,normal,edge,helpCorner,normal,helpHelp,helpEdge,helpEdge,helpHelp,normal,helpCorner,nextCorner,nextNext,normal,normal,normal,normal,nextNext,nextCorner,corner,nextCorner,helpCorner,edge,edge,helpCorner,nextCorner,corner);
var boardValuesBackUp=new Array(corner,nextCorner,helpCorner,edge,edge,helpCorner,nextCorner,corner,nextCorner,nextNext,normal,normal,normal,normal,nextNext,nextCorner,helpCorner,normal,helpHelp,helpEdge,helpEdge,helpHelp,normal,helpCorner,edge,normal,helpEdge,normal,normal,helpEdge,normal,edge,edge,normal,helpEdge,normal,normal,helpEdge,normal,edge,helpCorner,normal,helpHelp,helpEdge,helpEdge,helpHelp,normal,helpCorner,nextCorner,nextNext,normal,normal,normal,normal,nextNext,nextCorner,corner,nextCorner,helpCorner,edge,edge,helpCorner,nextCorner,corner);
var whichSquare=new Array(65);
var validMoves=new Array(65);

function resetFish()
	{
	for (i=0;i<64;i++)
		{
		board[i]=boardBackUp[i];
		boardValues[i]=boardValuesBackUp[i];
		if (document.score.reverse.checked) {board[i]=boardBackUpReverse[i];}
		}
	if (document.score.whogoes[0].checked==true) {colour=1;xcolour=2;document.score.humanColour.src=black;document.score.computerColour.src=white}
	if (document.score.whogoes[1].checked==true) {colour=2;xcolour=1;document.score.humanColour.src=white;document.score.computerColour.src=black}
	men=displayBoard();
	for (j=0;j<64;j++) {xboard[j]=board[j]};
	validMoves=selectSquare(colour);
	hasAnyOneWon=0;
	}


function playerTurn(selectedSquare)
	{
	cgYes=document.computerGo.src;
	moveYes=0;
	if (cgYes==blank) {moveYes=move(colour,selectedSquare)};
	if (moveYes!=0)
		{
		men=displayBoard();
		document.computerGo.src=blank2;
		setTimeout("computerTurn()",1000);
		}
	else {if (cgYes==blank) {alert("You cannot go there")}}
	return board;
	}

function computerTurn()
	{
	document.computerGo.src=blank2;
	xcolour=1; if (colour==1) {xcolour=2};
	for (j=0;j<64;j++) {xboard[j]=board[j]};
	validMoves=selectSquare(xcolour);
	if (validMoves[64]>-1) {fgh=move(xcolour,validMoves[64])}
	else
		{
		for (j=0;j<64;j++) {xboard[j]=board[j]};
		opponentsValidMoves=selectSquare(colour);
		if (opponentsValidMoves[64]>-1) {alert ("I cannot go! Your turn.")}
		else {if (hasAnyOneWon==0) {endGame()}}
		}
	men=displayBoard();
	//Test whether player can go
	for (j=0;j<64;j++) {xboard[j]=board[j]};
	validMoves=selectSquare(colour);
	if (validMoves[64]>-1) {document.computerGo.src=blank;return board;}
	else
		{
		for (j=0;j<64;j++) {xboard[j]=board[j]};
		opponentsValidMoves=selectSquare(xcolour);
		if (opponentsValidMoves[64]>-1) {alert("You cannot move at all. Sorry. My go...");computerTurn()}
		else {if (hasAnyOneWon==0) {endGame()}}
		}
	}

function selectSquare(sScolour)
	{
	for (i=0;i<64;i++)
		{
		whichSquare[i]=move(sScolour,i);
		for (j=0;j<64;j++) {board[j]=xboard[j]};
		}
	chosenOne=0; highScore=0;
	for (i=0;i<64;i++)
		{
		if (whichSquare[i]>highScore) {chosenOne=i;highScore=whichSquare[i]}
		}
	if (highScore==0) {chosenOne=-1}
	whichSquare[64]=chosenOne;
	return whichSquare;
	}



function move(mcolour,mselectedSquare)
	{
	north=doMove(mcolour,mselectedSquare,-8);
	northEast=doMove(mcolour,mselectedSquare,-7);
	east=doMove(mcolour,mselectedSquare,1);
	southEast=doMove(mcolour,mselectedSquare,9);
	south=doMove(mcolour,mselectedSquare,8);
	southWest=doMove(mcolour,mselectedSquare,7)
	west=doMove(mcolour,mselectedSquare,-1);
	northWest=doMove(mcolour,mselectedSquare,-9);
	if (north+northEast+east+southEast+south+southWest+west+northWest==0||board[mselectedSquare]!=0) {return 0} else {
	board[mselectedSquare]=mcolour;
	for (k=1;k<8;k++)
		{
		if (north>=k) {board[mselectedSquare+(k*-8)]=mcolour}
		if (northEast>=k) {board[mselectedSquare+(k*-7)]=mcolour}
		if (east>=k) {board[mselectedSquare+(k*1)]=mcolour}
		if (southEast>=k) {board[mselectedSquare+(k*9)]=mcolour}
		if (south>=k) {board[mselectedSquare+(k*8)]=mcolour}
		if (southWest>=k) {board[mselectedSquare+(k*7)]=mcolour}
		if (west>=k) {board[mselectedSquare+(k*-1)]=mcolour}
		if (northWest>=k) {board[mselectedSquare+(k*-9)]=mcolour}
		}
	scoreElephant=evaluateBoard(board,mcolour);
	return scoreElephant;
	}}

function evaluateBoard(board,eBcolour)
	{
	evaluateScore=0;
	for (m=0;m<64;m++)
	{
	if (board[m]==eBcolour) {evaluateScore=evaluateScore+boardValues[m]}
	}
	if (evaluateScore<0) {evaluateScore=0.1};
	return evaluateScore;
	}

function doMove(dMcolour,dMselectedSquare,dMdirection)
	{
	sS=dMselectedSquare;
	count=0; dMycolour=1; if (dMcolour==1) {dMycolour=2};
	hitEdge=0;
	while (board[sS]==dMycolour||sS==dMselectedSquare)
		{
		row=0;dir=0;
		if (sS==0||sS==8||sS==16||sS==24||sS==32||sS==40||sS==48||sS==56) {row=1};
		if (dMdirection==-9||dMdirection==7||dMdirection==-1) {dir=1}
		if (sS==7||sS==15||sS==23||sS==31||sS==39||sS==47||sS==55||sS==63) {row=7};
		if (dMdirection==9||dMdirection==-7||dMdirection==1) {dir=2}
		if (row==1&&dir==1) {hitEdge=1}
		if (row==7&&dir==2) {hitEdge=1}
		sS=sS+dMdirection;
		if (sS<0||sS>63) {hitEdge=1}
		count++;
		};
	if (count==1) {count=0}
	if (board[sS]==0||hitEdge==1) {count=0};
	return count;
	}

function displayBoard()
	{
	//also counts pieces and returns count as array men; also adjusts boardValues when corners captured via function alterBoardValues
	men=new Array(0,0,0);
	for (i=0;i<64;i++)
	{
	switch(board[i])
	{case 0:dBcolour=blank;men[0]++;break;
	case 1:dBcolour=black;men[1]++;break;
	case 2:dBcolour=white;men[2]++;break;}
	document.images[i].src=dBcolour;
	}
	humanScore=""+men[colour];
	computerScore=""+men[xcolour];
	if (humanScore.length==1) {humanScore="0"+humanScore}
	if (computerScore.length==1) {computerScore="0"+computerScore}
	document.human1.src="arialno"+humanScore.charAt(0)+".gif";
      document.human2.src="arialno"+humanScore.charAt(1)+".gif";
	document.computer1.src="arialno"+computerScore.charAt(0)+".gif";
      document.computer2.src="arialno"+computerScore.charAt(1)+".gif";
	boardValues=alterBoardValues(0,1,2,8,16,9,18);
	boardValues=alterBoardValues(7,5,6,15,23,14,21);
	boardValues=alterBoardValues(56,48,40,57,58,49,42);
	boardValues=alterBoardValues(63,61,62,55,47,54,45);
	return men;
	}

function alterBoardValues(cornerName,edge1,edge2,edge3,edge4,nextName,helpName)
	{
	if (board[cornerName]>0)
		{
		boardValues[edge1]=edge;
		boardValues[edge2]=edge;
		boardValues[edge3]=edge;
		boardValues[edge4]=edge;
		boardValues[nextName]=normal;
		boardValues[helpName]=helpEdge;
		}
	return boardValues;
	}

function endGame()
	{
	men=displayBoard();
	if (men[0]>0) {earlyEnd="No-one can move. "} else {earlyEnd=""}
	if (men[colour]>men[xcolour]) {winner="you";winnerString="Well done!"} else {winner="I";winnerString="Bad luck."}
	if (men[colour]==men[xcolour]) {winner="nobody";winnerString="We are equally matched!"}
	menString=earlyEnd+"Game over! I scored "+men[xcolour]+", you scored "+men[colour]+", so "+winner+" won. "+winnerString;
	alert(menString);
	hasAnyOneWon=1;
	}

function waxOn(square)
	{
	if (validMoves[square]>0&&document.computerGo.src==blank) {document.images[square].src=cango}
	}

function waxOff(square)
	{
	if (document.images[square].src==cango) {document.images[square].src=blank}
	}

function onLoadRoutine() {
		displayBoard();
		for (j=0;j<64;j++) {
			xboard[j]=board[j];
		}
		validMoves=selectSquare(colour);
	}

function breakFrames() {
	if (self.parent.frames.length != 0) {
			self.parent.location=document.location;
	}
}
