Esempio n. 1
0
        public void EvaluateKnight(ref Position position, ref int square, ref int color, ref int score)
        {
            Bitboard destinations = Bitboards.KnightBitboards[square];
            Bitboard myPieces     = position.GetPieceBitboard(color, Position.ALL_PIECES);

            score += materialValues[Position.KNIGHT];

            while (destinations != 0)
            {
                int attackSquare = destinations.BitScanForward();
                Bitboards.PopLsb(ref destinations);
                int attackedPiece = position.GetPieceSquares()[attackSquare];
                if (attackedPiece == Position.EMPTY)
                {
                    score += 1; // Base mobility bonus
                }
                // Bonuses for attacking opponent pieces
                else if ((Bitboards.SquareBitboards[attackSquare] & myPieces) == 0)
                {
                    score += 2;
                }
                // Bonuses for defending own pieces
                else
                {
                    score += attackBonus[attackedPiece];
                }
            }
        }
Esempio n. 2
0
        public int EvaluateSlidingPiece(Position position, int color, int pieceType, int materialValue, int mobilityMultiplier, ref Bitboard destinationBitboard)
        {
            Bitboard pieces = position.GetPieceBitboard(color, pieceType);
            int      score  = 0;

            while (pieces != 0)
            {
                score += materialValue;
                int square = pieces.BitScanForward();
                Bitboards.PopLsb(ref pieces);

                Bitboard attacks = 0;
                if (pieceType == Position.ROOK || pieceType == Position.QUEEN)
                {
                    attacks = Bitboards.GetRookMoveBitboard(square, position.AllPiecesBitboard);
                }
                if (pieceType == Position.BISHOP || pieceType == Position.QUEEN)
                {
                    attacks |= Bitboards.GetBishopMoveBitboard(square, position.AllPiecesBitboard);
                }
                attacks &= destinationBitboard;
                score   += Bitboards.CountBits(attacks) * mobilityMultiplier;
            }
            return(score);
        }
Esempio n. 3
0
        public int EvaluatePawns(ref Position position, int color)
        {
            int score = 0;

            Bitboard myPawns  = position.GetPieceBitboard(color, Position.PAWN);
            Bitboard myPieces = position.GetPieceBitboard(color, Position.ALL_PIECES);

            Bitboard opponentPawns =
                position.GetPieceBitboard(color == Position.WHITE ? Position.BLACK : Position.WHITE, Position.PAWN);

            Bitboard allPawns = myPawns | opponentPawns;

            Bitboard pawns = myPawns;

            while (pawns != 0)
            {
                score += materialValues[Position.PAWN];

                int currSquare = pawns.BitScanForward();
                Bitboards.PopLsb(ref pawns);

                // Add bonus based on file of pawn (to promote taking towards the center)
                score += pawnFileBonus[Bitboards.GetColumn(currSquare)];

                // Analyze stacked pawns
                Bitboard filePawns = Bitboards.ColumnBitboards[currSquare] & myPawns;

                if ((filePawns & (filePawns - 1)) != 0) // More than 1 pawn in this rank
                {
                    score -= stackedPawnPenalty;
                }

                // Analyze isolated pawns
                if ((Bitboards.IsolatedPawnBitboards[currSquare] & myPawns) == 0)
                {
                    score -= isolatedPawnPenalty;
                }

                // Analyze passed pawns
                if ((Bitboards.PassedPawnBitboards[color, currSquare] & allPawns) == 0)
                {
                    score += passedPawnBonus[color, Bitboards.GetRow(currSquare)];
                }

                Bitboard pawnAttacks = Bitboards.PawnBitboards[color, currSquare];
                while (pawnAttacks != 0)
                {
                    int attackSquare = pawnAttacks.BitScanForward();
                    Bitboards.PopLsb(ref pawnAttacks);
                    int attackedPiece = position.GetPieceSquares()[attackSquare];
                    if (attackedPiece == Position.EMPTY || (myPieces & Bitboards.SquareBitboards[attackSquare]) != 0)
                    {
                        continue;
                    }
                    score += attackBonus[attackedPiece];
                }
            }
            return(score);
        }
Esempio n. 4
0
        public int EvaluateKnights(ref Position position, int color)
        {
            Bitboard knights = position.GetPieceBitboard(color, Position.KNIGHT);
            int      score   = 0;

            while (knights != 0)
            {
                int knightSquare = knights.BitScanForward();
                Bitboards.PopLsb(ref knights);
                EvaluateKnight(ref position, ref knightSquare, ref color, ref score);
            }
            return(score);
        }
Esempio n. 5
0
        public int EvaluateSliders(ref Position position, int color, int pieceType)
        {
            Bitboard sliders = position.GetPieceBitboard(color, pieceType);
            int      score   = 0;

            while (sliders != 0)
            {
                int sliderSquare = sliders.BitScanForward();
                Bitboards.PopLsb(ref sliders);
                EvaluateSlider(ref position, ref color, ref pieceType, ref sliderSquare, ref score);
            }
            return(score);
        }
Esempio n. 6
0
        public int EvaluateKnights(Position position, int color, int mobilityMultiplier, ref Bitboard destinationBitboard)
        {
            Bitboard pieces = position.GetPieceBitboard(color, Position.KNIGHT);
            int      score  = 0;

            while (pieces != 0)
            {
                score += 300; // Material value
                int square = pieces.BitScanForward();
                Bitboards.PopLsb(ref pieces);
                score += mobilityMultiplier * (Bitboards.CountBits(destinationBitboard & Bitboards.KnightBitboards[square]));
            }
            return(score);
        }
 public void SetAllSquares()
 {
     view.ResetSquares();
     for (int color = 0; color < 2; color++)
     {
         for (int piece = 0; piece < 6; piece++)
         {
             Bitboard b = model.GetPieceBitboard(color, piece);
             while (b != 0)
             {
                 int square = b.BitScanForward();
                 Bitboards.PopLsb(ref b);
                 view.SetPiece(square, color, piece);
             }
         }
     }
 }
Esempio n. 8
0
        public int EvaluatePawnStructure(Position position, int color)
        {
            int score = 0;

            Bitboard myPawns = position.GetPieceBitboard(color, Position.PAWN);

            Bitboard opponentPawns =
                position.GetPieceBitboard(color == Position.WHITE ? Position.BLACK : Position.WHITE, Position.PAWN);

            Bitboard allPawns = myPawns | opponentPawns;

            Bitboard pawns = myPawns;

            while (pawns != 0)
            {
                int currSquare = pawns.BitScanForward();
                Bitboards.PopLsb(ref pawns);

                // Add bonus based on file of pawn (to promote taking towards the center)
                score += pawnFileBonus[Bitboards.GetColumn(currSquare)];

                // Analyze stacked pawns
                Bitboard filePawns = Bitboards.ColumnBitboards[currSquare] & myPawns;

                if ((filePawns & (filePawns - 1)) != 0) // More than 1 pawn in this rank
                {
                    score -= stackedPawnPenalty;
                }

                // Analyze isolated pawns
                if ((Bitboards.IsolatedPawnBitboards[currSquare] & myPawns) == 0)
                {
                    score -= isolatedPawnPenalty;
                }

                // Analyze passed pawns
                if ((Bitboards.PassedPawnBitboards[color, currSquare] & allPawns) == 0)
                {
                    score += passedPawnBonus[color, Bitboards.GetRow(currSquare)];
                }
            }
            return(score);
        }