Esempio n. 1
0
        public bool IsColorInCheck(int p_color)
        {
            int indexOfKing = BitBoard.GetIndexOfFirstBit(Pieces[p_color | BitBoard.PIECE_TYPE_KING]);

            return(IsSquareAttacked(1 - p_color, indexOfKing));
        }
Esempio n. 2
0
        public MoveList GetLegalMoves()
        {
            MoveList moveList = new MoveList();

            int   fromSquareIndex;
            int   toSquareIndex;
            ulong bbToSquares;

            ulong bbMyPieces        = Pieces[ColorToMove];
            ulong bbEnemyPieces     = Pieces[1 - ColorToMove];
            ulong bbNotMyPieces     = ~bbMyPieces;
            ulong bbAllPieces       = bbMyPieces | bbEnemyPieces;
            ulong bbEmptySquare     = ~bbAllPieces;
            int   myKingSquareIndex = BitBoard.GetIndexOfFirstBit(Pieces[ColorToMove | BitBoard.PIECE_TYPE_KING]);

            #region Generate King Moves

            fromSquareIndex = myKingSquareIndex;
            bbToSquares     = BitBoard.KingAttack[myKingSquareIndex] & bbNotMyPieces;
            while (bbToSquares != 0)
            {
                toSquareIndex = BitBoard.GetIndexOfFirstBitAndRemoveIt(ref bbToSquares);
                moveList.AddMove(fromSquareIndex, toSquareIndex);
            }

            if (CastleQueenside[ColorToMove] &&
                ((bbAllPieces & BitBoard.Square[myKingSquareIndex - 1]) == 0) &&
                ((bbAllPieces & BitBoard.Square[myKingSquareIndex - 2]) == 0) &&
                ((bbAllPieces & BitBoard.Square[myKingSquareIndex - 3]) == 0) &&
                !IsSquareAttacked(1 - ColorToMove, myKingSquareIndex) &&
                !IsSquareAttacked(1 - ColorToMove, myKingSquareIndex - 1) &&
                !IsSquareAttacked(1 - ColorToMove, myKingSquareIndex - 2))
            {
                moveList.AddMove(myKingSquareIndex, myKingSquareIndex - 2);
            }

            if (CastleKingside[ColorToMove] &&
                ((bbAllPieces & BitBoard.Square[myKingSquareIndex + 1]) == 0) &&
                ((bbAllPieces & BitBoard.Square[myKingSquareIndex + 2]) == 0) &&
                !IsSquareAttacked(1 - ColorToMove, myKingSquareIndex) &&
                !IsSquareAttacked(1 - ColorToMove, myKingSquareIndex + 1) &&
                !IsSquareAttacked(1 - ColorToMove, myKingSquareIndex + 2))
            {
                moveList.AddMove(myKingSquareIndex, myKingSquareIndex + 2);
            }

            #endregion Generate King Moves

            #region Generate Rook & Queen Moves

            ulong bbRooksAndQuens = Pieces[ColorToMove | BitBoard.PIECE_TYPE_ROOK] | Pieces[ColorToMove | BitBoard.PIECE_TYPE_QUEEN];
            while (bbRooksAndQuens != 0)
            {
                fromSquareIndex = BitBoard.GetIndexOfFirstBitAndRemoveIt(ref bbRooksAndQuens);
                bbToSquares     = BitBoard.GetRookAttacks(fromSquareIndex, bbAllPieces) & bbNotMyPieces;
                while (bbToSquares != 0)
                {
                    toSquareIndex = BitBoard.GetIndexOfFirstBitAndRemoveIt(ref bbToSquares);
                    moveList.AddMove(fromSquareIndex, toSquareIndex);
                }
            }

            #endregion Generate Rook & Queen Moves

            #region Generate Bishop & Queen Moves

            ulong bbBishopsAndQuens = Pieces[ColorToMove | BitBoard.PIECE_TYPE_BISHOP] | Pieces[ColorToMove | BitBoard.PIECE_TYPE_QUEEN];
            while (bbBishopsAndQuens != 0)
            {
                fromSquareIndex = BitBoard.GetIndexOfFirstBitAndRemoveIt(ref bbBishopsAndQuens);
                bbToSquares     = BitBoard.GetBishopAttacks(fromSquareIndex, bbAllPieces) & bbNotMyPieces;
                while (bbToSquares != 0)
                {
                    toSquareIndex = BitBoard.GetIndexOfFirstBitAndRemoveIt(ref bbToSquares);
                    moveList.AddMove(fromSquareIndex, toSquareIndex);
                }
            }

            #endregion Generate Bishop & Queen Moves

            #region Generate Knight Moves

            ulong knights = Pieces[ColorToMove | BitBoard.PIECE_TYPE_KNIGHT];

            while (knights != 0)
            {
                fromSquareIndex = BitBoard.GetIndexOfFirstBitAndRemoveIt(ref knights);
                bbToSquares     = BitBoard.KnightAttack[fromSquareIndex] & bbNotMyPieces;
                while (bbToSquares != 0)
                {
                    toSquareIndex = BitBoard.GetIndexOfFirstBitAndRemoveIt(ref bbToSquares);
                    moveList.AddMove(fromSquareIndex, toSquareIndex);
                }
            }

            #endregion Generate Knight Moves

            #region Generate Pawn Moves

            ulong pawns = Pieces[ColorToMove | BitBoard.PIECE_TYPE_PAWN];

            if (ColorToMove == BitBoard.COLOR_WHITE)
            {
                #region White pawn

                bbToSquares = (pawns << 8) & bbEmptySquare;
                ulong toSquares2           = ((bbToSquares & BitBoard.Rank[2]) << 8) & bbEmptySquare;
                ulong toSquareForPromotion = bbToSquares & BitBoard.Rank[7];
                bbToSquares &= BitBoard.NotRank[7];
                while (bbToSquares != 0)
                {
                    toSquareIndex = BitBoard.GetIndexOfFirstBitAndRemoveIt(ref bbToSquares);
                    moveList.AddMove(toSquareIndex - 8, toSquareIndex);
                }
                while (toSquares2 != 0)
                {
                    toSquareIndex = BitBoard.GetIndexOfFirstBitAndRemoveIt(ref toSquares2);
                    moveList.AddMoveEnPassant(toSquareIndex - 16, toSquareIndex, toSquareIndex - 8);
                }
                while (toSquareForPromotion != 0)
                {
                    toSquareIndex = BitBoard.GetIndexOfFirstBitAndRemoveIt(ref toSquareForPromotion);
                    moveList.AddMovePromotion(toSquareIndex - 8, toSquareIndex, BitBoard.PIECE_TYPE_QUEEN);
                    moveList.AddMovePromotion(toSquareIndex - 8, toSquareIndex, BitBoard.PIECE_TYPE_ROOK);
                    moveList.AddMovePromotion(toSquareIndex - 8, toSquareIndex, BitBoard.PIECE_TYPE_BISHOP);
                    moveList.AddMovePromotion(toSquareIndex - 8, toSquareIndex, BitBoard.PIECE_TYPE_KNIGHT);
                }
                //attacks
                bbToSquares = (pawns << 7) & BitBoard.NotFile[7] & (bbEnemyPieces | EnPassant);
                while (bbToSquares != 0)
                {
                    toSquareIndex = BitBoard.GetIndexOfFirstBitAndRemoveIt(ref bbToSquares);
                    moveList.AddMove(toSquareIndex - 7, toSquareIndex);
                }
                bbToSquares = (pawns << 9) & BitBoard.NotFile[0] & (bbEnemyPieces | EnPassant);
                while (bbToSquares != 0)
                {
                    toSquareIndex = BitBoard.GetIndexOfFirstBitAndRemoveIt(ref bbToSquares);
                    moveList.AddMove(toSquareIndex - 9, toSquareIndex);
                }

                #endregion White pawn
            }
            else
            {
                #region Black pawn

                bbToSquares = (pawns >> 8) & bbEmptySquare;
                ulong toSquares2           = ((bbToSquares & BitBoard.Rank[5]) >> 8) & bbEmptySquare;
                ulong toSquareForPromotion = bbToSquares & BitBoard.Rank[0];
                bbToSquares &= BitBoard.NotRank[0];
                while (bbToSquares != 0)
                {
                    toSquareIndex = BitBoard.GetIndexOfFirstBitAndRemoveIt(ref bbToSquares);
                    moveList.AddMove(toSquareIndex + 8, toSquareIndex);
                }
                while (toSquares2 != 0)
                {
                    toSquareIndex = BitBoard.GetIndexOfFirstBitAndRemoveIt(ref toSquares2);
                    moveList.AddMoveEnPassant(toSquareIndex + 16, toSquareIndex, toSquareIndex + 8);
                }
                while (toSquareForPromotion != 0)
                {
                    toSquareIndex = BitBoard.GetIndexOfFirstBitAndRemoveIt(ref toSquareForPromotion);
                    moveList.AddMovePromotion(toSquareIndex + 8, toSquareIndex, BitBoard.PIECE_TYPE_QUEEN);
                    moveList.AddMovePromotion(toSquareIndex + 8, toSquareIndex, BitBoard.PIECE_TYPE_ROOK);
                    moveList.AddMovePromotion(toSquareIndex + 8, toSquareIndex, BitBoard.PIECE_TYPE_BISHOP);
                    moveList.AddMovePromotion(toSquareIndex + 8, toSquareIndex, BitBoard.PIECE_TYPE_KNIGHT);
                }
                //attacks
                bbToSquares = (pawns >> 7) & BitBoard.NotFile[0] & (bbEnemyPieces | EnPassant);
                while (bbToSquares != 0)
                {
                    toSquareIndex = BitBoard.GetIndexOfFirstBitAndRemoveIt(ref bbToSquares);
                    moveList.AddMove(toSquareIndex + 7, toSquareIndex);
                }
                bbToSquares = (pawns >> 9) & BitBoard.NotFile[7] & (bbEnemyPieces | EnPassant);
                while (bbToSquares != 0)
                {
                    toSquareIndex = BitBoard.GetIndexOfFirstBitAndRemoveIt(ref bbToSquares);
                    moveList.AddMove(toSquareIndex + 9, toSquareIndex);
                }

                #endregion Black pawn
            }

            #endregion Generate Pawn Moves

            return(moveList);
        }