private int AlphaBeta(Chessboard board, int depth, int alpha, int beta, ChessColour player) { recursiveCount += 1; if (depth == 0) { return Evaluator.Evaluate(board); } Chessboard originalBoard = new Chessboard(board); if(player == ChessColour.White) { foreach(var move in FindAllMoves(board, player)) { board.MakeMove(move.FromIndex, move.ToIndex); alpha = Math.Max(alpha, AlphaBeta(board, depth - 1, alpha, beta, ChessColour.Black)); board.UndoMove(move); if(beta <= alpha) { break; } } return alpha; } else { foreach (var move in FindAllMoves(board, player)) { board.MakeMove(move.FromIndex, move.ToIndex); beta = Math.Min(beta, AlphaBeta(board, depth - 1, alpha, beta, ChessColour.White)); board.UndoMove(move); if (beta <= alpha) { break; } } return beta; } }
/// <summary> /// Given a ray of potential moves (assuming the player isn't in check), this method will generate a ray /// a new ray which eliminates any of the potential moves which result in check. /// </summary> /// <param name="potentialMoves">The potential moves of a piece</param> /// <param name="index">The location of the piece</param> /// <param name="pieceColour">The colour of the piece</param> /// <returns>A bit array of moves which will NOT result in check for <paramref name="pieceColour"/></returns> private BitArray GetCheckPreventionRay(BitArray potentialMoves, int index, ChessColour pieceColour) { var ray = new BitArray(potentialMoves.Count, false); var board = new Chessboard(this); for (int i = 0; i < potentialMoves.Count; i++) { var bit = potentialMoves[i]; if (bit) { var move = Utils.BuildMove(Locations, index, i); board.MakeMove(move); if (!board.IsPlayerInCheck(pieceColour)) { ray[i] = true; } board.UndoMove(move); } } return ray; }