﻿/* Written by Greg Thatcher, http://www.GregThatcher.com */

    var theNumShuttles = 9;
    var theRedPieceCount = 5;
    var theGreenPieceCount = 5;
    var theWaitForComputerMove = false;
    
    function createXmlDocument (xmlString)
    {    
        if (window.ActiveXObject)
        {
            var doc=new ActiveXObject("Microsoft.XMLDOM");
            doc.async="false";
            doc.loadXML(xmlString);
        }
        // code for Mozilla, Firefox, Opera, etc.
        else
        {
            var parser=new DOMParser();
            var doc=parser.parseFromString(xmlString,"text/xml");
        }
        return doc;
    }
 

    
    /******************* Position Class ****************************/
    function Position (shuttle, hole)
    {
        this.shuttle = shuttle;
        this.hole = hole;
    }
    
    
    
    /******************* Move Class ****************************/
    function Move ()
    {
        this.isShuttleMove = false;
        this.shuttle = 0;
        this.shiftBy = 0;
        this.fromShuttle = 0;
        this.toShuttle = 0;
        this.fromHole = 0;
        this.toHole = 0;
    }
    
    Move.prototype.GetDescription = function()
    {
        var description = "";
        if (this.isShuttleMove)
        {
            description = "<move type='shuttle' shuttle='" + this.shuttle + "' shift='" + this.shiftBy + "' />";
        }
        else
        {
            description = "<move type='peg' fromshuttle='" + this.fromShuttle + "' fromhole='" + this.fromHole + "' toshuttle='" + this.toShuttle + "' tohole='" + this.toHole + "' />";
        }
        return description;
    }
    
    Move.prototype.MoveShuttle = function(shuttle, shiftBy)
    {
        this.isShuttleMove = true;
        this.shuttle = shuttle;
        this.shiftBy = shiftBy;
    }

    Move.prototype.MovePeg = function(oldShuttle, shuttle, oldHole, hole)
    {
        theirBoard.lastMove.isShuttleMove = false;
        theirBoard.lastMove.fromShuttle = oldShuttle;
        theirBoard.lastMove.toShuttle = shuttle;
        theirBoard.lastMove.fromHole = oldHole;
        theirBoard.lastMove.toHole = hole;
    }

    /******************* Board Class ****************************/
    function Board ()
    {
        this.shifts = new Array(theNumShuttles);
        this.lastMiddleMover = -1;
        this.redPieces = new Array(theRedPieceCount);
        this.greenPieces = new Array(theGreenPieceCount);
        this.computerMovedMiddleShuttle = false;
        
        var i;
        for (i = 0; i < theRedPieceCount; i++)
        {
            this.redPieces[i] = new Position (0, 0);
        }
        
        for (i = 0; i < theGreenPieceCount; i++)
        {
            this.greenPieces[i] = new Position (0, 0);
        }
        for (i = 0; i < theNumShuttles; i++)
        {
            this.shifts[i] = 0;
        }
    }
    
    
    Board.prototype.GetDescription =  function ()
    {
        var descriptionString = "<board>\r\n";
        var computerMovedShuttleLast;
        for (i = 0; i < theRedPieceCount; i++)
        {
            descriptionString += "<redpiece shuttle='" + theirBoard.redPieces[i].shuttle + "' hole='" + theirBoard.redPieces[i].hole + "' />\r\n";
        }
        
        for (i = 0; i < theGreenPieceCount; i++)
        {
            descriptionString += "<greenpiece shuttle='" + theirBoard.greenPieces[i].shuttle + "' hole='" + theirBoard.greenPieces[i].hole + "' />\r\n";
        }
        for (i = 0; i < theNumShuttles; i++)
        {
            descriptionString += "<shuttle index='" + i + "' shift='" + theirBoard.shifts[i] + "' />\r\n";
        }
        //document.getElementById("TextAreaDescription").value = descriptionString;
        descriptionString += "</board>\r\n";
        computerMovedShuttleLast = "<computerMovedMiddleShuttleLastTime>" + this.computerMovedMiddleShuttle + "</computerMovedMiddleShuttleLastTime>"
        return "<shuttlesgame>\r\n" + descriptionString + "\r\n" + this.lastMove.GetDescription() + "\r\n" + computerMovedShuttleLast + "\r\n</shuttlesgame>\r\n";
        
    }
    Board.prototype.Initialize = function ()
    {
        this.redPieces[0].shuttle = 0;
        this.redPieces[0].hole = 0;
        this.redPieces[1].shuttle = 0;
        this.redPieces[1].hole = 1;
        this.redPieces[2].shuttle = 0;
        this.redPieces[2].hole = 2;
        this.redPieces[3].shuttle = 0;
        this.redPieces[3].hole = 3;
        this.redPieces[4].shuttle = 0;
        this.redPieces[4].hole = 4;
        
        this.greenPieces[0].shuttle = 8;
        this.greenPieces[0].hole = 0;
        this.greenPieces[1].shuttle = 8;
        this.greenPieces[1].hole = 1;
        this.greenPieces[2].shuttle = 8;
        this.greenPieces[2].hole = 2;
        this.greenPieces[3].shuttle = 8;
        this.greenPieces[3].hole = 3;
        this.greenPieces[4].shuttle = 8;
        this.greenPieces[4].hole = 4;
        
        this.lastBoardDescription = '';
        this.lastMove = new Move();

    }
    Board.prototype.MoveShuttle = function (shuttle, shift)
    {
        this.shifts[shuttle] += shift;    
        this.lastMove = new Move();
        this.lastMove.MoveShuttle (shuttle, shift);
    }
    Board.prototype.MovePeg = function (computerMove, pegIndex, shuttle, hole)
    {
        
        var oldShuttle;
        var oldHole;
        //alert ("PegIndex: " + pegIndex);
        if (computerMove)
        {
            oldShuttle = theirBoard.greenPieces[pegIndex].shuttle;
            oldHole = theirBoard.greenPieces[pegIndex].hole;
            this.greenPieces[pegIndex].shuttle = shuttle;
            this.greenPieces[pegIndex].hole = hole;
        }
        else
        {
            oldShuttle = theirBoard.redPieces[pegIndex].shuttle;
            oldHole = theirBoard.redPieces[pegIndex].hole;
            this.redPieces[pegIndex].shuttle = shuttle;
            this.redPieces[pegIndex].hole = hole;
        }        
        this.lastMove = new Move();
        this.lastMove.MovePeg (oldShuttle, shuttle, oldHole, hole);
        
        //alert ("Board.MovePeg");
    }    


