Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PlayervsAIMode"/> class.
        /// </summary>
        /// <param name="consoleManager">The console manager.</param>
        /// <param name="commandsManager">The commands manager.</param>
        public PlayervsAIMode(ConsoleManager consoleManager, CommandsManager commandsManager) : base(consoleManager, commandsManager)
        {
            _ai           = new AICore();
            _history      = new List <Move>();
            _openingBook  = new OpeningBookProvider();
            _currentColor = Color.White;
            _done         = false;

            CalculateBitboard(new DefaultFriendlyBoard(), false);

            VisualBoard.OnFieldSelection         += Board_OnFieldSelection;
            VisualBoard.OnPieceMove              += Board_OnPieceMove;
            PromotionWindow.OnPromotionSelection += PromotionWindow_OnPromotionSelection;

            SetCommandHandlers();
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GameSession"/> class.
        /// </summary>
        /// <param name="helperThreadsCount">The helper threads count.</param>
        public GameSession(int helperThreadsCount)
        {
            _helperThreadsCount = helperThreadsCount;

            _aiCore = new AICore();
            _aiCore.OnThinkingOutput += AICore_OnThinkingOutput;

            Bitboard = new Bitboard(new DefaultFriendlyBoard());
            _preferredTimeCalculator = new PreferredTimeCalculator(50);

            _remainingTime = new[]
            {
                999999999,
                999999999
            };

            _history = new List <Move>();

            _openingBook = new OpeningBookProvider();
        }
        /// <summary>
        /// Runs AI game.
        /// </summary>
        /// <param name="command">The AI command.</param>
        private void RunAIGame(Command command)
        {
            var preferredTime    = command.GetArgument <float>(0);
            var helperTasksCount = command.GetArgument <int>(1);

            var whiteAI      = new AICore();
            var blackAI      = new AICore();
            var currentColor = Color.White;

            var history     = new List <Move>();
            var openingBook = new OpeningBookProvider();

            CalculateBitboard(new DefaultFriendlyBoard(), false);

            Task.Run(() =>
            {
                while (true)
                {
                    var currentAI  = currentColor == Color.White ? whiteAI : blackAI;
                    var enemyColor = ColorOperations.Invert(currentColor);

                    Move moveToApply;
                    var openingBookMove = openingBook.GetMoveFromBook(history);

                    if (openingBookMove != null)
                    {
                        moveToApply = Bitboard.Moves.First(p =>
                                                           p.From == openingBookMove.From && p.To == openingBookMove.To);
                    }
                    else
                    {
                        var aiResult = currentAI.Calculate(currentColor, Bitboard, preferredTime, helperTasksCount);
                        moveToApply  = aiResult.PVNodes[0];

                        ConsoleManager.WriteLine();
                        ConsoleManager.WriteLine($"$w{currentColor}:");
                        ConsoleManager.WriteLine($"$wBest move: $g{aiResult.PVNodes} $w(Score: $m{aiResult.Score}$w)");
                        ConsoleManager.WriteLine($"$wTotal nodes: $g{aiResult.Stats.TotalNodes} N $w(Depth: $m{aiResult.Depth}$w)");
                        ConsoleManager.WriteLine($"$wTime: $m{aiResult.Time} s");
                        ConsoleManager.WriteLine();
                    }

                    CalculateBitboard(moveToApply, false);

                    if (Bitboard.IsStalemate(enemyColor))
                    {
                        ConsoleManager.WriteLine("$gStalemate, wins!");
                        break;
                    }

                    if (Bitboard.IsThreefoldRepetition())
                    {
                        ConsoleManager.WriteLine("$gThreefold repetition!");
                        break;
                    }

                    if (Bitboard.IsMate(enemyColor))
                    {
                        ConsoleManager.WriteLine($"$gMate, {currentColor} wins!");
                        break;
                    }

                    currentColor = enemyColor;
                    history.Add(moveToApply);
                }
            });
        }