﻿

// JScript File


var currentPlayer= 1; 
var soundsOn= true;
var musicOn= false;
var playerScore= new Array(3); 
    playerScore[1]= 2; 
    playerScore[2]= 2;

var buttonImage = new Array();
    buttonImage[0]=new Image();
    buttonImage[1]=new Image();
    buttonImage[2]= new Image();

buttonImage[0].src= "images/bg.gif";
buttonImage[1].src= "images/pink.gif";
buttonImage[2].src= "images/green.gif";

var playerNameOne = null;
var playerNameTwo = null;


/* This function warn players when they press the navigator close button
    Result: the function returns warning message
*/
function GameExit()
{
        return "The game will over and all scores will be lost.";
        
}
/* This function resets the game (the function returns the start values of the game)
    Result: n/a
*/

function ResetGame() 
{   
    currentPlayer= 1; 
    playerScore[1]= 2; 
    playerScore[2]= 2;
    ConfigureSounds();
    ShowScore();
   
	for (var y=0; y<8; y++) 
    {
		for (var x=0; x<8; x++) 
		{
			if (((x == 3) && (y == 3)) || ((x == 4) && (y == 4)))
		    {		
				document.images["x"+x+"y"+y].src = buttonImage[1].src;
			}
			else if (((x == 3) && (y == 4)) || ((x == 4) && (y == 3)))
			{
				document.images["x"+x+"y"+y].src = buttonImage[2].src;
			}
			else 
			{
				document.images["x"+x+"y"+y].src = buttonImage[0].src;
			}
		}
    }	
    var soundBox=document.getElementsByName("sounds"); 
    soundBox[0].checked= true;  
    ConfigureSounds();
     soundManager.stop('pinko');
    var musicBox=document.getElementsByName("music"); 
    musicBox[0].checked= false;  
	document.getElementById('pink').src = "images/pink.gif";
    document.getElementById('green').src = "images/bg.gif";
    playerNameOne = 0;
    playerNameTwo = 0;
    while(playerNameOne == null ||playerNameOne == "" || playerNameOne.length > 10) 
    {
        playerNameOne =  prompt("Provide name for Player 1 (up to 10 characters) :", "Player 1");
        
        playerNameOne = (playerNameOne != null) ? playerNameOne:"Player 1";
        
    }
    while(playerNameTwo == null ||playerNameTwo == "" || playerNameTwo.length > 10)
    {
        playerNameTwo =  prompt("Provide name for Player 2 (up to 10 characters) :", "Player 2");
        playerNameTwo  = (playerNameTwo != null) ? playerNameTwo : "Player 2";
    }
    document.getElementById("playerOne").innerHTML = playerNameOne;
    document.getElementById("playerTwo").innerHTML = playerNameTwo;
    if(soundsOn)
	        {
	            soundManager.play('gliss');
	        }
    alert("You can start the Game.");
    
}

/* This function checks if the pressed button is empty button first and then 
    if the given condition is true calls other function which make game happen.
    Parameters:
        int x - this parameter holds the board column.
        int y - this parameter holds board row.
        This are the coordinates of an image.    
*/

function ImageOnClick(x,y)
{
	if (document.images["x"+x+"y"+y].src == buttonImage[0].src) 
	{			
		if (CheckValidMove(x,y)) 
		{	
			ChangePulls(x,y);
			ShowScore();
			ChangeCurrentPlayer();
			PassPlayer();
		}
		else
		{
			if(soundsOn)
	        {
	            soundManager.play('bang');
	        }
	        
		}
		
    }
}
/* This function checks valid move for the current player and if the condition
    is true returns true if not (player pressed a button where pull can not be placed)
    call function which plays sound.
    Parameters:
        int x - this parameter holds the board column.
        int y - this parameter holds board row.
        This are the coordinates of an image. 
    Result?
*/
function CheckValidMove(x,y) 
{   
    for (var j=-1; j<2; j++) 
	    {
		    for (var i=-1; i<2; i++) 
		    {
                if (CountPulls(x,y,i,j))
          	    {
		            return true;
	            }
	        }
		}
	
}
/* This function counts the opposite player's pulls ...
    Parameters:
        int x - this parameter holds the board column.
        int y - this parameter holds board row.
        This are the coordinates of an image.
        int lx - this parameter holds board number of columns.
        int ly - this parameter holds board number of rows.
       
    Result: the function returns the number of opposite player's pulls  
*/
function CountPulls(x,y,lx,ly) 
{   
    var lx;
    var ly;
    var x;
    var y;
	var count = 0;
	x = x+lx;
	y = y+ly;
	while ((x >= 0) && (x < 8)&& (y >= 0) && (y < 8) 
	        && document.images["x"+x+"y"+y].src == buttonImage[3-currentPlayer].src) 
	{
		x = x+lx;
		y = y+ly; 
		count++;
	}
	if ((x<0) || (x>7) || (y<0) || (y>7) || document.images["x"+x+"y"+y].src == buttonImage[0].src)
	{
    	return(0);
    	
	}  
	else
	{
		return(count);
	}  
}
/* This function place the current player pulls and reverse the opposite player's pulls 
    Parameters:
        int x - this parameter holds the board column.
        int y - this parameter holds board row.
        This are the coordinates of an image.
        int reverse - this parameter holds the number of opposite player pulls arround the changed pull.
*/
function ChangePulls(x,y) 
{
	document.images["x"+x+"y"+y].src = buttonImage[currentPlayer].src; 
	playerScore[currentPlayer]++;
	
			if(soundsOn)
	        {
	            soundManager.play('click');
	        }
	
	var reverse;
	var x;
	var y;
	for (var j=-1; j<2; j++) 
    {
        for (var i=-1; i<2; i++) 
        {
	        reverse= CountPulls(x,y,i,j); 
	        while (reverse) 
	        {
	            playerScore[3-currentPlayer]--;
		        document.images["x"+(x+(i*reverse))+"y"+(y+(j*reverse))].src = buttonImage[currentPlayer].src;
		        playerScore[currentPlayer]++; 
		        reverse--;
	        }
        }
    }
}
// This function shows scores
	
function ShowScore()
{
    document.getElementById("score").player1.value = playerScore[1];
	document.getElementById("score").player2.value = playerScore[2];

}
// This function checks the scores to alert game over and the winner
   
function GameOver()
{
    if ((playerScore[1] == 0) || (playerScore[2] == 0) || (playerScore[1]+playerScore[2] == 64))
    {       
		if(soundsOn)
	    {
	       
	        soundManager.play('explosion');
	    }	
	    
	    if (playerScore[1] > playerScore[2])
		{
		   
			alert("Congratulations, "+playerNameOne+"!\n You won by "
			            +playerScore[1]+" pieces.\nIf you want to play again press \"New Game\" button.");
		
		   
		}
		else if (playerScore[2] > playerScore[1]) 
		{
		  
			alert("Congratulations, "+playerNameTwo+"!\n You won by "
			            +playerScore[2]+" pieces.\nIf you want to play again press \"New Game\" button.");
		
		  
		}
		else 
		{
			alert("Equal!\nIf you want to play again press \"New Game\" button.");
		}
	}
}	
// This function change the current player and move the player mark 
   	
function ChangeCurrentPlayer()
{   
	currentPlayer = 3- currentPlayer;
    if(currentPlayer == 1)
    {
    document.getElementById('pink').src = buttonImage[1].src;
    document.getElementById('green').src = buttonImage[0].src;
    }
    else
    {
    document.getElementById('pink').src = buttonImage[0].src;
    document.getElementById('green').src = buttonImage[2].src;
    }

}
/* This function checks if there is valid move for every button on the board
    and if there is not alert message and change the current player
    Parameters:
        int x - this parameter holds the board column.
        int y - this parameter holds board row.
*/
function PassPlayer()
{
var canContinue = 0;
			for(var x=0;x<=7;x++)
			{
			    for(var y=0;y<=7;y++)
			    {
			        if( (document.images["x"+x+"y"+y].src == buttonImage[0].src) && CheckValidMove(x,y))  
			        {
			            canContinue++;
			        }
			     }
			}			
			if( canContinue == 0 && playerScore[1]+playerScore[2] != 64)
			{
			    if(currentPlayer == 1)
			    {
			          alert(playerNameOne+",  you have to pass!\n"+ playerNameTwo+" it's your turn!");
			    }
			    else
			    {
			         alert(playerNameTwo+",  you have to pass!\n"+ playerNameOne+" it's your turn!");
			    }
			     ChangeCurrentPlayer();
			}
			else
			{
			    GameOver();
			}
}


//This function checks if the check box for sounds is checked 
function ConfigureSounds()
{
    var soundBox=document.getElementsByName("sounds");       
    if(soundBox[0].checked)
    {
        soundsOn=true; 
    }
    else 
    {
        soundsOn=false;
    }
   
}
function ConfigureMusic()
{
    var musicBox=document.getElementsByName("music");       
    if(musicBox[0].checked)
    {
     soundManager.play('pinko',99);
    }
    else
    {
     soundManager.stop('pinko');
    }
    
}









