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; }
public static long[] GetKey(Board board) { long[] key = new long[] { 0, 0 }; long square = BitboardUtils.H1; byte index = 0; int color = 0; while (square != 0) { color = (square & board.whites) != 0 ? 0 : 1; key[color] ^= GetKeyPieceIndex(index, board.GetPieceAt(square)); square <<= 1; index++; } if (board.GetWhiteKingsideCastling()) { key[0] ^= whiteKingSideCastling; } if (board.GetWhiteQueensideCastling()) { key[0] ^= whiteQueenSideCastling; } if (board.GetBlackKingsideCastling()) { key[1] ^= blackKingSideCastling; } if (board.GetBlackQueensideCastling()) { key[1] ^= blackQueenSideCastling; } // passant flags only when pawn can capture long passant = board.GetPassantSquare(); if ((passant != 0) && (((!board.GetTurn() && (((passant << 9) | (passant << 7)) & board.blacks & board.pawns) != 0)) || ((board.GetTurn() && ((((long)(((ulong)passant ) >> 9)) | ((long)(((ulong)passant) >> 7))) & board.whites & board.pawns) != 0)) )) { color = board.GetTurn() ? 0 : 1; // TODO test key[1 - color] ^= passantColumn[BitboardUtils.GetColumn(passant)]; } if (board.GetTurn()) { key[0] ^= whiteMove; } return key; }