Пример #1
0
        // TODO: Add Validation
        public static Move FromLongAlgebraic(Position position, string move)
        {
            int[] squares = position.GetPieceSquares();

            int originSquare      = Bitboards.GetSquareFromName(move.Substring(0, 2));
            int destinationSquare = Bitboards.GetSquareFromName(move.Substring(2, 2));
            int capturePiece      = squares[destinationSquare];
            int movedPiece        = squares[originSquare];

            // Castle Moves
            if (movedPiece == Position.KING && Bitboards.SquareDistance[originSquare, destinationSquare] > 1)
            {
                int castleDirection = Bitboards.GetColumn(destinationSquare) == 6 ? Position.KING : Position.QUEEN;
                return(new Move(originSquare, destinationSquare, movedPiece, false, true, castleDirection));
            }
            // En Passent and Promotion
            if (movedPiece == Position.PAWN)
            {
                // En Passent
                if (Bitboards.GetColumn(originSquare) != Bitboards.GetColumn(destinationSquare) &&
                    capturePiece == Position.EMPTY)
                {
                    return(new Move(originSquare, destinationSquare, movedPiece, true, false));
                }
                else if (move.Length > 4)
                {
                    const string promotionMap   = "-qrbn";
                    int          promotionPiece = promotionMap.IndexOf(move[4]);
                    return(new Move(originSquare, destinationSquare, movedPiece, capturePiece, promotionPiece));
                }
            }
            // Everything else
            return(new Move(originSquare, destinationSquare, movedPiece, capturePiece));
        }
Пример #2
0
        public static int[] GenerateOffsets(Bitboard bitboard)
        {
            int[] result = new int[Bitboards.CountBits(bitboard)];
            int   cnt    = 0;

            while (bitboard != 0)
            {
                result[cnt++] = Bitboards.BitScanForward(bitboard);
                Bitboards.PopLsb(ref bitboard);
            }
            return(result);
        }
Пример #3
0
        public override string ToString()
        {
            string pieceStr = "KQRBNP";

            StringBuilder sb = new StringBuilder();

            sb.Append(Bitboards.GetNameFromSquare(OriginSquare()));
            sb.Append(Bitboards.GetNameFromSquare(DestinationSquare()));
            if (PromotionType() != Position.EMPTY)
            {
                sb.Append('=');
                sb.Append(pieceStr[PromotionType()]);
            }
            return(sb.ToString());
        }
Пример #4
0
        public static Bitboard[] GeneratePermutations(Bitboard occupancyBitboard)
        {
            int numBits          = Bitboards.CountBits(occupancyBitboard);
            int numPossibilities = (int)Math.Pow(2, numBits);

            int[] offsets = GenerateOffsets(occupancyBitboard);

            Bitboard[] result = new Bitboard[numPossibilities];

            for (int i = 0; i < numPossibilities; i++)
            {
                for (int bit = 0; bit < numBits; bit++)
                {
                    bool bitIsSet = (i & (1 << bit)) != 0;
                    if (bitIsSet)
                    {
                        result[i] |= Bitboards.SquareBitboards[offsets[bit]];
                    }
                }
            }

            return(result);
        }