Exemplo n.º 1
0
        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;
            }
        }
Exemplo n.º 2
0
        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;
            }
        }
Exemplo n.º 3
0
        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);
            }
        }
Exemplo n.º 4
0
        /// <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;
        }