/// <summary> /// Calculate the heuristic for the state of the board, based on this player. /// </summary> /// <param name="board">The current state of the board in the search.</param> /// <returns>The heuristic for this node in the search.</returns> private int CalculateMiniMaxHeuristic(Board board) { // safety-check the board parameter if (board == null) { throw new ArgumentNullException("board"); } int myPieces = board.GetColorCount(BoardColor); int otherPieces = board.GetOppositeColorCount(BoardColor); int heuristic = myPieces - otherPieces; // in order to stop the algorithm from going straight to the nearest edge, // introduce some randomness if (myPieces + otherPieces <= randomHeuristicCutoff) { heuristic += (int)(2.0 * random.NextDouble()); } return(heuristic); }
/// <summary> /// Update the AI player. /// </summary> /// <param name="board">The current game board.</param> public override void Update(Board board) { // safety-check the board parameter if (board == null) { throw new ArgumentNullException("board"); } // if it's not our turn, then we're done if (board.CurrentColor != BoardColor) { return; } // generate a new move. Move move; bool moveFound = false; // in order to begin the game with some variety, the AI will make random // acceptable moves until 25 moves have been made, then switch to minimax int totalPieces = board.GetColorCount(BoardColor) + board.GetOppositeColorCount(BoardColor); if (totalPieces <= randomMoveCutoff) { moveFound = GenerateMoveRandom(board, out move); } else { moveFound = GenerateMoveMiniMax(board, maximumDepth, out move); } // apply the move if (moveFound) { board.ApplyMove(BoardColor, move); } else { board.Pass(BoardColor); } }