示例#1
0
        /// <summary>
        /// Adds a piece to the board, and updates the hash keys if needed
        /// </summary>
        /// <param name="square">The square for the piece</param>
        /// <param name="side">For which side the piece is to be added</param>
        /// <param name="pieceType">The type of piece to add</param>
        private void AddPiece(Square square, Player side, EPieceType pieceType)
        {
            Piece piece = (int)pieceType | (side << 3);

            Position.AddPiece(piece, square);

            State.Key ^= piece.GetZobristPst(square);

            if (pieceType == EPieceType.Pawn)
            {
                State.PawnStructureKey ^= piece.GetZobristPst(square);
            }

            State.Material.Add(piece);
        }
示例#2
0
        /// <summary>
        /// Apply a FEN string board setup to the board structure.
        /// </summary>
        /// <param name="fenString">The string to set</param>
        /// <param name="validate">If true, the fen string is validated, otherwise not</param>
        /// <returns>
        /// 0 = all ok.
        /// -1 = Error in piece file layout parsing
        /// -2 = Error in piece rank layout parsing
        /// -3 = Unknown piece detected
        /// -4 = Error while parsing moving side
        /// -5 = Error while parsing castleling
        /// -6 = Error while parsing en-passant square
        /// -9 = FEN length exceeding maximum
        /// </returns>
        public FenError SetFen(FenData fen, bool validate = false)
        {
            if (validate)
            {
                Fen.Fen.Validate(fen.Fen.ToString());
            }

            // correctly clear all pieces and invoke possible notification(s)
            var bb = Occupied;

            while (bb)
            {
                var square = bb.Lsb();
                Position.RemovePiece(square, Position.BoardLayout[square.AsInt()]);
                BitBoards.ResetLsb(ref bb);
            }

            var chunk = fen.Chunk();

            if (chunk.IsEmpty)
            {
                return(new FenError());
            }

            var    f = 1; // file (column)
            var    r = 8; // rank (row)
            Player player;

            foreach (var c in chunk)
            {
                if (char.IsDigit(c))
                {
                    f += c - '0';
                    if (f > 9)
                    {
                        return(new FenError(-1, fen.Index));
                    }
                }
                else if (c == '/')
                {
                    if (f != 9)
                    {
                        return(new FenError(-2, fen.Index));
                    }

                    r--;
                    f = 1;
                }
                else
                {
                    var pieceIndex = PieceExtensions.PieceChars.IndexOf(c);

                    if (pieceIndex == -1)
                    {
                        return(new FenError(-3, fen.Index));
                    }

                    player = char.IsLower(PieceExtensions.PieceChars[pieceIndex]);

                    var square = new Square(r - 1, f - 1);

                    AddPiece(square, player, (EPieceType)pieceIndex);

                    f++;
                }
            }

            // player
            chunk = fen.Chunk();

            if (chunk.IsEmpty || chunk.Length != 1)
            {
                return(new FenError(-3, fen.Index));
            }

            player = (chunk[0] != 'w').ToInt();

            // castleling
            chunk = fen.Chunk();

            if (!SetupCastleling(chunk))
            {
                return(new FenError(-5, fen.Index));
            }

            // en-passant
            chunk = fen.Chunk();

            if (chunk.Length == 1 || chunk[0] == '-' || !chunk[0].InBetween('a', 'h'))
            {
                State.EnPassantSquare = ESquare.none;
            }
            else
            {
                State.EnPassantSquare = chunk[1].InBetween('3', '6') ? ESquare.none : new Square(chunk[1] - '1', chunk[0] - 'a').Value;
            }

            // move number
            chunk = fen.Chunk();

            var moveNum     = 0;
            var halfMoveNum = 0;

            if (!chunk.IsEmpty)
            {
                chunk.ToIntegral(out halfMoveNum);

                // half move number
                chunk = fen.Chunk();

                chunk.ToIntegral(out moveNum);

                if (moveNum > 0)
                {
                    moveNum--;
                }
            }

            PositionIndex = PositionStart = moveNum;

            Position.State = _stateList[PositionIndex];

            State.ReversibleHalfMoveCount = halfMoveNum;

            State.SideToMove = player;

            if (player.IsBlack())
            {
                State.Key ^= Zobrist.GetZobristSide();
                State.PawnStructureKey ^= Zobrist.GetZobristSide();
            }

            State.Key ^= State.CastlelingRights.GetZobristCastleling();

            if (State.EnPassantSquare != ESquare.none)
            {
                State.Key ^= State.EnPassantSquare.File().GetZobristEnPessant();
            }

            Position.InCheck = Position.IsAttacked(Position.GetPieceSquare(EPieceType.King, player), ~player);

            return(0);
        }
示例#3
0
        /// <summary>
        /// Apply a FEN string board setup to the board structure.
        /// *EXCEPTION FREE FUNCTION*
        /// </summary>
        /// <param name="fenString">The string to set</param>
        /// <param name="validate">If true, the fen string is validated, otherwise not</param>
        /// <returns>
        /// 0 = all ok.
        /// -1 = Error in piece file layout parsing
        /// -2 = Error in piece rank layout parsing
        /// -3 = Unknown piece detected
        /// -4 = Error while parsing moving side
        /// -5 = Error while parsing castleling
        /// -6 = Error while parsing en-passant square
        /// -9 = FEN length exceeding maximum
        /// </returns>
        public FenError SetFen(string fenString, bool validate = false)
        {
            // TODO : Replace with stream at some point

            // basic validation, catches format errors
            if (validate)
            {
                Fen.Fen.Validate(fenString);
            }

            foreach (var square in Occupied)
            {
                Position.RemovePiece(square, Position.BoardLayout[square.AsInt()]);
            }

            //for (var i = 0; i <= PositionIndex; i++)
            //    _stateList[i].Clear();

            Position.Clear();

            var fen = new FenData(fenString);

            Player player;
            var    c = fen.GetAdvance;

            var f = 1; // file (column)
            var r = 8; // rank (row)

            // map pieces to data structure
            while (c != 0 && !(f == 9 && r == 1))
            {
                if (char.IsDigit(c))
                {
                    f += c - '0';
                    if (f > 9)
                    {
                        return(new FenError(-1, fen.Index));
                    }

                    c = fen.GetAdvance;
                    continue;
                }

                if (c == '/')
                {
                    if (f != 9)
                    {
                        return(new FenError(-2, fen.Index));
                    }

                    r--;
                    f = 1;
                    c = fen.GetAdvance;
                    continue;
                }

                var pieceIndex = PieceExtensions.PieceChars.IndexOf(c);

                if (pieceIndex == -1)
                {
                    return(new FenError(-3, fen.Index));
                }

                player = char.IsLower(PieceExtensions.PieceChars[pieceIndex]);

                var square = new Square(r - 1, f - 1);

                AddPiece(square, player, (EPieceType)pieceIndex);

                c = fen.GetAdvance;

                f++;
            }

            if (!Fen.Fen.IsDelimiter(c))
            {
                return(new FenError(-4, fen.Index));
            }

            c = fen.GetAdvance;

            player = c == 'w' ? 0 : 1;

            if (player == -1)
            {
                return(new FenError(-4, fen.Index));
            }

            if (!Fen.Fen.IsDelimiter(fen.GetAdvance))
            {
                return(new FenError(-5, fen.Index));
            }

            if (!SetupCastleling(fen))
            {
                return(new FenError(-5, fen.Index));
            }

            if (!Fen.Fen.IsDelimiter(fen.GetAdvance))
            {
                return(new FenError(-6, fen.Index));
            }

            State.EnPassantSquare = fen.GetEpSquare();

            // temporary.. the whole method should be using this, but this will do for now.

            var moveCounters = fen.Fen.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            var first  = moveCounters[moveCounters.Length - 2];
            var second = moveCounters[moveCounters.Length - 1];

            second.ToIntegral(out int number);

            if (number > 0)
            {
                number -= 1;
            }

            PositionIndex = PositionStart = number;

            first.ToIntegral(out number);

            State = Position.State = _stateList[PositionIndex];

            State.ReversibleHalfMoveCount = number;

            State.SideToMove = player;

            if (player.IsBlack())
            {
                var zobristSide = Zobrist.GetZobristSide();
                State.Key ^= zobristSide;
                State.PawnStructureKey ^= zobristSide;
            }

            State.Key ^= Zobrist.GetZobristCastleling(State.CastlelingRights);

            if (State.EnPassantSquare != ESquare.none)
            {
                State.Key ^= Zobrist.GetZobristEnPessant(State.EnPassantSquare.File().AsInt());
            }

            Position.InCheck = Position.IsAttacked(Position.GetPieceSquare(EPieceType.King, State.SideToMove), ~State.SideToMove);

            //State.GenerateMoves(true);

            return(0);
        }