<!--

var self;

function MiniBattleships()
{
    self = this;
    this.usrArr = new Array(4); this.cmpArr = new Array(4);
    this.shipsArr = new Array();this.randPos = new Array(4);
    this.HZNTL = 10;this.VRTCL=20;this.gameOver = false;
    this.usrScr = 0; this.cmpScr = 0;
    this.cmpGrid = new Array();this.cmpSrch = new Array();this.compSavedCell = new Array();
    this.tmID = null;
    	
    this.spliceArray = function(arr)
    {
        if (arr.length > 0) {arr.splice(0,arr.length);}
        if (arr.length > 0) {while(arr.length > 0) {arr.pop();}}
    }
	
	this.initGame = function(numships)
	{
	    this.spliceArray(this.cmpGrid);
	    this.spliceArray(this.shipsArr);
	    this.spliceArray(this.cmpSrch);
	    this.spliceArray(this.compSavedCell);
	    this.gameOver = false;this.usrScr = 0; this.cmpScr = 0;
	    
	    for (var i = 0; i < 4; i++)
	    {
	        this.usrArr[i] = new Array(8);
	        this.cmpArr[i] = new Array(8);
	        for (var j = 0; j < 8; j++)
	        {
		        this.usrArr[i][j] = 0;
		        this.cmpArr[i][j] = 0;
		        this.cmpGrid.push(i*8+j);
	        }
	    }
	    
	    for (var i = 0; i < this.randPos.length; i++) {this.randPos[i] = i%4;}
	    var i = this.getRandom(2,5); for (; i > 0; i--) {this.shipsArr.push(2);}
	    this.shipsArr[0] = 3;

	    this.setShips(this.usrArr, this.shipsArr);
	    this.setShips(this.cmpArr, this.shipsArr);
	    this.showBattleBoard(this.usrArr, "usrbrd", 0, true, false);
	    this.showBattleBoard(this.cmpArr, "cmpbrd", 32, false, true);
	    this.showScoreBoard();
	}
	
	
	this.showScoreBoard = function()
	{
	    var str = '<div class="usrscr">&nbsp;You:' + this.usrScr + ' of ' + this.shipsArr.length + '</div>';
	    str += '<div class="cmpscr">Enemy:' + this.cmpScr + ' of ' + this.shipsArr.length + '&nbsp;</div>';
	    
	    var o = document.getElementById("scrbrd");
	    if (o != null) {o.innerHTML = str;}
	    if ((this.usrScr == this.shipsArr.length) || (this.cmpScr == this.shipsArr.length)) 
	    {
	    	this.gameOver = true;
	    	if (this.usrScr < this.shipsArr.length) {this.showUnfoundShips(this.usrArr);}
	    	if (this.tmID != null) {clearInterval(this.tmID);}
			var str = '<div class="btntxt" style="margin-left:50px;">Game over</div><div id="lbtn"></div><div id="mbtn" onClick="self.submitfrm();">OK</div><div id="rbtn"></div>';
			var f = document.getElementById("btnbrd"); if (f != null) {f.innerHTML = str;}        
	    }
	}
	
	this.showUnfoundShips = function(arr)
	{
		for (var i = 0; i < 4; i++)
		{
		    for (var j = 0; j < 8; j++)
		    {
				if (arr[i][j] > 100 && arr[i][j] < 1000)
				{
					var seg = arr[i][j]%10;
					var horv = Math.floor((arr[i][j]%100)/10)*10;
					var clsstr = "ship_" + (horv == this.HZNTL ? "h_" : "v_") + seg;
					var o = document.getElementById("sq_" + (i*8+j)); if (o != null) {o.className = clsstr;}
				}
		    }
		}
		
	}
	
	this.submitfrm = function()
	{
		var f = document.getElementById("minibattleshipsbrd");
		if (f != null) 
		{
			var str = "Try again."; 
			if (this.usrScr > this.cmpScr) {str = "You won the battle.";}
			else if (this.usrScr < this.cmpScr) {str = "You lost the battle.";}
			f.innerHTML = "";
			var o = f.appendChild(document.createElement('div'));
			o.innerHTML='<input type="hidden" name="helppage" value="minibattleshipshelp.html" />';
			var o = f.appendChild(document.createElement('div'));
			o.innerHTML='<input type="hidden" name="gamepage" value="minibattleships.html" />';
			o = f.appendChild(document.createElement('div'));
			o.innerHTML='<input type="hidden" name="msg" value="' + str + '" />';
		}

		document.minibattleships_frm.submit();
	}
	
	this.showBattleBoard = function(arr, brdid, sidx, ocFlag, shpFlag)
	{
	    var str = '';
	    for (var i = 0; i < 4; i++)
	    {
	        for (var j = 0; j < 8; j++)
	        {
	            if (arr[i][j] == 0) 
	            {
	                if (ocFlag)
	                {
	                    str += '<div id = "sq_' + (i*8+j+sidx) + '" class="sqr_e" onClick="self.sqrTouch(' + i + ',' + j + ');"></div>';
	                }
	                else
	                {
	                    str += '<div id = "sq_' + (i*8+j+sidx) + '" class="sqr_e"></div>';
	                }
	            }
	            else
	            {
	                var clsstr = "sqr_e";
	                var clkstr = "";var divstr = '';
	                if (arr[i][j] > 100 && arr[i][j] < 1000)
	                {
	                    if (shpFlag)
	                    {
							var seg = arr[i][j]%10;
							var horv = Math.floor((arr[i][j]%100)/10)*10;
							clsstr = "ship_" + (horv == this.HZNTL ? "h_" : "v_") + seg;
	                    }
	                    if (ocFlag)
	                    {
	                        clkstr = ' onClick="self.sqrTouch(' + i + ',' + j + ');" ';
	                    }
	                }
	                else if (arr[i][j] == 1000)
	                {
	                    clsstr = "sqr_of";
	                }
	                else if (arr[i][j] > 1000)
	                {
	                    clsstr = "sqr_on";
	                }
	                else if (arr[i][j] > 10 && arr[i][j] < 30)
	                {
						var seg = arr[i][j]%10;
						var horv = Math.floor((arr[i][j]%100)/10)*10;
						clsstr = "ship_" + (horv == this.HZNTL ? "h_" : "v_") + seg;
						divstr = '<div class="fire_' + this.getRandom(1,3) + '"></div>';
	                }
	                
	                str += '<div id = "sq_' + (i*8+j+sidx) + '" class="' + clsstr + '"' + clkstr + '>' + divstr + '</div>';
	            }	            
	        }
	    }
	    
	    var o = document.getElementById(brdid); if (o != null) {o.innerHTML = str;}
	}
	
	this.addShip = function(arr, r, c, segs, ri, ci, idx, horz)
	{
		arr[r][c] = idx*100 + horz + 1;
		var k = 1;for (; k < segs-1; k++) {arr[r+ri*k][c+ci*k] = idx*100 + horz + 2;}
		arr[r+ri*k][c+ci*k] = idx*100 + horz + 3;
	}
	
	this.canPlaceShip = function(arr, segs, r, c, ri, ci)
	{
	    var cando = true;
		for (var k = 1; cando && k < segs; k++) 
		{
		    var nr = r+k*ri;
		    var nc = c+k*ci;
		    if (nr < 0 || nr > 3 || nc < 0 || nc > 7) {cando = false;}
		    else if (arr[nr][nc] != 0) {cando = false;}
		}
		return cando;
	}

	this.isShipPlaced = function(arr, segs, r, c, dir, idx)
	{
	    var shipPlaced = false;
	    if (dir == 0)
	    {
	        if (this.canPlaceShip(arr, segs, r, c, 0, -1))
	        {
	            this.addShip(arr, r,c+1-segs,segs,0,1,idx,this.HZNTL);
	            shipPlaced = true;
	        }
	    }
	    else if (dir == 1)
	    {
	        if (this.canPlaceShip(arr, segs, r, c, 0, 1))
	        {
	            this.addShip(arr, r,c,segs,0,1,idx,this.HZNTL);
	            shipPlaced = true;
	        }
	    }
	    else if (dir == 2)
	    {
	        if (this.canPlaceShip(arr, segs, r, c, -1, 0))
	        {
	            this.addShip(arr, r+1-segs,c,segs,1,0,idx,this.VRTCL);
	            shipPlaced = true;
	        }
	    }
	    else if (dir == 3)
	    {
	        if (this.canPlaceShip(arr, segs, r, c, 1, 0))
	        {
	            this.addShip(arr, r,c,segs,1,0,idx,this.VRTCL);
	            shipPlaced = true;
	        }
	    }	    
	    return shipPlaced;
	}
	
	this.placeShip = function(arr, posArr, shipType, idx)
	{
	    var done = false;
	    while (done == false)
	    {
	        var r = this.getRandom(0,3);var c = this.getRandom(0,7);
	        while (arr[r][c] != 0) {r = this.getRandom(0,3); c = this.getRandom(0,7);}
	        var i = 0;
	        while (done == false && i < posArr.length) {done = this.isShipPlaced(arr, shipType, r, c, posArr[i], idx);i++;}
	    }
	}
	
	this.setShips = function(arr, shpArr)
	{
	    for (var idx = 0; idx < shpArr.length; idx++)
	    {
		    this.shuffleArray(this.randPos);
	        this.placeShip(arr,this.randPos, shpArr[idx], idx+1);
	    }
	}
	
	this.shuffleArray = function(a)
	{
		for (var m = 0; m < a.length; m++)
		{
			t1 = this.getRandom(0,a.length-1);t2 = this.getRandom(0,a.length-1);
			if (t1 != t2) {tt = a[t1];a[t1]=a[t2];a[t2]=tt;}
		}
	}
	
	this.sqrTouch = function(r,c)
	{
	    if (this.gameOver) {return;}
	    var curval = this.usrArr[r][c]; curval = curval % 1000;
	    this.usrArr[r][c] = 1000 + curval;

	    var id = r*8 + c;var clsstr = "sqr_of"; if (curval > 0) {clsstr = "sqr_on";}
	    var o = document.getElementById("sq_" + id);
	    if (o != null) {o.className = clsstr;o.onclick = null;}
	    if (curval > 0)
	    {
	        var rcArr =  this.foundAShip(r,c,this.usrArr);
	        if (rcArr.length > 0) {this.usrScr++;this.showWreckedShip(rcArr,this.usrArr, 0);this.showScoreBoard();}
	    }
	    this.tryComputerMove();
	}
	
	this.addAdjCell = function(r, c, arr)
	{
	    if (r < 0 || r > 3 || c < 0 || c > 7) {return;}
	    if (this.cmpArr[r][c] == 0 || (this.cmpArr[r][c] < 1000 && this.cmpArr[r][c] > 100)) {arr.push([r,c]);}
	}
	
	this.sortcoor = function(a, b)
	{
	    if (a[0] == b[0]) {return (a[1]-b[1]);}
	    else {return (a[0]-b[0]);}
	}

	this.getSmartMove = function()
	{
	    var rid = 0;
	    if (this.cmpSrch.length == 0) 
	    {
	        if (this.compSavedCell.length > 0)
	        {
	            var rc = this.compSavedCell.pop();
	            this.cmpSrch.push(rc);
	        }
	        else
	        {
	    	    rid = this.getRandom(0, this.cmpGrid.length-1);
	        }
	    }
	    
	    if (this.cmpSrch.length > 0)
	    {
			var tmpArr = new Array();
	        if (this.cmpSrch.length == 1)
	        {
				var rc = this.cmpSrch[0];
				this.addAdjCell(rc[0], rc[1]-1, tmpArr);
				this.addAdjCell(rc[0], rc[1]+1, tmpArr);
				this.addAdjCell(rc[0]-1, rc[1], tmpArr);
				this.addAdjCell(rc[0]+1, rc[1], tmpArr);
				var x = this.getRandom(0, tmpArr.length-1);
				rid = tmpArr[x][0]*8 + tmpArr[x][1];
	        }
			else
			{
			    this.cmpSrch.sort(self.sortcoor);
			    var rc1 = this.cmpSrch[0]; var rc2 = this.cmpSrch[this.cmpSrch.length-1];
			    if (rc1[0] == rc2[0]) {this.addAdjCell(rc1[0], rc1[1]-1, tmpArr);this.addAdjCell(rc2[0], rc2[1]+1, tmpArr);}
			    else {this.addAdjCell(rc1[0]-1, rc1[1], tmpArr);this.addAdjCell(rc2[0]+1, rc2[1], tmpArr);}
			    if (tmpArr.length == 0) {this.compSavedCell.push(this.cmpSrch.pop());return this.getSmartMove();}
			    else
			    {
					var x = this.getRandom(0, tmpArr.length-1);
					rid = tmpArr[x][0]*8 + tmpArr[x][1];
			    }
			}
			
			for (var i = 0; i < this.cmpGrid.length; i++)
			{
			    if (rid == this.cmpGrid[i]) {rid=i;break;}
			}
	    }
	    
		return rid;
	}
	
	this.tryComputerMove = function()
	{
	    var rndid = this.getSmartMove();
	    var idx = this.cmpGrid[rndid];
	    // remove this id from cmp grid
	    var t1 = this.cmpGrid[0];this.cmpGrid[0] = this.cmpGrid[rndid];this.cmpGrid[rndid] = t1;
	    this.cmpGrid.shift();
	    var c = idx%8;var r = Math.floor(idx/8);
	    var curval = this.cmpArr[r][c]; curval = curval % 1000;
	    this.cmpArr[r][c] = 1000 + curval;
		var id = 32+r*8 + c;var clsstr = "sqr_of"; if (curval > 0) {clsstr = "sqr_on";}
		var o = document.getElementById("sq_" + id);
		if (o != null) {o.className = clsstr;o.onclick = null;}
	    
	    if (curval > 0)
	    {
	        this.cmpSrch.push([r,c]);
	        var rcArr =  this.foundAShip(r,c,this.cmpArr);
	        if (rcArr.length > 0) {this.removeSearchedShip(rcArr, this.cmpSrch);this.cmpScr++;this.showWreckedShip(rcArr,this.cmpArr, 32);this.showScoreBoard();}
	    }
	}
	
	this.removeSearchedShip = function(rcarr, srcharr)
	{
	    var arr1 = new Array();
	    while (srcharr.length > 0)
	    {
	        var rc1 = srcharr.pop();
	        var found = false;
	        for (var k = 0; (found == false) && (k < rcarr.length); k++)
	        {
	            var rc2 = rcarr[k];
	            if ((rc1[0] == rc2[0]) && (rc1[1] == rc2[1])) {found=true;}
	        }
	        if (found == false) {arr1.push(rc1);}
	    }
	    
		this.spliceArray(srcharr);
	    if (arr1.length > 0)
	    {
			for (var k = 0; k < arr1.length; k++) {srcharr.push(arr1[k]);}
	    }
	}

	this.foundAShip = function(r, c, arr)
	{
	    var sval = arr[r][c] % 1000; var horv = sval % 100; horv = Math.floor(horv/10)*10;
	    sval = Math.floor(sval/100)*100;
	    var ri = 0; var ci = 0; if (horv == this.HZNTL) {ci = -1;} else {ri = -1;}
	    var k = 0;
	    while (arr[r+ri*k][c+ci*k]%10 != 1)
	    {
	        k++;
	    }
	    sval = 1000 + sval + horv;
	    var tmpArr = new Array();
	    if (arr[r+ri*k][c+ci*k] == sval+1)
	    {
	        tmpArr.push([r+ri*k, c+ci*k]);
	        if (horv == this.HZNTL) {ci = 1;} else {ri = 1;}
	        k = k * -1;k++;
	        while (arr[r+ri*k][c+ci*k] == sval + 2) {tmpArr.push([r+ri*k, c+ci*k]);k++;}
	        if (arr[r+ri*k][c+ci*k] == sval + 3) {tmpArr.push([r+ri*k, c+ci*k]);} else {this.spliceArray(tmpArr);}
	    }
	    return tmpArr;
	}
	
	this.showWreckedShip = function(rcArr, arr, sidx)
	{
	    while (rcArr.length > 0)
	    {
	        var rc = rcArr.shift();
			var seg = arr[rc[0]][rc[1]]%100;
			var horv = Math.floor(seg/10)*10;seg = seg % 10;
			arr[rc[0]][rc[1]] = horv + seg;
			var idx = sidx + rc[0]*8 + rc[1];
			var o = document.getElementById("sq_" + idx);
			if (o != null) 
			{
				var clsstr = "ship_" + (horv == this.HZNTL ? "h_" : "v_") + seg;
				o.className = clsstr;
				var firestr = '<div class="fire_' + this.getRandom(1,3) + '"></div>';
				o.innerHTML = firestr;
			}
	    }
	}
		

	this.getRandom = function(min,max)
	{
		return min + Math.floor(Math.random()*(max-min+1))
	}
}


-->


