/// <summary> /// Initializes a new instance of the <see cref="BGMove"/> class. /// </summary> /// <param name="start">Start.</param> /// <param name="end">End.</param> /// <param name="diceRoll">Dice roll.</param> public BGMove(BGPoint startPoint, BGPoint endPoint, int diceRoll) { this.StartPointID = startPoint.ID; this.EndPointID = endPoint.ID; this.DiceRoll = diceRoll; this.IsHit = (startPoint.Player != endPoint.Player) && (endPoint.Count == 1); }
/// <summary> /// Gets the outer table for the specified player. /// </summary> public BGPoint[] GetOuterTable(BGPlayerID player) { switch (player) { case (BGPlayerID.Player1): { BGPoint[] outerTable = new BGPoint[18]; for (int i = 0; i < 18; i++) { outerTable[i] = this.m_Points[23 - i]; } return(outerTable); } case (BGPlayerID.Player2): { BGPoint[] outerTable = new BGPoint[18]; for (int i = 0; i < 18; i++) { outerTable[i] = this.m_Points[i]; } return(outerTable); } default: { return(null); } } }
/// <summary> /// Gets the inner table for the specified player. /// </summary> public BGPoint[] GetInnerTable(BGPlayerID player) { switch (player) { case (BGPlayerID.Player1): { BGPoint[] innerTable = new BGPoint[6]; for (int i = 0; i < 6; i++) { innerTable[i] = this.m_Points[i]; } return(innerTable); } case (BGPlayerID.Player2): { BGPoint[] innerTable = new BGPoint[6]; for (int i = 0; i < 6; i++) { innerTable[i] = this.m_Points[23 - i]; } return(innerTable); } default: { return(null); } } }
/// <summary> /// Initializes a new instance of the <see cref="MoveCalculator"/> class. /// </summary> public BGBoardMap() { this.m_Points = new BGPoint[24]; this.m_HomeP1 = new BGPoint(BGPointID.HomeP1); this.m_HomeP2 = new BGPoint(BGPointID.HomeP2); this.m_JailP1 = new BGPoint(BGPointID.JailP1); this.m_JailP2 = new BGPoint(BGPointID.JailP2); for (int i = 0; i < 24; i++) { this.m_Points[i] = new BGPoint((BGPointID)i); } }
/// <summary> /// Applies the specified move to the board map. /// Returns whether the move has been applied successfully, resulting in the board map being changed. /// </summary> /// <param name="move"></param> /// <returns></returns> public bool Use(BGMove move) { BGPoint startPoint = GetPoint(move.StartPointID); BGPoint endPoint = GetPoint(move.EndPointID); // Return immediately if the move is invalid. if (startPoint.Count == 0) { return(false); } // Case 1: The endpoint does not have any checkers. // Case 2: The endpoint is occupied by the same player occupying the start point. if (endPoint.Player == BGPlayerID.None || startPoint.Player == endPoint.Player) { startPoint.Count--; endPoint.Count++; endPoint.Player = startPoint.Player; if (startPoint.Count == 0) { startPoint.Player = BGPlayerID.None; } return(true); } // Case 3: The endpoint is occupied by the opponent of the player occupying the start point and is vulnerable. else if (endPoint.Player != startPoint.Player && endPoint.Count == 1) { GetJail(endPoint.Player).Count++; startPoint.Count--; endPoint.Player = startPoint.Player; if (startPoint.Count == 0) { startPoint.Player = BGPlayerID.None; } return(true); } return(false); }
/// <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); } }
/// <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); } }
/// <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); } }
/// <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); }