/*********
Game object
**********/
function Game(){
if(!document.createElement || !document.appendChild){
alert("Your browser does not appear to support the needed javascript.\n"+
"You probably need to upgrade your browser for the game to function.");
return;
}


//properties for whole game
this.msg = new getObj('messages').obj; //for messages
this.pegHeld = new PegArea('pegHeld',1,1,'');

this.selectedPegType = ''; //type of peg being held
this.playerTurn = 'A'; //'A' or 'B' indicating which player's turn
this.lastPlacedPeg = ''; //peg object that was last put on the board


//create pegAreas
this.Apegs = new PegArea('Apegs', 5,5, 'AHome');
this.Ascore = new PegArea('Ascore', 5,1, 'emptyHome');
this.board = new PegArea('board', 13,13, 'emptyBoard');
this.Bscore = new PegArea('Bscore', 5,1, 'emptyHome');
this.Bpegs = new PegArea('Bpegs', 5,5, 'BHome');

}//end game object

/**********
method function to undo a move
**********/
Game.prototype.undoMove = function(){
//this.board.showPegs();
if(this.selectedPegType != ''){
//peg selected
        theGame.msg.innerHTML = 'You haven\'t made a move.'
}else if(this.lastPlacedPeg == ''){
//player hasn't done anything
        theGame.msg.innerHTML = 'Nothing to undo.'
}else{

eval('this.'+this.lastPlacedPeg.type.substr(0,1)+'pegs.getEmpty().setType(this.lastPlacedPeg.type)');



theGame.msg.innerHTML = 'Most recent move undone. It is player '+
        this.lastPlacedPeg.type.substr(0,1)+'\s turn.'


this.lastPlacedPeg.setType('emptyBoard');
this.playerTurn = (this.playerTurn == 'A') ? 'B' : 'A';
this.lastPlacedPeg = '';
this.selectedPegType = '';
}
}
/**********
method function to check for a winner
**********/
Game.prototype.checkWin = function(pegObj){
//first, count adjacent pegs
//if one direction has 5, highlight them, and end the game.


var pegObjInfo = this.getPegInfo(pegObj.id)
var relatedPegs = pegObj.pegArea.getRelated(pegObjInfo[1],pegObjInfo[2]);
var countArr = new Array();

for(var diri=1;diri<10;diri++){
if(diri < 5){
countArr[diri] = 1;
}else if(diri > 5){
countArr[diri] = 0;
}
}
var retArr = new Array();
var tempDir;

for(var index=0;index<relatedPegs.length;index++){
        if(relatedPegs[index].type == pegObj.type){
                tempDir = findDir(pegObj.id, relatedPegs[index].id)
                countArr[tempDir] += 1;
        }
}

//need to combine counts

/*
1 & 9
2 & 8
3 & 7
4 & 6
*/
retArr[0] = countArr[1] + countArr[9];
retArr[1] = countArr[2] + countArr[8];
retArr[2] = countArr[3] + countArr[7];
retArr[3] = countArr[4] + countArr[6];
theGame.msg.innerHTML = retArr.join(',');

if(retArr.join('').search(/5/) != -1){
theGame.msg.innerHTML = 'Player '+pegObj.type.substring(0,1)+' has won!';
winner(pegObj.type.substring(0,1));
}

/*******
function for when there is a winner
*******/
function winner(player){
//get the placer's scoring area
var scoreArea = eval('theGame.'+player+'score');

//check that it isn't full
if(typeof(scoreArea.getEmpty()) != undefined && player != 'A'){
//B won, put anextra peg in
scoreArea.getEmpty().setType(player+'Win');
}else if(typeof(scoreArea.getEmpty()) != undefined){
//put one peg in
scoreArea.getEmpty().setType(player+'Win');
}else{
//score is full
alert('Player '+player+' is the first to reach 5 points, and wins the match!');
newGameBtn.disabled = false;
undoBtn.disabled = false;
//mouse clicks anywhere on game should not work
gameInProgress = false;
}


newGameBtn.disabled = false;
}


/*********
function takes two pegs, and 
finds direction from 1st to 2nd.
*********/
function findDir(originPegId, destPegId){
var oRow = getRowCol(originPegId)[0];
var oCol = getRowCol(originPegId)[1];
var dRow = getRowCol(destPegId)[0];
var dCol = getRowCol(destPegId)[1];
var dirRow = oRow-dRow;
var dirCol = oCol-dCol;

/*
1 = left up 
2 = up 
3 = right up 
4 = left 
6 = right 
7 = left down
8 = down
9 = right down
*/

function getDirRow(dir){
if(dir == 0){
//on same row
    return 4;
}else if(dir < 0){
//above origin
    return 7;
}else{
//below origin
    return 1;
}
}
function getDirCol(dir){
if(dir == 0){
//on same col
    return 1;
}else if(dir < 0){
//to right of origin
    return 2;
}else{
//to left of origin
    return 0;
}
}

return getDirCol(dirCol)+getDirRow(dirRow);

}
/**********
function that takes an id, and 
returns an array of row(0) and col(1) id
**********/
function getRowCol(PegID){
var retArray = new Array();
//row
retArray[0] = PegID.substring(PegID.search(/r[0-9]/)+1,PegID.search(/c[0-9]/));
//col
retArray[1] = PegID.substring(PegID.search(/c[0-9]/)+1);

return retArray;
}




}
/**********
method function to send click event to peg
**********/
Game.prototype.clickPeg = function(pegId){

var pegDetails = this.getPegInfo(pegId);

eval('this.'+pegDetails[0]+'.clickPeg('+pegDetails[1]+','+pegDetails[2]+')');
}


/**********
method function that takes an id, and 
returns an array of id(0) row(1) and col(2)
**********/
Game.prototype.getPegInfo =  function(PegID){
var retArray = new Array();
//id
retArray[0] = PegID.substring(0, PegID.search(/r[0-9]/));
//row
retArray[1] = PegID.substring(PegID.search(/r[0-9]/)+1,PegID.search(/c[0-9]/));
//col
retArray[2] = PegID.substring(PegID.search(/c[0-9]/)+1);

return retArray;
}

/******
method function to reset for new game
*******/
Game.prototype.reset = function(){
theGame.Apegs.reset();
theGame.Ascore.reset();
theGame.board.reset();
theGame.Bscore.reset();
theGame.Bpegs.reset();
}

