Skip to content

Instantly share code, notes, and snippets.

@dizzi
Created January 23, 2011 15:06
Show Gist options
  • Save dizzi/792132 to your computer and use it in GitHub Desktop.
Save dizzi/792132 to your computer and use it in GitHub Desktop.
private int[] generateMoves(int status, int color) {
int[] moves = new int[45];
long whites = 0, blacks = 0;
blacks = Utils.getBlacks(status);
whites = Utils.getWhites(status);
long tmp = 0;
int movePointer = 0;
if (color == Main.white) {
long pawnAttack = 0;
long whiteKingAttack = 0;
long whiteRookAttack = 0;
// generate white king, rook and pawn attack maps
// king - moves + chess check
whiteKingAttack = MapGenerator.generateKingMap(Utils.getWhiteKing(status));
whiteKingAttack = whiteKingAttack & ~(whites);
long whiteRawPawnAttack = 0;
if (Utils.isQueen(status)) {
pawnAttack = MapGenerator.generateValidQueenMap(whites, blacks, Utils.getWhitePawn(status), Main.white);
} else {
// pawn - moves + attacks + chess check
pawnAttack = MapGenerator.generatePawnQueenMap(Utils.getWhitePawn(status), true);
whiteRawPawnAttack = pawnAttack;
pawnAttack = pawnAttack & ~(whites | blacks);
tmp |= MapGenerator.generatePawnAttackMap(Utils.getWhitePawn(status));
// if pawn can attack someone add this moves to the white moves
pawnAttack |= (tmp & blacks);
}
whiteRookAttack = MapGenerator.generateValidRookMap(whites, blacks, Utils.getWhiteRook(status), Main.white);
// long whiteRawRookAttack = MapGenerator.generateRookMap(Utils.getWhiteRook(status));
int move = 0;
for (int i = 0; i < 49; i++) {
// @TODO queen generation
if ((pawnAttack & 1) == 1) {// check if given move doesnt expose
// king to chess
if (isCorrectMove(Utils.setWhitePawn(status, i), Main.white)) {
move = (Utils.setPieceMoveFrom(Utils.setPieceMoveTo(Utils.setPieceMoveKind(0, Main.pawn), i),
Utils.getWhitePawn(status)));
moves[movePointer] = move;
movePointer++;
}
}
pawnAttack >>= 1;
if ((whiteKingAttack & 1) == 1) {
if (isCorrectMove(Utils.setWhiteKing(status, i), Main.white)) {
moves[movePointer] = (Utils.setPieceMoveFrom(
Utils.setPieceMoveTo(Utils.setPieceMoveKind(0, Main.king), i), Utils.getWhiteKing(status)));
movePointer++;
}
}
whiteKingAttack >>= 1;
if ((whiteRookAttack & 1) == 1) { //(((blackKingAttack | blackRawRookattack) & Utils.setBit(Utils.getWhiteKing(status), 0)) == 0 ) ||
if (isCorrectMove(Utils.setWhiteRook(status, i), Main.white)) {
move = (Utils.setPieceMoveFrom(Utils.setPieceMoveTo(Utils.setPieceMoveKind(0, Main.rook), i),
Utils.getWhiteRook(status)));
moves[movePointer] = move;
movePointer++;
}
}
whiteRookAttack >>= 1;
}
} else {
long blackKingAttack = 0;
long blackRookAttack = 0;
// init raw attacks - without chess checking
blackKingAttack = MapGenerator.generateKingMap(Utils.getBlackKing(status));
blackKingAttack = blackKingAttack & ~(blacks);
// rook - moves + shadow attacks + chess check
blackRookAttack = MapGenerator.generateValidRookMap(whites, blacks, Utils.getBlackRook(status), Main.black);
// long blackRawRookAttack = MapGenerator.generateRookMap(Utils.getBlackRook(status));
for (int i = 0; i < 49; i++) {
if ((blackKingAttack & 1) == 1) { //(((whiteKingAttack | whiteRawRookattack) & Utils.setBit(Utils.getBlackKing(status), 0)) == 0 ) ||
if (isCorrectMove(Utils.setBlackKing(status, i), Main.black)) {
moves[movePointer] = (Utils.setPieceMoveFrom(
Utils.setPieceMoveTo(Utils.setPieceMoveKind(0, Main.king), i), Utils.getBlackKing(status)));
movePointer++;
}
}
blackKingAttack >>= 1;
if ((blackRookAttack & 1) == 1) { //
if (isCorrectMove(Utils.setBlackRook(status, i), Main.black)) {
moves[movePointer] = (Utils.setPieceMoveFrom(
Utils.setPieceMoveTo(Utils.setPieceMoveKind(0, Main.rook), i), Utils.getBlackRook(status)));
movePointer++;
}
}
blackRookAttack >>= 1;
}
}
int[] retArray = new int[movePointer];
if (movePointer != 0)
System.arraycopy(moves, 0, retArray, 0, movePointer);
else
return new int[0];
return retArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment