Ejemplo n.º 1
0
        /// <summary>
        /// Adds a piece specified in the command to the visual board and updates bitboard.
        /// </summary>
        /// <param name="command">The AddPiece command.</param>
        private void AddPiece(Command command)
        {
            var colorArgument = command.GetArgument <string>(0);
            var pieceArgument = command.GetArgument <string>(1);
            var fieldArgument = command.GetArgument <string>(2);

            var colorParseResult = Enum.TryParse(colorArgument, true, out Color color);

            if (!colorParseResult)
            {
                ConsoleManager.WriteLine($"$rInvalid color type ($R{color}$r)");
                return;
            }

            var pieceParseResult = Enum.TryParse(pieceArgument, true, out PieceType piece);

            if (!pieceParseResult)
            {
                ConsoleManager.WriteLine($"$rInvalid piece type ($R{piece}$r)");
                return;
            }

            var fieldPosition = PositionConverter.ToPosition(fieldArgument);

            VisualBoard.FriendlyBoard.SetPiece(new FriendlyPiece(fieldPosition, piece, color));
            CalculateBitboard(VisualBoard.FriendlyBoard, _quiescenceSearch);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Removes piece specified in the command from the visual board and updates bitboard.
        /// </summary>
        /// <param name="command">The RemovePiece command.</param>
        private void RemovePiece(Command command)
        {
            var fieldArgument = command.GetArgument <string>(0);

            var fieldPosition = PositionConverter.ToPosition(fieldArgument);

            VisualBoard.FriendlyBoard.RemovePiece(fieldPosition);
            CalculateBitboard(VisualBoard.FriendlyBoard, _quiescenceSearch);
        }
        /// <summary>
        /// Gets the source piece position by parsing the Style12 move.
        /// </summary>
        /// <param name="text">The text to parse.</param>
        /// <returns>The source piece position.</returns>
        private Position GetSourcePiecePosition(string text)
        {
            var positionIndex  = 2;
            var positionLength = 2;

            var fromSubstring = text.Substring(positionIndex, positionLength);

            return(PositionConverter.ToPosition(fromSubstring));
        }
        /// <summary>
        /// Gets the destination piece position by parsing the Style12 move.
        /// </summary>
        /// <param name="text">The text to parse.</param>
        /// <returns>The destination piece position.</returns>
        private Position GetDestinationPiecePosition(string text)
        {
            var positionIndex  = 5;
            var positionLength = 2;

            var toSubstring = text.Substring(positionIndex, positionLength);

            return(PositionConverter.ToPosition(toSubstring));
        }
        /// <summary>
        /// Parses normal (not promoting) move to CECP move object.
        /// </summary>
        /// <param name="moveText">The move in text format to parse.</param>
        /// <returns>The CECP move object.</returns>
        private CECPMove ParseNormalMove(string moveText)
        {
            var fromText = moveText.Substring(0, 2);
            var toText   = moveText.Substring(2, 2);

            var fromPosition = PositionConverter.ToPosition(fromText);
            var toPosition   = PositionConverter.ToPosition(toText);

            return(new CECPMove(fromPosition, toPosition));
        }
        /// <summary>
        /// Parses promoting move to CECP move object.
        /// </summary>
        /// <param name="moveText">The move in text format to parse.</param>
        /// <returns>The CECP move object.</returns>
        private CECPMove ParsePromotionMove(string moveText)
        {
            var fromText             = moveText.Substring(0, 2);
            var toText               = moveText.Substring(2, 2);
            var promotionPieceSymbol = Convert.ToChar(moveText.Substring(4, 1));

            var fromPosition       = PositionConverter.ToPosition(fromText);
            var toPosition         = PositionConverter.ToPosition(toText);
            var promotionPieceType = PieceConverter.GetPiece(promotionPieceSymbol);

            return(new CECPMove(fromPosition, toPosition, promotionPieceType));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Reads a <see cref="Position"/> object.
        /// </summary>
        /// <param name="reader">The file reader.</param>
        /// <exception cref="InvalidPositionValueException">Thrown when a loaded position cannot be converted properly.</exception>
        /// <returns>The loaded position (or null if there was a <see cref="PersistenceConstants.NullValue"/> in the file).</returns>
        private Position?ReadPosition(StreamReader reader)
        {
            var line = reader.ReadLine();

            if (line == null)
            {
                throw new InvalidPositionValueException();
            }

            var lineAfterTrim = line.Trim();

            if (lineAfterTrim == PersistenceConstants.NullValue)
            {
                return(null);
            }

            return(PositionConverter.ToPosition(lineAfterTrim));
        }
Ejemplo n.º 8
0
        private static List <OpeningBookMove> ReadOpening(string line)
        {
            var opening   = new List <OpeningBookMove>();
            var textMoves = line.Split(' ');

            foreach (var textMove in textMoves)
            {
                var fromText = textMove.Substring(0, 2);
                var toText   = textMove.Substring(2, 2);

                var from = PositionConverter.ToPosition(fromText);
                var to   = PositionConverter.ToPosition(toText);

                var openingMove = new OpeningBookMove(from, to);
                opening.Add(openingMove);
            }

            return(opening);
        }
Ejemplo n.º 9
0
        public async Task Chess(CommandContext ctx, [Description("`new` aby rozpocząć grę lub ruch (np. `e2e4`)")] string action = null)
        {
            if (action == null)
            {
                await ctx.RespondAsync("Użycie: `!szachy new` aby rozpocząć nową grę lub `!szachy e2e4` aby wykonać ruch.");
            }
            else if (action == "new")
            {
                _selectedPositions.Clear();
                _messageIds.Clear();

                CreateSession();

                var boardMessage = await ctx.RespondAsync(new DiscordMessageBuilder().WithContent("**Nowa gra utworzona:**").WithFile("board.png", GetBoardImage()));

                _messageIds.Add(boardMessage.Id);
            }
            else
            {
                _gameSession.UpdateRemainingTime(Color.White, 200);
                _gameSession.UpdateRemainingTime(Color.Black, 200);

                Position fromPosition, toPosition;
                try
                {
                    var from = action.Substring(0, 2);
                    var to   = action.Substring(2, 2);

                    fromPosition = PositionConverter.ToPosition(from);
                    toPosition   = PositionConverter.ToPosition(to);
                }
                catch (Exception e)
                {
                    await ctx.RespondAsync("Nieprawidłowy ruch");

                    return;
                }

                var moveValidationBitboard = new Bitboard(_gameSession.Bitboard);
                moveValidationBitboard.Calculate(GeneratorMode.CalculateAttacks | GeneratorMode.CalculateMoves, false);

                var move = moveValidationBitboard.Moves.FirstOrDefault(p => p.From == fromPosition && p.To == toPosition);
                if (move == null)
                {
                    await ctx.RespondAsync("Nieprawidłowy ruch");

                    return;
                }

                var validationBitboardAfterMove = moveValidationBitboard.Move(move);
                validationBitboardAfterMove.Calculate(false);

                if (validationBitboardAfterMove.IsCheck(Color.White))
                {
                    await ctx.RespondAsync("Invalid move");

                    return;
                }


                foreach (var msgIdToDelete in _messageIds)
                {
                    var messageToDelete = await ctx.Channel.GetMessageAsync(msgIdToDelete);

                    await ctx.Channel.DeleteMessageAsync(messageToDelete);
                }

                _messageIds.Clear();

                _gameSession.Move(Color.White, fromPosition, toPosition);

                _selectedPositions.Clear();
                _selectedPositions.Add(fromPosition);
                _selectedPositions.Add(toPosition);

                var playerBoardMsg = await ctx.RespondAsync(new DiscordMessageBuilder().WithContent("**Ruch gracza:**").WithFile("board.png", GetBoardImage()));

                _messageIds.Add(playerBoardMsg.Id);

                var thinkingMessage = await ctx.RespondAsync("Myślę...");

                var aiMove = _gameSession.MoveAI(Color.Black);

                _selectedPositions.Clear();
                _selectedPositions.Add(aiMove.PVNodes[0].From);
                _selectedPositions.Add(aiMove.PVNodes[0].To);

                await thinkingMessage.DeleteAsync();

                var aiBoardMsg = await ctx.RespondAsync(new DiscordMessageBuilder().WithContent("**Ruch AI:**").WithFile("board.png", GetBoardImage()));

                _messageIds.Add(aiBoardMsg.Id);
            }
        }