Пример #1
0
        private static int perft(ChessBoard cb, ThreadData threadData, int depth)
        {
            threadData.StartPly();
            MoveGenerator.GenerateMoves(threadData, cb);
            MoveGenerator.GenerateAttacks(threadData, cb);

            if (depth == 0)
            {
                threadData.EndPly();
                return(1);
            }

            var counter = 0;

            while (threadData.HasNext())
            {
                var move = threadData.Next();
                if (!cb.IsLegal(move))
                {
                    continue;
                }

                cb.DoMove(move);
                counter += perft(cb, threadData, depth - 1);
                cb.UndoMove(move);
            }

            threadData.EndPly();
            return(counter);
        }
Пример #2
0
        public static void Main()
        {
            var cb = ChessBoardInstances.Get(0);

            // read all fens, including score
            var fens = Tuner.LoadFens("d:\\backup\\chess\\epds\\violent.epd", false, true);

            Console.WriteLine("Fens found : " + fens.Count);

            double sameScore    = 0;
            double totalAttacks = 0;
            var    start        = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

            foreach (var entry in fens)
            {
                ChessBoardUtil.SetFen(entry.Key, cb);
                _threadData.StartPly();
                MoveGenerator.GenerateAttacks(_threadData, cb);
                while (_threadData.HasNext())
                {
                    var move = _threadData.Next();
                    if (!cb.IsLegal(move))
                    {
                        continue;
                    }

                    totalAttacks++;
                    var seeScore      = SeeUtil.GetSeeCaptureScore(cb, move);
                    var materialScore = EvalUtil.CalculateMaterialScore(cb);
                    var qScore        = ChessConstants.ColorFactor[cb.ColorToMoveInverse] * materialScore -
                                        CalculateQScore(cb, move, true);
                    if (seeScore == qScore)
                    {
                        sameScore++;
                    }

                    // else {
                    // seeScore = SEEUtil.getSeeCaptureScore(cb, move);
                    // qScore = ChessConstants.COLOR_FACTOR[cb.colorToMoveInverse] * materialScore - calculateQScore(cb,
                    // move, true);
                    // }
                }

                _threadData.EndPly();
            }

            Console.WriteLine($"{sameScore:f0} {totalAttacks:f0} = {sameScore / totalAttacks:f4}");
            Console.WriteLine("msec: " + (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - start));
        }
Пример #3
0
        private static long Qperft(ChessBoard chessBoard, int depth)
        {
            ThreadData.StartPly();
            MoveGenerator.GenerateMoves(ThreadData, chessBoard);
            MoveGenerator.GenerateAttacks(ThreadData, chessBoard);

            long counter = 0;

            if (depth == 1)
            {
                while (ThreadData.HasNext())
                {
                    if (chessBoard.IsLegal(ThreadData.Next()))
                    {
                        counter++;
                    }
                }

                ThreadData.EndPly();
                return(counter);
            }

            while (ThreadData.HasNext())
            {
                var move = ThreadData.Next();
                if (!chessBoard.IsLegal(move))
                {
                    continue;
                }

                chessBoard.DoMove(move);
                counter += Qperft(chessBoard, depth - 1);
                chessBoard.UndoMove(move);
            }

            ThreadData.EndPly();
            return(counter);
        }
Пример #4
0
        public static Dictionary <string, double> LoadFens(string fileName, bool containsResult, bool includingCheck)
        {
            Console.WriteLine("Loading " + fileName);

            var fens       = new Dictionary <string, double>();
            var checkCount = 0;
            var checkmate  = 0;
            var stalemate  = 0;

            try
            {
                foreach (var line in File.ReadLines(fileName))
                {
                    double score = 0;
                    string fenString;
                    if (containsResult)
                    {
                        var scoreString = GetScoreStringFromLine(line);
                        fenString = GetFenStringFromLine(line);
                        if (scoreString.Equals("\"1/2-1/2\";"))
                        {
                            score = 0.5;
                        }
                        else if (scoreString.Equals("\"1-0\";"))
                        {
                            score = 1;
                        }
                        else if (scoreString.Equals("\"0-1\";"))
                        {
                            score = 0;
                        }
                        else
                        {
                            throw new Exception("Unknown result: " + scoreString);
                        }
                    }
                    else
                    {
                        fenString = line;
                    }

                    ChessBoardUtil.SetFen(fenString, Cb);

                    if (Cb.CheckingPieces == 0)
                    {
                        ThreadData.StartPly();
                        MoveGenerator.GenerateAttacks(ThreadData, Cb);
                        MoveGenerator.GenerateMoves(ThreadData, Cb);
                        if (ThreadData.HasNext())
                        {
                            fens.Add(fenString, score);
                        }
                        else
                        {
                            stalemate++;
                        }

                        ThreadData.EndPly();
                    }
                    else
                    {
                        checkCount++;
                        if (!includingCheck)
                        {
                            continue;
                        }
                        ThreadData.StartPly();
                        MoveGenerator.GenerateAttacks(ThreadData, Cb);
                        MoveGenerator.GenerateMoves(ThreadData, Cb);
                        if (ThreadData.HasNext())
                        {
                            fens.Add(fenString, score);
                        }
                        else
                        {
                            checkmate++;
                        }

                        ThreadData.EndPly();
                    }

                    //line = br.readLine();
                }
            }
            catch (IOException e)
            {
                throw new Exception("", e);
            }

            Console.WriteLine("In check : " + checkCount);
            Console.WriteLine("Checkmate : " + checkmate);
            Console.WriteLine("Stalemate : " + stalemate);
            return(fens);
        }