var dodebug=false;
var movered=0;
var moveblack=0;
var whoseturn="r";
var numpieces=21;
var hpos=new Array(6,6,6,6,6,6,6);
var gameover=false;
var board=makeboard();
var compprefix="b";
function makerow()
{
return (new Array("e","e","e","e","e","e"));}
function makeboard()
{
return (new Array(makerow(),makerow(),makerow(),makerow(),makerow(),makerow(),makerow()));}

function twiceback()
{
doback();
doback();}

function doback()
{
if(movered==0)return;
gameover=false;
var n;
var x;
var piece;
if(whoseturn=="b")
	{
	n=--movered;
	piece=eval("document.getElementById('r"+n+"').style");
	x=getx(piece);
	piece.left=100;
	whoseturn="r";}
else
	{
	n=--moveblack;
	piece=eval("document.getElementById('b"+n+"').style");
	x=getx(piece);
	piece.left=675;
	whoseturn="b";	}
piece.top=(n*15+130);
board[x][hpos[x]]="e";
hpos[x]++;
turnupdate();}

function checkposition(e)
{
if(window.event)x=window.event.x;
else x=e.pageX;
x=Math.floor((x-208)/56);
if((x>=0)&&(x<7)&&(gameover==false))
	if(hpos[x]>0)
		{
		debug("-------------------------");
		debug("Player chooses "+x);
		domove(x);
		if(gameover==false)domove(computermove());		}
return(true);}

function domove(x)
	{
	if(hpos[x]>0)
		{ hpos[x]--;
		if(whoseturn=="r")
			{ board[x][hpos[x]]="r";
			whoseturn="b";
			var piece=eval("document.getElementById('r"+movered+"').style");
			movered++;}
		else { board[x][hpos[x]]="b";
			whoseturn="r";
			var piece=eval("document.getElementById('b"+moveblack+"').style");
			moveblack++;
			if(moveblack==numpieces)gameover=true; }
		piece.left=x*56+220;
		piece.top=hpos[x]*54+130;
		checkwin((whoseturn=="r")?"b":"r");
		if(gameover==false)turnupdate();
		//if(whoseturn=="b")debug(computermove());
		}
	else alert("Column "+x+" full");}
function turnupdate()
{
if(whoseturn=="r")
			{
			gamestatus.style.color="red";
			gamestatus.style.left=55;
			gamestatus.innerHTML="Red's turn";}
		else { gamestatus.style.color="#5555ff";
			gamestatus.innerHTML="Blue's turn";
			gamestatus.style.left=625;}}
function getx(piece) {
var temp=piece.left.substring(0,piece.left.length-2);
return(temp-220)/56;}

function checkwin(prefix)
{var lim=(whoseturn=="b")?movered:moveblack;
var x;
var y;
var piece;
var temp="";

for(var n=0;n<lim;n++)
	{	piece=eval("document.getElementById('"+prefix+n+"').style");
	x=getx(piece);
	y=piece.top.substring(0,piece.top.length-2);
	y=(y-130)/54;
	var win=false;
	for(var i=-1;i<2;i++)for(var j=-1;j<2;j++)
		{
		temp=getpattern(prefix,x,y,i,j);
		if(temp=="1111")
			{
			gameover=true;
			gamestatus.style.left=300;
			gamestatus.style.fontSize="30pt";
		if(whoseturn=="b")
			{
			gamestatus.style.color="red";
			gamestatus.innerHTML="Red wins!";}
		else{gamestatus.style.color="#5555ff";
			gamestatus.innerHTML="Blue wins!";}
			return;}}}
if(gameover==true)
	{	gamestatus.style.left=385;
	gamestatus.style.color="yellow";
	gamestatus.style.fontSize="30pt";
	gamestatus.innerHTML="Tie";}}

function getpattern(player,x,y,xmove,ymove)
{
//0=off edge of board
//1=player color
//2=opponent color
//3=empty
if((xmove==0)&&(ymove==0))return("0000");
var piece;
var opponent=(player=="r")?"b":"r";
var results="";
for(var n=0;n<4;n++)
	{
	if((x<0)||(x>6)||(y<0)||(y>5))return"0000";
	else
		{
		piece=board[x][y];
		if(piece==player)results+="1";
		else if(piece==opponent)results+="2";
		else if(piece=="e")results+="3";
		else results+="?";}
	x+=xmove;
	y+=ymove;	}
return results;}
function doinit()
{if(!document.all)
	{	gamestatus=document.getElementById('gamestatus');
	window.onclick=checkposition;}
for (var n=0;n<numpieces;n++)
	{	eval("document.getElementById('r"+n+"').style.left=100;");
	eval("document.getElementById('r"+n+"').style.top="+(n*15+130)+";");
	eval("document.getElementById('b"+n+"').style.left=675;");
	eval("document.getElementById('b"+n+"').style.top="+(n*15+130)+";");}
gamestatus.innerHTML="Red's turn";
//domove(computermove());
if(dodebug)debugbox=window.open("", "info","resizable=0,toolbar=0,menubar=0,scrollbars=1,width=600,height=400");
if(dodebug)debugbox.document.write("<html><head><title>Debug window</title><body>");
}

function computermove()
{
var blockthrees;
var nolift;
var smallnolift;
var opentwo;
var fork;
var smallfork;
var line;
var bigline;
var ctlcenter;
var futureopentwo;

var bigx=0;
var bigweight=0;
var curweight;
var flat;
var skew;
var lift;
var liftskew;

var n;
var centerdist=new Array();
var tempdist;

for(n=0;n<7;n++)if(hpos[n]>0)
	{
	tempdist=0;
	if(hpos[n]>2)tempdist+=hpos[n]-3;
	else tempdist+=3-hpos[n];
	if(n>3)tempdist+=n-3;
	else tempdist+=3-n;
	centerdist[n]=6-tempdist;
	debug("Center distance for "+n+" is "+centerdist[n]);
	}

for(n=0;n<7;n++)if(hpos[n]>0)
	{
	blockthrees=false
	nolift=true;
	smallnolift=true;
	opentwo=false;
	futureopentwo=false;
	fork=0;
	smallfork=0;
	line=false;
	for(var i=-1;i<2;i++)for(var j=-1;j<2;j++)
		{if((i!=0)||(j!=0)){
		flat=getpattern(compprefix,n,hpos[n]-1,i,j);
		if(flat=="3111")
			{
			debug("winning move");
			return(n);
			}
		lift=getpattern(compprefix,n,hpos[n]-2,i,j);
		skew=getpattern(compprefix,n-i,hpos[n]-1-j,i,j);
		liftskew=getpattern(compprefix,n-i,hpos[n]-2-j,i,j);
		//block threes
		if(flat=="3222")
			{
			debug("block threes "+n);
			blockthrees=true;
			}
		if(skew=="2322")
			{
			debug("block wedge "+n);
			blockthrees=true;
			}
		if(liftskew=="2322")
			{
			debug("lift into wedge "+n);
			nolift=false;
			}
		if(lift=="3222")
			{
			debug("lift into three "+n);
			nolift=false;
			}
		if(flat=="3223")
			{
			if(isnext(n,3,i,j))opentwo=true;
			else futureopentwo=true;
			}
		if((flat=="3133")||(flat=="3113"))
			{
			debug("build line "+n);
			line=true;
			}
		if(flat=="3113")
			{
			bigline=true;
			debug("big line "+n);
			}
		if((skew=="2323")&&(isnext(n,2,i,j)))
			{
			opentwo=true;
			debug("block split open two "+n);
			}
		if(flat=="3322")
			{
			fork+=1;
			smallfork+=1;
			debug("potential fork "+n);
			}
		if(skew=="3322")
			{
			fork+=1;
			smallfork+=1;
			debug("potential fork "+n);
			}
		if((liftskew=="3322")&&(i!=0))
			{
			smallnolift=false;
			}
		if(skew=="2333")
			{
			smallfork+=1;
			if(smallfork>1)debug("small fork "+n);
			}

		}}
	curweight=0;
	if(blockthrees)curweight+=46;
	if(nolift)curweight+=23;
	if(smallnolift)curweight+=2;
	if(opentwo)curweight+=7;
	if(fork>1)curweight+=7;
	else if(smallfork>1)curweight+=2;
	if(futureopentwo)curweight+=2;
	curweight+=centerdist[n];
	if(line)curweight+=1;
	if(bigline)curweight+=2;
	if(curweight>bigweight)
		{
		bigweight=curweight;
		bigx=n;
		}
	}
debug("Computer chooses "+bigx);
return(bigx);
}

function isnext(n,offset,i,j)
{
if(hpos[n+offset*i]==(hpos[n]+offset*j))return true;
return false;
}

function debug(stuff)
{
if(dodebug)debugbox.document.write(stuff+"<br>");}
