Example #1
0
        public static int GetLoudMoves(BoardState boardState, Span <Move> moves, int offset, ulong evasionMask)
        {
            var color      = boardState.ColorToMove;
            var enemyColor = ColorOperations.Invert(color);
            var rooks      = boardState.Pieces[color][Piece.Rook];

            while (rooks != 0)
            {
                var piece = BitOperations.GetLsb(rooks);
                rooks = BitOperations.PopLsb(rooks);

                var from           = BitOperations.BitScan(piece);
                var availableMoves = RookMovesGenerator.GetMoves(boardState.OccupancySummary, from) & boardState.Occupancy[enemyColor];
                availableMoves &= evasionMask;

                while (availableMoves != 0)
                {
                    var field      = BitOperations.GetLsb(availableMoves);
                    var fieldIndex = BitOperations.BitScan(field);
                    availableMoves = BitOperations.PopLsb(availableMoves);

                    moves[offset++] = new Move(from, fieldIndex, MoveFlags.Capture);
                }
            }

            return(offset);
        }
Example #2
0
        public static int GetMobility(BoardState boardState, int color, ref ulong fieldsAttackedByColor)
        {
            var centerMobility         = 0;
            var extendedCenterMobility = 0;
            var outsideMobility        = 0;

            var rooks = boardState.Pieces[color][Piece.Rook];

            while (rooks != 0)
            {
                var piece = BitOperations.GetLsb(rooks);
                rooks = BitOperations.PopLsb(rooks);

                var from           = BitOperations.BitScan(piece);
                var availableMoves = RookMovesGenerator.GetMoves(boardState.OccupancySummary, from);

                centerMobility         += (int)BitOperations.Count(availableMoves & EvaluationConstants.Center);
                extendedCenterMobility += (int)BitOperations.Count(availableMoves & EvaluationConstants.ExtendedCenter);
                outsideMobility        += (int)BitOperations.Count(availableMoves & EvaluationConstants.Outside);

                fieldsAttackedByColor |= availableMoves;
            }

            return(EvaluationConstants.CenterMobilityModifier * centerMobility +
                   EvaluationConstants.ExtendedCenterMobilityModifier * extendedCenterMobility +
                   EvaluationConstants.OutsideMobilityModifier * outsideMobility);
        }
Example #3
0
        public byte GetAttackingPiecesWithColor(int color, int fieldIndex)
        {
            byte result = 0;

            var jumpAttacks           = KnightMovesGenerator.GetMoves(fieldIndex);
            var attackingKnights      = jumpAttacks & Pieces[color][Piece.Knight];
            var attackingKnightsCount = BitOperations.Count(attackingKnights);

            if (attackingKnightsCount != 0)
            {
                result |= (byte)((attackingKnightsCount == 1 ? 1 : 3) << SeePiece.Knight1);
            }

            var diagonalAttacks  = BishopMovesGenerator.GetMoves(OccupancySummary, fieldIndex) & Occupancy[color];
            var attackingBishops = diagonalAttacks & Pieces[color][Piece.Bishop];

            if (attackingBishops != 0)
            {
                result |= 1 << SeePiece.Bishop;
            }

            var occupancyWithoutFileRankPieces = OccupancySummary & ~Pieces[color][Piece.Rook] & ~Pieces[color][Piece.Queen];
            var fileRankAttacks     = RookMovesGenerator.GetMoves(occupancyWithoutFileRankPieces, fieldIndex) & Occupancy[color];
            var attackingRooks      = fileRankAttacks & Pieces[color][Piece.Rook];
            var attackingRooksCount = BitOperations.Count(attackingRooks);

            if (attackingRooksCount != 0)
            {
                result |= (byte)((attackingRooksCount == 1 ? 1 : 3) << SeePiece.Rook1);
            }

            var attackingQueens = (fileRankAttacks | diagonalAttacks) & Pieces[color][Piece.Queen];

            if (attackingQueens != 0)
            {
                result |= 1 << SeePiece.Queen;
            }

            var boxAttacks     = KingMovesGenerator.GetMoves(fieldIndex);
            var attackingKings = boxAttacks & Pieces[color][Piece.King];

            if (attackingKings != 0)
            {
                result |= 1 << SeePiece.King;
            }

            var field          = 1ul << fieldIndex;
            var potentialPawns = boxAttacks & Pieces[color][Piece.Pawn];
            var attackingPawns = color == Color.White ?
                                 field & ((potentialPawns << 7) | (potentialPawns << 9)) :
                                 field & ((potentialPawns >> 7) | (potentialPawns >> 9));

            if (attackingPawns != 0)
            {
                result |= 1 << SeePiece.Pawn;
            }

            return(result);
        }
        /// <summary>
        /// Calculates available moves.
        /// </summary>
        /// <param name="generatorParameters">The generator parameters.</param>
        private void CalculateAvailableMoves(GeneratorParameters generatorParameters)
        {
            PawnMovesGenerator.Generate(generatorParameters);
            KnightMovesGenerator.Generate(generatorParameters);
            KingMovesGenerator.Generate(generatorParameters);

            RookMovesGenerator.Generate(PieceType.Rook, generatorParameters);
            BishopMovesGenerator.Generate(PieceType.Bishop, generatorParameters);

            RookMovesGenerator.Generate(PieceType.Queen, generatorParameters);
            BishopMovesGenerator.Generate(PieceType.Queen, generatorParameters);
        }
Example #5
0
        public bool IsFieldAttacked(int color, int fieldIndex)
        {
            var enemyColor = ColorOperations.Invert(color);

            var fileRankAttacks = RookMovesGenerator.GetMoves(OccupancySummary, fieldIndex) & Occupancy[enemyColor];
            var attackingRooks  = fileRankAttacks & (Pieces[enemyColor][Piece.Rook] | Pieces[enemyColor][Piece.Queen]);

            if (attackingRooks != 0)
            {
                return(true);
            }

            var diagonalAttacks  = BishopMovesGenerator.GetMoves(OccupancySummary, fieldIndex) & Occupancy[enemyColor];
            var attackingBishops = diagonalAttacks & (Pieces[enemyColor][Piece.Bishop] | Pieces[enemyColor][Piece.Queen]);

            if (attackingBishops != 0)
            {
                return(true);
            }

            var jumpAttacks      = KnightMovesGenerator.GetMoves(fieldIndex);
            var attackingKnights = jumpAttacks & Pieces[enemyColor][Piece.Knight];

            if (attackingKnights != 0)
            {
                return(true);
            }

            var boxAttacks     = KingMovesGenerator.GetMoves(fieldIndex);
            var attackingKings = boxAttacks & Pieces[enemyColor][Piece.King];

            if (attackingKings != 0)
            {
                return(true);
            }

            var field          = 1ul << fieldIndex;
            var potentialPawns = boxAttacks & Pieces[enemyColor][Piece.Pawn];
            var attackingPawns = color == Color.White ?
                                 field & ((potentialPawns >> 7) | (potentialPawns >> 9)) :
                                 field & ((potentialPawns << 7) | (potentialPawns << 9));

            if (attackingPawns != 0)
            {
                return(true);
            }

            return(false);
        }
Example #6
0
        public static bool IsMoveLegal(BoardState boardState, Move move)
        {
            var enemyColor     = ColorOperations.Invert(boardState.ColorToMove);
            var availableMoves = RookMovesGenerator.GetMoves(boardState.OccupancySummary, move.From);
            var toField        = 1ul << move.To;

            if (move.IsSinglePush() && (availableMoves & toField) != 0 && (boardState.OccupancySummary & toField) == 0)
            {
                return(true);
            }

            if (move.IsCapture() && (availableMoves & toField) != 0 && (boardState.Occupancy[enemyColor] & toField) != 0)
            {
                return(true);
            }

            return(false);
        }