var player=new Array(10);
var computer=new Array(10);
var boat=new Array(10);//no.of boats
var comCells=new Array(225);
var play=0;	//play==0---player turn play==1---computer turn
var lastCel=225;
var next;//next cel
var dir=0;
var position=-1;
var currentcel;
var disFirst;
var disFirstCel;
var cel;
var isContinue;
var pathSetter=false;
var isFinesh=false;
var moves=0;
var timee=0;
var finished=0;
var firstmove=0;
var level=1;
function generate(){
	for(var i=0;i<10;i++){
		player[i]=new Array();
		computer[i]=new Array();
		boat[i]=new Array(2);
	}
	boat[0][0]=5;//boat length
	boat[1][0]=4;
	boat[2][0]=4;
	boat[3][0]=3;
	boat[4][0]=3;
	boat[5][0]=3;
	boat[6][0]=2;
	boat[7][0]=2;
	boat[8][0]=2;
	boat[9][0]=2;
	for(var i=0;i<boat.length;i++){
		boat[i][1]=(Math.random()*parseInt(1)).toFixed(0);//boat direction
	}
	place(boat,1);//for player
	place(boat,0);//for computer
	computerInit();
}
shuffle = function(sh){
	for(var j, x, i = sh.length; i; j = parseInt(Math.random() * i), x = sh[--i], sh[i] = sh[j], sh[j] = x);
	return sh;
};
function computerInit(){
    isContinue=false;
	for(var i=0;i<225;i++){
		comCells[i]=new Array(2);
		comCells[i][0]=i;
		comCells[i][1]=0;
	}
	comCells=shuffle(comCells);
}
function place(boat,user){
	for(var i=0;i<boat.length;i++){
		var length=parseInt(boat[i][0]);
		var limiter=15-length;
		var img;
		if(boat[i][1]==0){//left to right
			var isFill=false;
			do{
				var xStart=(Math.random()*parseInt(limiter)).toFixed(0);
				var yStart=(Math.random()*parseInt(14)).toFixed(0);
				xStart=parseInt(xStart);
				yStart=parseInt(yStart);
				var empty=true;
				for(var j=0;j<length;j++){
					if(!isEmpty(parseInt(xStart+j),parseInt(yStart),user)){
						empty=false;
						break;
					}
					if(yStart<=13){
						if(!isEmpty(parseInt(xStart+j),parseInt(yStart+1),user)){
							empty=false;	//alert("test");
							break;
						}
					}
					if(yStart>0){
						if(!isEmpty(parseInt(xStart+j),parseInt(yStart-1),user)){
							empty=false;	//alert("test");
							break;
						}
					}
					if(xStart+j<=13){
						if(!isEmpty(parseInt(xStart+j+1),parseInt(yStart),user)){
							empty=false;	//alert("test");
							break;
						}
					}
					if(xStart+j>0){
						if(!isEmpty(parseInt(xStart+j-1),parseInt(yStart),user)){
							empty=false;	//alert("test");
							break;
						}
					}
				}
				if(empty==true){
					for(var j=0;j<length;j++){
						if(j==0){
							img="boat3.gif";
						}
						else if(j==(length-1)){
							img="boat1.gif";
						}
						else{
							img="boat2.gif";
						}	
						//fillCell(parseInt(xStart)+j,parseInt(yStart),user,img);
						if(user==0){
							computer[i][j]=fillCell(parseInt(xStart)+j,parseInt(yStart),user,img);
						}
						else if(user==1){
							player[i][j]=fillCell(parseInt(xStart)+j,parseInt(yStart),user,img);
						}
					}
					isFill=true;
				}
			}while(isFill==false);
		}
		else{//top to bottom
			var isFill=false;
			do{
				xStart=(Math.random()*parseInt(14)).toFixed(0);
				yStart=(Math.random()*parseInt(limiter)).toFixed(0);
				xStart=parseInt(xStart);
				yStart=parseInt(yStart);
				var empty=true;
				for(var j=0;j<length;j++){
					if(!isEmpty(xStart,yStart+j,user)){
						empty=false;
					}
					if(xStart<=13){
						if(!isEmpty(parseInt(xStart+1),parseInt(yStart+j),user)){
							empty=false;	//alert("test");
							break;
						}
					}
					if(xStart>0){
						if(!isEmpty(parseInt(xStart-1),parseInt(yStart+j),user)){
							empty=false;	//alert("test");
							break;
						}
					}
					if(yStart+j<=13){
						if(!isEmpty(parseInt(xStart),parseInt(yStart+j+1),user)){
							empty=false;	//alert("test");
							break;
						}
					}
					if(yStart+j>0){
						if(!isEmpty(parseInt(xStart),parseInt(yStart+j-1),user)){
							empty=false;	//alert("test");
							break;
						}
					}
				}
				if(empty==true){
					for(var j=0;j<length;j++){
						if(j==0){
							img="boat4.gif";
						}
						else if(j==(length-1)){
							img="boat6.gif";
						}
						else{
							img="boat5.gif";
						}	
						if(user==0){
							computer[i][j]=fillCell(parseInt(xStart),parseInt(yStart)+j,user,img);
						}
						else{
							player[i][j]=fillCell(parseInt(xStart),parseInt(yStart)+j,user,img);
						}
						//fillCell(xStart,yStart+j,user,img);
					}
					isFill=true;
				}
			}while(isFill==false);
		}
	}
}
function isEmpty(x,y,user){
	if(user==0){					//computer
		var b=y*15+x;
		b='p'+b;
		var path=document[b].src;
		var file=path.substring(path.lastIndexOf("/")+1,path.length);
		if(file=="empty.gif"){
			return true;
		}
		else{					//player
			return false;
		}
	}
	else{
		var b=y*15+x;
		b='c'+b;
		var path=document[b].src;
		var file=path.substring(path.lastIndexOf("/")+1,path.length);
		if(file=="empty.gif"){
			return true;
		}
		else{					//player
			return false;
		}
	}
}
function fillCell(x,y,user,img){
	var b=parseInt(y)*15+parseInt(x);
	if(user==0){					//computer
		b='p'+b;
		document[b].src="empty.gif";
        }
	else if(user==1){						//player
		b='c'+b;
		document[b].src=img;
	}
	else if(user==2){
		b='p'+b;
		document[b].src=img;
	}
	return b;
}
function click(cel){
         if(finished==0)
         {
           timer();  
           if(isFinesh==false){
		if(play==0)
			player_play(cel);
		else if(play==1)
			computer_play();
		else
			return true;
	}
	else{
	
	}
     }
}
function player_play(cel){
	var status=false;
	var path=document[cel].src;
	var file=path.substring(path.lastIndexOf("/")+1,path.length);
	for(var i=0;i<computer.length;i++)
	{
		for(var j=0;j<computer[i].length;j++){
			if(file=="del.gif"){
				status=true;
				break;
			}
			else if(computer[i][j]==cel){
				document[cel].src="del.gif";
				status=true;
				if(isDestroyed(i,0)==true){
					alert("You destroyed my ship");
					if(isComplete(0)==true){
                                                finished=1;
						alert("Game completed");
                                                clearTimeout(timera);
						var scr=score();
					//	alert("Your Score="+scr);
                                                moves=scr;                                               
                                                testAjaxa(moves,timee,'reverse'); 
						isFinesh=true;
						return true;
						play=2;
					}
				}
				play=1;
				computer_play();
				return true;
			}
		}
	}
	if(status==false&&file!="unknown.gif"){
		document[cel].src="unknown.gif";
		play=1;
		computer_play();
		return true;
	}
}
function isDestroyed(i,play){
	if(play==0){
		for(var j=0;j<computer[i].length;j++){
			var path=document[computer[i][j]].src;
			var file=path.substring(path.lastIndexOf("/")+1,path.length);
			if(file!="del.gif"){
				return false;
			}
		}
		return true;
	}
	else{
		for(var j=0;j<player[i].length;j++){
			var path=document[player[i][j]].src;
			var file=path.substring(path.lastIndexOf("/")+1,path.length);
			if(file!="del.gif"){
				return false;
			}
		}
		return true;
	}
}
function isComplete(play){
	if(play==0){
		for(var i=0;i<computer.length;i++){
			for(var j=0;j<computer[i].length;j++){
				var path=document[computer[i][j]].src;
				var file=path.substring(path.lastIndexOf("/")+1,path.length);
				if(file!="del.gif"){
					return false;
				}
			}
		}
		return true;
	}
	else{
		for(var i=0;i<player.length;i++){
			for(var j=0;j<player[i].length;j++){
				var path=document[player[i][j]].src;
				var file=path.substring(path.lastIndexOf("/")+1,path.length);
				if(file!="del.gif"){
					return false;
				}
			}
		}
		return true;
	}
}
function computer_play(){
	var status=false;
	var isMyturn=true;
	var isOk=false;
	if(isContinue==false){//Continue to destroyed
		do{
                        position++;
			cel=comCells[position][0];
			if(comCells[position][1]==0){
				isOk=true;
				comCells[position][1]=1;
			}
			else{
				isOk=false;
			}
		}while(isOk==false);
		previous=comCells[position][0];
                comCells[position][1]=1;
	}
	else{	cel=next;//alert("Next="+next);
		comCells[next][1]=1;
	}
	currentcel=cel;
	cel='c'+cel;
	var path=document[cel].src;
	var file=path.substring(path.lastIndexOf("/")+1,path.length);
	for(var i=0;i<computer.length;i++){
		for(var j=0;j<player[i].length;j++){
			if(file=="del.gif"){
				status=true;
				isMyturn=true;
				if(isContinue==true){
					isContinue=false;
                                        position=disFirst;
				}
				break;
			}
			else if(player[i][j]==cel){		//If boat destroyed
				//alert("Current Dir="+dir);
                                status=true;
				document[cel].src="del.gif";
				if(isContinue==false){
					lastCel=comCells[position][0];
					dir=findDir(previous);
					disFirst=position;
					disFirstCel=currentcel;
					next=findNextCel(dir,currentcel);//alert(dir+"next cell"+next);
					isContinue=true;
				}
				if(isDestroyed(i,1)==true){	//if Completely destroyed boat
					alert("I destroyed your ship");
					isNext=false;
                                        status=true;
                                        isContinue=false;
                                        position=disFirst;
                                       	if(isComplete(1)==true){//All the Boat destroyed
						alert("Computer won the Game");
                                                document.location.reload();
						isFinesh=true;
						play=2;
					}
				}
                                else if(isContinue==true){
					next=findNextCel(dir,currentcel);//alert(dir+"next cell"+next);
					//next=findNextCel(dir,next);
				}
				break;
			}
		}	
		if(status==true)
			break;
	}
	if(status==false&&file!="unknown.gif"){
		document[cel].src="unknown.gif";
		if(isContinue==true){
			dir=findDir(disFirstCel);//alert("New Dir "+dir);
			next=findNextCel(dir,disFirstCel);
			//alert("Find next after fail "+next+" using "+disFirstCel);
		}
		play=0;
		isMyTurn=false;
	}
        play=0;
	return true;
}


function isEmptyCel(position){
		var temp='c'+position;
		var path=document[temp].src;
		var file=path.substring(path.lastIndexOf("/")+1,path.length);
		if(file=="del.gif"||file=="unknown.gif"){
			return false;
		}
		else{
			return true;
		}
}
function continueDir(temp,dir){
    var cel=temp;
    if(dir==0){
        cel=cel-1;
    }
    else if(dir==1){
        cel=cel+1;
    }
    else if(dir==2){
        cel=cel-15;
    }
    else if(dir==3){
        cel=cel+15;
    }
}
function findDir(ccel){
		var isSet=false;
		var direction;
                var temp=ccel;
		if(ccel%15!=0){temp=temp-1;
			isSet=isEmptyCel(temp);
			if(isSet==true){
				direction=0;//left
			}
		}
		if((ccel+1)%15!=0&&isSet==false){
                        temp=ccel;
                        temp=temp+1;
			isSet=isEmptyCel(temp);
			if(isSet==true){
				direction=1;//right
			}
		}
		if(ccel>14&&isSet==false){
			temp=ccel;
                        temp=temp-15;
			isSet=isEmptyCel(temp);
			if(isSet==true){
				direction=2;//up
			}
		}
		if(ccel<210&&isSet==false){
			temp=ccel;
                        temp=temp+15;
			isSet=isEmptyCel(temp);
			if(isSet==true){
				direction=3;//down
			}
		}
                if(isSet==false){
                    //alert("Error in finding Dir for the cell "+ccel+"with result dir "+dir);
                }//alert("dir="+dir+" for cell is "+ccel);
		dir=direction;
		return(dir);
}
function findNextCel(direction,position){
        var nextCel;
	if(direction==0){
		nextCel=position-1;//alert("Left dir position "+position);
		if(isEmptyCel(nextCel)==true){
			return nextCel;
		}
		else{
			//alert("next cell error");
			//;
			return(findNextCel(findDir(disFirst),disFirst));
		}
	}
	else if(direction==1){
		nextCel=position+1;//alert("Right dir position "+position);
		if(isEmptyCel(nextCel)==true){
			return nextCel;
		}
		else{
			//alert("next cell error");
			//findDir(disFirst);
			return(findNextCel(findDir(disFirst),disFirst));
		}
	}
	else if(direction==2){
		nextCel=position-15;//alert("Top dir position "+position);
		if(isEmptyCel(nextCel)==true){
			return nextCel;
		}
		else{
			//alert("next cell error");
			//findDir(disFirst);
			return(findNextCel(findDir(disFirst),disFirst));
		}
	}
	else if(direction==3){
		//alert("Down dir position "+position);
		nextCel=position+15;
		if(isEmptyCel(nextCel)==true){
			return nextCel;
		}
		else{
			//findDir(disFirst);
			return(findNextCel(findDir(disFirst),disFirst));
			//alert("next cell error");
		}
	}
	else{
		return(findNextCel(findDir(disFirst),disFirst));
		//findDir(disFirst);
		//findNextCel(dir,disFirst);
		//alert(direction+" Alert at find next "+nextCel+" c position="+position);
		//return nextCel;
	}
}
function score(){
	var temp;
	var scr=0;
	var path;
	var file;
	for(var i=0;i<225;i++){
		temp='p'+i;
		path=document[temp].src;
		file=path.substring(path.lastIndexOf("/")+1,path.length);
		if(file!="del.gif"&&file!="unknown.gif")
		{ scr++;}
	}
	return scr;
}
function start_play()
{
   if(finished==0)
  {
        if(firstmove==0)
        {
           timer();  
           
           firstmove++;
        }
  }  
}

