

/*****************************************************************************
 *
 *  Define the BetaComputerPlayer Object and Functions
 *
 *****************************************************************************/
function BetaComputerPlayer(aName, theRiskLevel)
{
    this.init(aName, theRiskLevel);
}

/**
 *  Setup inheritance
 */
BetaComputerPlayer.prototype = new ComputerPlayer();
BetaComputerPlayer.prototype.constructor = BetaComputerPlayer;
BetaComputerPlayer.superclass = ComputerPlayer.prototype;

BetaComputerPlayer.prototype.init = function(aName, theRiskLevel)
{
    BetaComputerPlayer.superclass.init.call(this, aName, theRiskLevel);

    this.startingTripWeight = 4;
    this.tripWeight = this.startingTripWeight;
    this.riskLevel = theRiskLevel;
}

BetaComputerPlayer.prototype.startNewHand = function()
{
    BetaComputerPlayer.superclass.startNewHand.call(this);
    this.tripWeight = this.startingTripWeight;
}

BetaComputerPlayer.prototype.analyzeMove = function(rows, mustTakeRow, theRound)
{
    var alone = this.areWeAlone(theRound);

    if (!alone)
    {
        this.tripWeight -= this.riskLevel;
    }

    // default action is to not take a row unless one looks good or we have to
    var rowToTake = -1;
    var ties = new Array();
    var bestWeight = -1000;
    var weightSum = 0;
    var decidedToTakeRow = false;
    var foundEmptyRow = false;

    // figure out which row we want to take if we were to take one.
    for (var i=0; i<rows.length; i++)
    {
        if ((rows[i] != null) && (rows[i].length > 1))
        {
            // find the weight of this row
            var weight = this.assignRowWeight(rows, i, theRound);
            weightSum += weight;

            // if it's the best so far
            if (weight > bestWeight)
            {
                // start a new bucket to hold ties at this weight
                bestWeight = weight;
                ties = new Array();
            }

            // if the weight of this row is a tie with other then
            // add it to the bucket.
            if (bestWeight == weight)
            {
                ties.push(i);
            }

            if ((rows[i] != null) && (rows[i].length == 1))
            {
                foundEmptyRow = true;
            }
        }
    }

    // if I'm alone and the bestWeight is still in my favor, then
    // I'll bump up my push luck factor.
    if (alone && (bestWeight > 2))
    {
        this.tripWeight += this.riskLevel;
    }

    // from the bestWeigtht make a decision
    if (bestWeight >= this.tripWeight)
    {
        decidedToTakeRow = true;
    }

    var originalMove = BetaComputerPlayer.superclass.analyzeMove.call(this, rows, mustTakeRow, theRound);

    // if we have to take a row or we've decided to take a row
    if (mustTakeRow || decidedToTakeRow)
    {
        var myPick = Math.floor((Math.random() * 15791)) % ties.length;
        rowToTake = ties[myPick];
    }

    if ((originalMove != rowToTake) && (originalMove != 0))
    {
        //alert("Original Move was " + originalMove + " Beta move is " + rowToTake);
    }

    return rowToTake;
}

BetaComputerPlayer.prototype.assignRowWeight = function(rows, rowIndex, theRound)
{
    var row = rows[rowIndex];
    if ((row == null) || (row.length <= 1))
    {
        return -1;
    }

    var players = theRound.getPlayers();
    var myScore = this.hand.scoreRow(row);
    var myScoreChange = (myScore.after - myScore.before);

    return myScoreChange;
}