public int Evaluate(Chessboard board) { int value = 0; value += EvaluatePieceValues(board); return value; }
/// <summary> /// Private copy constructor. Used for copying. /// </summary> /// <param name="board"></param> public Chessboard(Chessboard board) { _locations = CreateChessBoard(); for (int i = 0; i < board.Locations.Count; i++) { Locations[i].Piece = board.Locations[i].Piece; } UpdatePieceLocations(); }
private int EvaluatePieceValues(Chessboard board) { int value = 0; foreach (var location in board.Locations) { value += GetValueOfPiece(location.Piece); } return value; }
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; } }
public ChessMove FindBestAlphaBetaMove(Chessboard board, ChessColour player) { var moves = new List<Tuple<int, ChessMove>>(); foreach(var move in FindAllMoves(board, player)) { var newBoard = new Chessboard(board); newBoard.MakeMove(move.FromIndex, move.ToIndex); var score = AlphaBeta(newBoard, MAX_PLY - 1, int.MinValue, int.MaxValue, Utils.GetOtherColour(player)); moves.Add(Tuple.Create( score, move)); } if(player == ChessColour.White) { return FindMax(moves).Item2; } else { return FindMin(moves).Item2; } }
/// <summary> /// Performs minimax search for best move. No pruning /// </summary> /// <param name="board"></param> /// <param name="player"></param> /// <returns></returns> public ChessMove FindBestMove(Chessboard board, ChessColour player) { return FindBestMove(board, player, MAX_PLY).Item2; }
private Tuple<int, ChessMove> FindBestMove(Chessboard board, ChessColour player, int currentPly) { recursiveCount += 1; var movesAndScore = new List<Tuple<int, ChessMove>>(); foreach(var move in FindAllMoves(board, player)) { var newBoard = new Chessboard(board); newBoard.MakeMove(move.FromIndex, move.ToIndex); // base case if (currentPly == 1) { movesAndScore.Add(new Tuple<int, ChessMove>(Evaluator.Evaluate(newBoard), move)); } else //recursive case { var bestMove = FindBestMove(newBoard, Utils.GetOtherColour(player), currentPly - 1); movesAndScore.Add(Tuple.Create(bestMove.Item1, move)); } } if (player == ChessColour.Black) { return FindMin(movesAndScore); } else { return FindMax(movesAndScore); } }
private IEnumerable<ChessMove> FindAllMoves(Chessboard board, ChessColour player) { var pieceLocations = board.GetLocations(player); foreach (var location in pieceLocations) { var locationIndex = board.Locations.IndexOf(location); var ray = board.GetRay(location); for (int i = 0; i < ray.Count; i++) { if (ray[i]) { yield return new ChessMove(locationIndex, i, location.Piece, board.Locations[i].Piece); } } } }
/// <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; }
public void initialize() { this.board = new Chessboard(); }