Ejemplo n.º 1
0
 public void PrintAllVulnerability()
 {
     for (BGPointID point = BGPointID.Point1; point < BGPointID.Point24; point++)
     {
         Debug.Log(string.Format("Vulnerability for {0} = {1}", point, GetVulnerability(point)));
     }
 }
Ejemplo n.º 2
0
    /// <summary>
    /// Gets the point with the specified point ID.
    /// </summary>
    public BGPoint GetPoint(BGPointID id)
    {
        switch (id)
        {
        case (BGPointID.HomeP1): return(m_HomeP1);

        case (BGPointID.HomeP2): return(m_HomeP2);

        case (BGPointID.JailP1): return(m_JailP1);

        case (BGPointID.JailP2): return(m_JailP2);

        default: return(m_Points[(int)id]);
        }
    }
Ejemplo n.º 3
0
    /// <summary>
    /// Gets the point the EXACT specified distance behind the given BOARD point with respect to the particular player.
    /// Returns null, if no such point exists.
    /// </summary>
    /// <param name="pointID"></param>
    /// <param name="player"></param>
    /// <param name="distance"></param>
    /// <returns></returns>
    public BGPoint GetPreviousPoint(BGPointID pointID, BGPlayerID player, int distance)
    {
        if (pointID <= BGPointID.Point1 || pointID >= BGPointID.Point24)
        {
            return(null);
        }

        if (player == BGPlayerID.Player1)
        {
            BGPoint previousPoint = null;
            int     previousIndex = ((int)pointID + distance);

            if (previousIndex <= 23)
            {
                previousPoint = GetPoint((BGPointID)previousIndex);
            }
            else if (previousIndex == 24)
            {
                previousPoint = GetJail(BGPlayerID.Player2);
            }

            return(previousPoint);
        }
        else if (player == BGPlayerID.Player2)
        {
            BGPoint previousPoint = null;
            int     previousIndex = ((int)pointID - distance);

            if (previousIndex >= 0)
            {
                previousPoint = GetPoint((BGPointID)previousIndex);
            }
            else if (previousIndex == -1)
            {
                previousPoint = GetJail(BGPlayerID.Player1);
            }

            return(previousPoint);
        }
        else
        {
            return(null);
        }
    }
Ejemplo n.º 4
0
 /// <summary>
 /// Automatically moves a checker from and to the given points with the specified IDs.
 /// </summary>
 /// <param name="startPointID"></param>
 /// <param name="endPointID"></param>
 public MoveResult MoveChecker(BGPointID startPointID, BGPointID endPointID)
 {
     return(MoveChecker(Board.GetPoint(startPointID), Board.GetPoint(endPointID)));
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Sets the point with the specified point Id with the specified player and checker count.
 /// Returns whether the operation was successful.
 /// </summary>
 /// <param name="id"></param>
 /// <param name="player"></param>
 /// <param name="count"></param>
 public bool SetPoint(BGPointID id, BGPlayerID player, int count)
 {
     return(GetPoint(id).Set(player, count));
 }
Ejemplo n.º 6
0
    /// <summary>
    /// Gets the chance that the specified point can be hit by an opposing player.
    /// </summary>
    /// <param name="pointID"></param>
    /// <returns></returns>
    public float GetVulnerability(BGPointID pointID)
    {
        BGPoint point = GetPoint(pointID);

        if (point.IsVulnerable())
        {
            float vulnerability = 0f;
            bool[,] hitTable = new bool[6, 6];
            BGPlayerID player   = point.Player;
            BGPlayerID opponent = (player == BGPlayerID.Player1) ? BGPlayerID.Player2 : BGPlayerID.Player1;

            for (int roll1 = 1; roll1 <= 6; roll1++)
            {
                BGPoint point1 = GetPreviousPoint(pointID, opponent, roll1);

                if (point1 != null)
                {
                    if (point1.IsOccupiedBy(opponent))
                    {
                        //Debug.Log(string.Format("{0} is vulnerable to any roll with {1}", point.ID, roll1));

                        for (int i = 0; i < 6; i++)
                        {
                            hitTable[roll1 - 1, i] = hitTable[i, roll1 - 1] = true;
                        }
                    }
                    else if (!point1.IsControlledBy(player))
                    {
                        for (int roll2 = 1; roll2 <= 6; roll2++)
                        {
                            BGPoint point2 = GetPreviousPoint(point1.ID, opponent, roll2);

                            if (point2 != null && point2.IsOccupiedBy(opponent))
                            {
                                //Debug.Log(string.Format("{0} is vulnerable to roll ({1}, {2}) from {3}", point.ID, roll1, roll2, point2));

                                hitTable[roll1 - 1, roll2 - 1] = hitTable[roll2 - 1, roll1 - 1] = true;
                            }
                        }
                    }
                }
            }

            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < 6; j++)
                {
                    if (hitTable[i, j])
                    {
                        //Debug.Log(string.Format(">> {0},{1}", i + 1, j + 1));

                        vulnerability += 1f / 36f;
                    }
                }
            }

            //Debug.Log(string.Format("Total vulnerability for {0} = {1}", point.ID, vulnerability));

            return(vulnerability);
        }
        else
        {
            return(0f);
        }
    }
Ejemplo n.º 7
0
 /// <summary>
 /// Gets a move involving the start and end points with the specified IDs. Returns null if no such move exists.
 /// </summary>
 /// <param name="startPointID"></param>
 /// <param name="endPointID"></param>
 /// <returns></returns>
 public BGMove GetMove(BGPointID startPointID, BGPointID endPointID)
 {
     return(Moves.FirstOrDefault(move => move.StartPointID == startPointID && move.EndPointID == endPointID));
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BGPoint"/> class.
 /// </summary>
 /// <param name="id">Identifier.</param>
 public BGPoint(BGPointID id)
 {
     this.ID     = id;
     this.Count  = 0;
     this.Player = BGPlayerID.None;
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Gets a move that can be made from the point with the specified ID using the specified dice roll.
 /// </summary>
 /// <param name="pointID"></param>
 /// <param name="diceRoll"></param>
 /// <returns></returns>
 public BGMove GetMoveFrom(BGPointID pointID, int diceRoll)
 {
     return(Moves.FirstOrDefault(move => move.StartPointID == pointID && move.DiceRoll == diceRoll));
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Gets whether a move exists between the start and end points with the specified IDs.
 /// </summary>
 public bool CanMove(BGPointID startPointID, BGPointID endPointID)
 {
     return(Moves.Any(move => move.StartPointID == startPointID && move.EndPointID == endPointID));
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Gets whether a move can be made from the point with the specified ID using the specified dice roll.
 /// </summary>
 /// <param name="pointID"></param>
 /// <param name="diceRoll"></param>
 /// <returns></returns>
 public bool CanMoveFrom(BGPointID pointID, int diceRoll)
 {
     return(Moves.Any(move => move.StartPointID == pointID && move.DiceRoll == diceRoll));
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Gets whether a move can be made from the point with the specified ID.
 /// </summary>
 /// <param name="pointID"></param>
 /// <returns></returns>
 public bool CanMoveFrom(BGPointID pointID)
 {
     return(Moves.Any(move => move.StartPointID == pointID));
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BGPoint"/> class.
 /// </summary>
 /// <param name="id">Identifier.</param>
 /// <param name="count">Count.</param>
 /// <param name="player">Player.</param>
 private BGPoint(BGPointID id, int count, BGPlayerID player)
 {
     this.ID     = id;
     this.Count  = count;
     this.Player = player;
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Places the current selected checker on the point with the specified ID.
 /// </summary>
 /// <param name="point"></param>
 public void PlaceChecker(BGPointID pointID)
 {
     PlaceChecker(Board.GetPoint(pointID));
 }
Ejemplo n.º 15
0
    /// <summary>
    /// Gets a list of all moves that can be made for the specified player with the specified board and dice roll.
    /// </summary>
    private List <BGMove> GetMoves(BGPlayerID player, BGBoardMap board, BGDiceRoll diceRoll)
    {
        List <BGMove> moves = new List <BGMove>();

        if (player == BGPlayerID.None)
        {
            return(moves);
        }

        BGPoint home = board.GetHome(player);
        BGPoint jail = board.GetJail(player);

        BGPoint[] innerTable = board.GetInnerTable(player);
        BGPoint[] outerTable = board.GetOuterTable(player);

        // Find available moves.

        if (jail.Count > 0)
        {
            for (int roll = 1; roll <= 6; roll++)
            {
                if (diceRoll.CanUse(roll) && !outerTable[roll - 1].IsBlocking(player))
                {
                    moves.Add(new BGMove(jail, outerTable[roll - 1], roll));
                }
            }
        }
        else
        {
            for (int roll = 1; roll <= 6; roll++)
            {
                if (diceRoll.CanUse(roll))
                {
                    for (BGPointID point = BGPointID.Point1; point <= BGPointID.Point24; point++)
                    {
                        BGPoint from = board.GetPoint(point);

                        if (from.IsOccupiedBy(player))
                        {
                            BGPoint to = null;

                            if (player == BGPlayerID.Player1 && ((point - roll) >= BGPointID.Point1))
                            {
                                to = board.GetPoint(point - roll);
                            }
                            else if (player == BGPlayerID.Player2 && ((point + roll) <= BGPointID.Point24))
                            {
                                to = board.GetPoint(point + roll);
                            }

                            if (to != null && !to.IsBlocking(player))
                            {
                                moves.Add(new BGMove(from, to, roll));
                            }
                        }
                    }
                }
            }

            if (!outerTable.Any(point => point.IsOccupiedBy(player)))
            {
                // Get max distance from home.

                int maxDistance = 0;

                for (int i = 0; i < innerTable.Length; i++)
                {
                    if (innerTable[i].IsOccupiedBy(player))
                    {
                        maxDistance = i + 1;
                    }
                }

                // Find moves that can be used to bear off.

                for (int roll = 1; roll <= 6; roll++)
                {
                    if (diceRoll.CanUse(roll))
                    {
                        if (innerTable[roll - 1].IsOccupiedBy(player))
                        {
                            moves.Add(new BGMove(innerTable[roll - 1], home, roll));
                        }
                        else if (maxDistance > 0 && innerTable[maxDistance - 1].IsOccupiedBy(player) && roll >= maxDistance)
                        {
                            moves.Add(new BGMove(innerTable[maxDistance - 1], home, roll));
                        }
                    }
                }
            }
        }

        return(moves);
    }
Ejemplo n.º 16
0
    /// <summary>
    /// Gets the AI perceived value for the specified board.
    /// </summary>
    /// <param name="board"></param>
    /// <returns></returns>
    protected override float GetScore(BGBoardMap board)
    {
        if (board.IsRunOut())
        {
            // If the board is a run out:
            //  1) Focus on getting checkers into the inner board as fast as possible and pick up as many checkers as possible.
            //  2) Penalize the score severely for each checker outside the inner table.
            //     - The farther away the checker is, the more severe the penalty.

            float score = 2000000;

            score += (board.GetPoint(BGPointID.HomeP2).Count * 10000);

            for (BGPointID id = BGPointID.Point24; id >= BGPointID.Point19; id--)
            {
                BGPoint point = board.GetPoint(id);

                if (point.Player == BGPlayerID.Player2)
                {
                    score += (point.Count * Mathf.Pow((int)id, 2));
                }
            }

            for (BGPointID id = BGPointID.Point18; id >= BGPointID.Point7; id--)
            {
                BGPoint point = board.GetPoint(id);

                if (point.Player == BGPlayerID.Player2)
                {
                    score -= (10 * point.Count * Mathf.Pow(24 + (int)(BGPointID.Point18 - id), 2));
                }
            }

            for (BGPointID id = BGPointID.Point6; id >= BGPointID.Point1; id--)
            {
                BGPoint point = board.GetPoint(id);

                if (point.Player == BGPlayerID.Player2)
                {
                    score -= (100 * point.Count * Mathf.Pow(24 + (17 - (int)id), 2));
                }
            }

            return(score);
        }
        else if (board.CanBearOff(BGPlayerID.Player2))
        {
            // If player 2 can bear off:
            //  1) Focus on picking up as many checkers as possible.
            //  2) If player 1 is jailed, penalize the score severely for each vulnerable checker.

            float score = 1000000;

            score += (board.GetPoint(BGPointID.HomeP2).Count * 10000);

            for (BGPointID id = BGPointID.Point24; id >= BGPointID.Point19; id--)
            {
                BGPoint point = board.GetPoint(id);

                if (point.Player == BGPlayerID.Player2)
                {
                    if (point.Count == 1 && board.GetJail(BGPlayerID.Player1).Count > 0)
                    {
                        score -= (10000 * point.Count * Mathf.Pow((int)id, 2));
                    }
                    else
                    {
                        score += (point.Count * Mathf.Pow((int)id, 2));
                    }
                }
            }

            return(score);
        }
        else
        {
            float score         = 0;
            int   stackingPower = 0;

            for (BGPointID id = BGPointID.Point24; id >= BGPointID.Point1; id--)
            {
                BGPoint point = board.GetPoint(id);

                if (point.Player == BGPlayerID.Player2)
                {
                    if (point.Count > 2)
                    {
                        stackingPower++;
                        score += Mathf.Pow((int)id, 2);
                    }
                    else if (point.Count == 2)
                    {
                        stackingPower++;
                        score += (5 * Mathf.Pow((int)id, 2));
                    }
                    else if (point.Count == 1)
                    {
                        score -= (10 * Mathf.Pow((int)id, 2) * board.GetVulnerability(point.ID));
                    }
                }
                else if (point.Player == BGPlayerID.Player1)
                {
                    if (point.Count == 1)
                    {
                        score -= Mathf.Pow(BGPointID.Point24 - id, 2);
                    }
                }

                if (!(point.Player == BGPlayerID.Player2 && point.Count >= 2))
                {
                    if (stackingPower > 1)
                    {
                        score += (Mathf.Pow(stackingPower - 1, 2) * 1000);
                    }

                    stackingPower = 0;
                }
            }

            score += (board.GetPoint(BGPointID.HomeP2).Count * 10000);

            return(score);
        }
    }
Ejemplo n.º 17
0
    /// <summary>
    /// Handles incoming data from the server.
    /// </summary>
    /// <param name="data"></param>
    protected override void OnIncomingData(string data)
    {
        Debug.Log(string.Format("Received data from server: {0}", data));

        string[] aData = data.Split('|');

        switch (aData[0])
        {
        case (NetworkCommands.Who):
        {
            Send(string.Format("{0}|{1}|{2}", NetworkCommands.Who, ClientName, IsHost));
        }
        break;

        case (NetworkCommands.Start):
        {
            GameManager.Instance.LoadGameScene();
        }
        break;

        case (NetworkCommands.RequestDiceRoll):
        {
            BackgammonGame.Instance.RollDice();
        }
        break;

        case (NetworkCommands.DiceRoll):
        {
            int roll1 = 0;
            int roll2 = 0;

            if (int.TryParse(aData[1], out roll1) && int.TryParse(aData[2], out roll2))
            {
                BackgammonGame.Instance.RollDice(roll1, roll2);
            }
        }
        break;

        case (NetworkCommands.OfferDoublingCube):
        {
            BackgammonGame.Instance.OfferDoublingCube();
        }
        break;

        case (NetworkCommands.AcceptDoublingCube):
        {
            BackgammonGame.Instance.AcceptDoublingCube();
        }
        break;

        case (NetworkCommands.DeclineDoublingCube):
        {
            BackgammonGame.Instance.DeclineDoublingCube();
        }
        break;

        case (NetworkCommands.MovesMade):
        {
            for (int i = 1; i < aData.Length; i++)
            {
                string[] move = aData[i].Split('-');

                BGPointID startPointID = (BGPointID)int.Parse(move[0]);
                BGPointID endPointID   = (BGPointID)int.Parse(move[1]);

                BackgammonGame.Instance.MoveChecker(startPointID, endPointID);
            }

            BackgammonGame.Instance.EndTurn();
        }
        break;
        }
    }