Exemple #1
0
        private static void DoEvaluate()
        {
            //var fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
            var fen               = "r4rk1/p2n1ppp/3qp3/6B1/N5P1/3P1b2/PPP1BbP1/R2Q1R1K b - - 0 14";
            var boardFactory      = new BoardFactory();
            var board             = boardFactory.ParseFEN(fen);
            var evaluationService = new EvaluationService();
            var evaluation        = evaluationService.Evaluate(board, null);

            Console.WriteLine(evaluation);
        }
        private static void TestMove()
        {
            var fact  = new BoardFactory();
            var board = fact.ParseFEN("r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -");

            board.EnPassantFile = Board.Files[3];
            var hyperbola         = new HyperbolaQuintessence();
            var evaluationService = new EvaluationService();

            Console.WriteLine(evaluationService.Evaluate(board));
            var attacksService = new AttacksService(hyperbola);
            var movesService   = new PossibleMovesService(attacksService, hyperbola);
            var forWhite       = true;
            var moves          = movesService.GetPossibleKingMoves(board).ToList();
            var dests          = moves.Select(x => x.To);
            var toMoveBoard    = fact.PiecesToBitBoard(dests);
            var attacked       = attacksService.GetAllAttacked(board);

            var newMove    = new Move(4, 2, ChessPiece.WhiteKing);
            var movedBoard = board.DoMove(newMove);

            Debugging.ShowBitBoard(movedBoard.BitBoard[ChessPiece.WhiteKing], movedBoard.BitBoard[ChessPiece.WhiteRook]);
        }
        public int Quiessence(int alpha, int beta, Board board, int currentDepth)
        {
#if TEST
            Test.Assert(beta > alpha);
            board.CheckBoard();
#endif
            if ((NodesSearched & 2047) == 0)
            {
                if (Interruptor.IsInterrupted())
                {
                    Stopped = true;
                    return(0);
                }
            }

            NodesSearched++;

            var isRepetition = IsRepetition(board);
            if (isRepetition)
            {
                return(0);
            }

            if (board.History.Length - board.LastTookPieceHistoryIndex >= 100)
            {
                return(0);
            }

            if (currentDepth >= MaxDepth)
            {
                return(EvaluationService.Evaluate(board));
            }

            int score = EvaluationService.Evaluate(board);

            if (score >= beta)
            {
                return(beta);
            }
            if (score > alpha)
            {
                alpha = score;
            }

            var potentialMoves = PossibleMovesService.GetAllPotentialMoves(board).Where(x => x.TakesPiece > 0).ToList();

            var oldAlpha   = alpha;
            var validMoves = 0;

            Move?bestMove  = null;
            var  bestScore = -Inf;
            score = -Inf;

            for (var i = 0; i < potentialMoves.Count; i++)
            {
                SortNextMove(i, board, potentialMoves, currentDepth, null);
                var potentialMove = potentialMoves[i];
                var bbAfter       = PossibleMovesService.DoMoveIfKingSafe(board, potentialMove);
                if (bbAfter == null)
                {
                    continue;
                }
                validMoves++;

                score = -Quiessence(-beta, -alpha, bbAfter, currentDepth + 1);

                if (Stopped)
                {
                    return(0);
                }

                if (score > alpha)
                {
                    if (score >= beta)
                    {
#if TEST
                        if (validMoves == 1)
                        {
                            FailHighFirst++;
                        }
                        FailHigh++;
#endif
                        return(beta);
                    }
                    alpha = score;
                }
            }
#if TEST
            Test.Assert(alpha >= oldAlpha);
            Test.Assert(alpha < Inf);
            Test.Assert(alpha > -Inf);
#endif
            return(alpha);
        }
        public int PrincipalVariationSearch(int alpha, int beta, Board board, int depth, int currentDepth, bool allowNullMoveSearch)
        {
#if TEST
            Test.Assert(beta > alpha);
            Test.Assert(depth >= 0);
            board.CheckBoard();
#endif
            if ((NodesSearched & 2047) == 0)
            {
                if (Interruptor.IsInterrupted())
                {
                    Stopped = true;
                    return(0);
                }
            }

            NodesSearched++;

            var isRepetition = IsRepetition(board);
            if (isRepetition && currentDepth > 0)
            {
                return(0);
            }

            if (board.History.Length - board.LastTookPieceHistoryIndex >= 100)
            {
                return(0);
            }

            if (currentDepth >= MaxDepth)
            {
                return(EvaluationService.Evaluate(board));
            }

            if (depth <= 0)
            {
                // quiessence next
                return(Quiessence(alpha, beta, board, currentDepth));
            }

            var enemyAttacks = PossibleMovesService.AttacksService.GetAllAttacked(board, !board.WhiteToMove);
            var myKing       = board.WhiteToMove ? board.BitBoard[ChessPiece.WhiteKing] : board.BitBoard[ChessPiece.BlackKing];
            var inCheck      = (enemyAttacks & myKing) != 0;

            if (inCheck)
            {
                depth++;
            }

            int  score  = -Inf;
            Move?pvMove = null;

            SearchTTEntry foundEntry;
            var           found = TTable.TryGet(board.Key, out foundEntry);
            if (found && foundEntry.Key == board.Key)
            {
                pvMove = foundEntry.Move;
                if (foundEntry.Depth >= depth)
                {
                    switch (foundEntry.Flag)
                    {
                    case SearchTTFlags.Beta:
                        return(beta);

                        break;

                    case SearchTTFlags.Exact:
                        return(foundEntry.Score);

                        break;

                    case SearchTTFlags.Alpha:
                        return(alpha);

                        break;
                    }
                }
            }

            const int nullMoveFactor = 2;
            if (allowNullMoveSearch && !inCheck && currentDepth > 0 && depth >= nullMoveFactor + 1)
            {
                var haveBigPiece = false;
                var pieceOffset  = board.WhiteToMove ? 1 : 7;
                for (var i = 1; i < 5; i++)
                {
                    if (board.PieceCounts[pieceOffset + i] > 0)
                    {
                        haveBigPiece = true;
                        break;
                    }
                }
                if (haveBigPiece)
                {
                    var nullMove  = new Move(0, 0, 0);
                    var nullBoard = board.DoMove(nullMove);
                    score = -PrincipalVariationSearch(-beta, -beta + 1, nullBoard, depth - nullMoveFactor - 1, currentDepth + 1, false);
                    if (Stopped)
                    {
                        return(0);
                    }

                    if (score >= beta && score > -MateThereshold && score < MateThereshold)
                    {
                        NullMoveCutOffs++;
                        return(beta);
                    }
                }
            }

            var potentialMoves = PossibleMovesService.GetAllPotentialMoves(board);

            var oldAlpha   = alpha;
            var validMoves = 0;

            Move?bestMove  = null;
            var  bestScore = -Inf;
            score = -Inf;

            var bbsAfter = new Board[potentialMoves.Count];
            for (var i = 0; i < potentialMoves.Count; i++)
            {
                SortNextMove(i, board, potentialMoves, currentDepth, pvMove);
                var potentialMove = potentialMoves[i];
                var bbAfter       = PossibleMovesService.DoMoveIfKingSafe(board, potentialMove);
                if (bbAfter == null)
                {
                    continue;
                }
                bbsAfter[i] = bbAfter;
                validMoves++;

                score = -PrincipalVariationSearch(-beta, -alpha, bbAfter, depth - 1, currentDepth + 1, true);

                if (Stopped)
                {
                    return(0);
                }

                if (score > bestScore)
                {
                    bestScore = score;
                    bestMove  = potentialMove;

                    if (score > alpha)
                    {
                        if (score >= beta)
                        {
#if TEST
                            if (validMoves == 1)
                            {
                                FailHighFirst++;
                            }
                            FailHigh++;
#endif

                            if (potentialMove.TakesPiece == 0)
                            {
                                SearchKillers[currentDepth, 1] = SearchKillers[currentDepth, 0];
                                SearchKillers[currentDepth, 0] = potentialMove.Key;
                            }

                            var entry = new SearchTTEntry(board.Key, bestMove.Value, beta, SearchTTFlags.Beta, depth);
                            TTable.Add(board.Key, entry);

                            return(beta);
                        }
                        alpha = score;

                        if (potentialMove.TakesPiece == 0)
                        {
                            SearchHistory[potentialMove.Piece, potentialMove.To] += depth;
                        }
                    }
                }
            }

            if (validMoves == 0)
            {
                if (inCheck)
                {
                    return(-MateScore + currentDepth);
                }
                else
                {
                    return(0);
                }
            }

#if TEST
            Test.Assert(alpha >= oldAlpha);
#endif

            if (alpha != oldAlpha)
            {
                var entry = new SearchTTEntry(board.Key, bestMove.Value, bestScore, SearchTTFlags.Exact, depth);
                TTable.Add(board.Key, entry);
                //PVTable[currentDepth] = new PVSResult(alpha, bbsAfter[bestMove], potentialMoves[bestMove]);
            }
            else
            {
                var entry = new SearchTTEntry(board.Key, bestMove.Value, alpha, SearchTTFlags.Alpha, depth);
                TTable.Add(board.Key, entry);
            }

            return(alpha);
        }