Пример #1
0
 public virtual int GenerateMoves(Board board, int[] moves, int mIndex)
 {
     this.moves = moves;
     bbAttacks = BitboardAttacks.GetInstance();
     moveIndex = mIndex;
     all = board.GetAll();
     // only for clearity
     mines = board.GetMines();
     others = board.GetOthers();
     byte index = 0;
     long square = unchecked((long)(0x1L));
     while (square != 0)
     {
         if (board.GetTurn() == ((square & board.whites) != 0))
         {
             if ((square & board.rooks) != 0)
             {
                 // Rook
                 GenerateMovesFromAttacks(Move.ROOK, index, bbAttacks.GetRookAttacks(index, all));
             }
             else
             {
                 if ((square & board.bishops) != 0)
                 {
                     // Bishop
                     GenerateMovesFromAttacks(Move.BISHOP, index, bbAttacks.GetBishopAttacks(index, all
                         ));
                 }
                 else
                 {
                     if ((square & board.queens) != 0)
                     {
                         // Queen
                         GenerateMovesFromAttacks(Move.QUEEN, index, bbAttacks.GetRookAttacks(index, all));
                         GenerateMovesFromAttacks(Move.QUEEN, index, bbAttacks.GetBishopAttacks(index, all
                             ));
                     }
                     else
                     {
                         if ((square & board.kings) != 0)
                         {
                             // King
                             GenerateMovesFromAttacks(Move.KING, index, bbAttacks.king[index]);
                         }
                         else
                         {
                             if ((square & board.knights) != 0)
                             {
                                 // Knight
                                 GenerateMovesFromAttacks(Move.KNIGHT, index, bbAttacks.knight[index]);
                             }
                             else
                             {
                                 if ((square & board.pawns) != 0)
                                 {
                                     // Pawns
                                     if ((square & board.whites) != 0)
                                     {
                                         if (((square << 8) & all) == 0)
                                         {
                                             AddMoves(Move.PAWN, index, index + 8, (square << 8), false, true, 0);
                                             // Two squares if it is in he first row
                                             if (((square & BitboardUtils.b2_d) != 0) && (((square << 16) & all) == 0))
                                             {
                                                 AddMoves(Move.PAWN, index, index + 16, (square << 16), false, false, 0);
                                             }
                                         }
                                         GeneratePawnCapturesFromAttacks(index, bbAttacks.pawnUpwards[index], board.GetPassantSquare
                                             ());
                                     }
                                     else
                                     {
                                         if ((((long)(((ulong)square) >> 8)) & all) == 0)
                                         {
                                             AddMoves(Move.PAWN, index, index - 8, ((long)(((ulong)square) >> 8)), false, true
                                                 , 0);
                                             // Two squares if it is in he first row
                                             if (((square & BitboardUtils.b2_u) != 0) && ((((long)(((ulong)square) >> 16)) & all
                                                 ) == 0))
                                             {
                                                 AddMoves(Move.PAWN, index, index - 16, ((long)(((ulong)square) >> 16)), false, false
                                                     , 0);
                                             }
                                         }
                                         GeneratePawnCapturesFromAttacks(index, bbAttacks.pawnDownwards[index], board.GetPassantSquare
                                             ());
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         square <<= 1;
         index++;
     }
     square = board.kings & mines;
     // my king
     byte myKingIndex = unchecked((byte)(-1));
     // Castling: disabled when in check or squares attacked
     if ((((all & (board.GetTurn() ? unchecked((long)(0x06L)) : unchecked((long)(0x0600000000000000L
         )))) == 0 && (board.GetTurn() ? board.GetWhiteKingsideCastling() : board.GetBlackKingsideCastling
         ()))))
     {
         myKingIndex = BitboardUtils.Square2Index(square);
         if (!board.GetCheck() && !bbAttacks.IsIndexAttacked(board, unchecked((byte)(myKingIndex
              - 1)), board.GetTurn()) && !bbAttacks.IsIndexAttacked(board, unchecked((byte)(myKingIndex
              - 2)), board.GetTurn()))
         {
             AddMoves(Move.KING, myKingIndex, myKingIndex - 2, 0, false, false, Move.TYPE_KINGSIDE_CASTLING
                 );
         }
     }
     if ((((all & (board.GetTurn() ? unchecked((long)(0x70L)) : unchecked((long)(0x7000000000000000L
         )))) == 0 && (board.GetTurn() ? board.GetWhiteQueensideCastling() : board.GetBlackQueensideCastling
         ()))))
     {
         if (myKingIndex == -1)
         {
             myKingIndex = BitboardUtils.Square2Index(square);
         }
         if (!board.GetCheck() && !bbAttacks.IsIndexAttacked(board, unchecked((byte)(myKingIndex
              + 1)), board.GetTurn()) && !bbAttacks.IsIndexAttacked(board, unchecked((byte)(myKingIndex
              + 2)), board.GetTurn()))
         {
             AddMoves(Move.KING, myKingIndex, myKingIndex + 2, 0, false, false, Move.TYPE_QUEENSIDE_CASTLING
                 );
         }
     }
     return moveIndex;
 }