📄 bitboardanalyzerimpl.java
字号:
int kingSquare = BitUtils.getHighestBit( _kingPosition); _kingRow = kingSquare >> 3; // Compute the line and row of the king for later use. _kingLine = kingSquare & 7; // Get and cache the empty squares of the current board. _emptySquares = getBoard().getEmptySquares(); // Compute the squares, that are not empty. _nonEmptySquares = ~_emptySquares; return isInKnightCheck( white) || isInBishopCheck( white) || isInRookCheck( white) || isInQueenCheck( white) || isInPawnCheck( white); } /** * Test if a king is in check on a given board. * * @param board The board to test. * @param white true, if the white king is checked, false otherwise. */ public final boolean isInCheck( BitBoard board, boolean white) { setBoard( board); return isInCheck( white); } /** * Check if a king is in check by a knight. * * @param white Flag to indicate if we test the white king. * * @return true, if the king is in check. false otherwise. */ private final boolean isInKnightCheck( boolean white) { // If we check for the white king, get the black knights long knightPositions = getBoard().getPositionOfPieces( white ? Piece.KNIGHT << 1 : ( Piece.KNIGHT << 1) | 1); // Compute a bitmask, that has 1 bits on all possible knight destinations while( knightPositions != 0L) { // Get the position of the first knight. int highestBit = BitUtils.getHighestBit( knightPositions); // Get all the moves for this knight and return immediately, if // the king is in check from this knight. if( ( getPlyGenerator().getKnightPlies( highestBit) & _kingPosition) != 0L) { return true; } // Remove this knight from the list. knightPositions &= ~(1L << highestBit); } return false; } /** * Check if a king is in check by a bishop. * * @param white Flag to indicate if we test the white king. * * @return true, if the king is in check. false otherwise. */ private final boolean isInBishopCheck( boolean white) { // Get all the bishop positions of the opposite color. long bishopPositions = getBoard().getPositionOfPieces( white ? Piece.BISHOP << 1 : ( Piece.BISHOP << 1) | 1); while( bishopPositions != 0L) { int highestBit = BitUtils.getHighestBit( bishopPositions); if( isInBishopPositionCheck( highestBit)) { return true; } bishopPositions &= ~(1L << highestBit); } return false; } /** * Check if the king is in check by a bishop. * * @param square The square, where the bishop is located. * * @return true, if the king is in check. false otherwise. */ private final boolean isInBishopPositionCheck( int square) { // Compute row and line of the rook. int bishopRow = square >> 3; int bishopLine = square & 7; if( Math.abs( bishopRow - _kingRow) == Math.abs( bishopLine - _kingLine)) { long bishopMask = 1L << square; if( bishopLine < _kingLine) { // The bishop is left from the king if( bishopRow < _kingRow) { // The bishop is below the king // Move the bishop to the upper right. while( ( ( bishopMask = ( bishopMask << 9)) & _nonEmptySquares) == 0L); } else { // Move the bishop to the lower right. while( ( ( bishopMask = ( ( bishopMask >> 7) & 0x01FFFFFFFFFFFFFFL)) & _nonEmptySquares) == 0L); } } else { // The bishop is right from the king. if( bishopRow < _kingRow) { // The bishop is below the king // Move the bishop to the upper left. while( ( ( bishopMask = ( bishopMask << 7)) & _nonEmptySquares) == 0L); } else { // Move the bishop to the lower left while( ( ( bishopMask = ( ( bishopMask >> 9) & 0x007FFFFFFFFFFFFFL)) & _nonEmptySquares) == 0L); } } return ( ( bishopMask & _kingPosition) != 0L); } return false; } /** * Check if a king is in check by a rook. * * @param white Flag to indicate if we test the white king. * * @return true, if the king is in check. false otherwise. */ private final boolean isInRookCheck( boolean white) { // Get all the rook positions of the opposite color. long rookPositions = getBoard().getPositionOfPieces( white ? Piece.ROOK << 1 : ( Piece.ROOK << 1) | 1); while( rookPositions != 0L) { int highestBit = BitUtils.getHighestBit( rookPositions); if( isInRookPositionCheck( highestBit)) { return true; } rookPositions &= ~(1L << highestBit); } return false; } /** * Check, if the king is in check of a rook position. * * @param square The square, where the rook is located. * * @return true, if the king is in check of this rook. * False otherwise. */ private final boolean isInRookPositionCheck( int square) { // Compute row and line of the rook. int rookRow = square >> 3; int rookLine = square & 7; long rookMask = 1L << square; // Compute a bitmask for the rook. if( rookLine == _kingLine) { if( rookRow > _kingRow) { while( ( ( rookMask = ( ( rookMask >> 8) & 0x00FFFFFFFFFFFFFFL)) & _nonEmptySquares) == 0L); } else { while( ( ( rookMask <<= 8) & _nonEmptySquares) == 0L); } if( ( rookMask & _kingPosition) != 0L) { return true; } } else { if( rookRow == _kingRow) { if( rookLine > _kingLine) { while( ( ( rookMask = ( ( rookMask >> 1) & 0x7FFFFFFFFFFFFFFFL)) & _nonEmptySquares) == 0L); } else { while( ( ( rookMask <<= 1) & _nonEmptySquares) == 0L); } if( ( rookMask & _kingPosition) != 0L) { return true; } } } return false; // No check for this rook. } /** * Check if a king is in check by a queen. * * @param white Flag to indicate if we test the white king. * * @return true, if the king is in check. false otherwise. */ private final boolean isInQueenCheck( boolean white) { // Get all the queen positions of the opposite color. long queenPositions = getBoard().getPositionOfPieces( white ? Piece.QUEEN << 1 : ( Piece.QUEEN << 1) | 1); // Since we can have more than 1 queen after a pawn reached the 8th row, check for all the bits while( queenPositions != 0L) { int highestBit = BitUtils.getHighestBit( queenPositions); if( isInQueenPositionCheck( highestBit)) { return true; } queenPositions &= ~(1L << highestBit); } return false; } /** * Check, if the king is in check of a queen. * * @param square The square, where the queen is located. * * @return true, if the king is in check. false otherwise. */ private final boolean isInQueenPositionCheck( int square) { return isInRookPositionCheck( square) || isInBishopPositionCheck( square); } /** * Check if a king is in check by a pawn. * * @param white Flag to indicate if we test the white king. * * @return true, if the king is in check, false otherwise. */ private final boolean isInPawnCheck( boolean white) { // The bitmask for the moved pawns. long movedPawns; // If we check for the white king, get the black pawns(!) if( white) { // Get the positions of all black pawns long pawnPositions = getBoard().getPositionOfPieces( Piece.PAWN << 1); // Move all the pawns movedPawns = ( ( ( ( pawnPositions & BitBoard._NOT_LINE_A) >> 9) & 0x007FFFFFFFFFFFFFL) | ( ( ( pawnPositions & BitBoard._NOT_LINE_H) >> 7) & 0x01FFFFFFFFFFFFFFL)); } else { // Get the positions of all white pawns long pawnPositions = getBoard().getPositionOfPieces( ( Piece.PAWN << 1) | 1); // Move all the pawns movedPawns = ( ( ( pawnPositions & BitBoard._NOT_LINE_H) << 9) | ( ( pawnPositions & BitBoard._NOT_LINE_A) << 7)); } // If the moved pawns are on the king position, the king is in check. return ( ( movedPawns & _kingPosition) != 0L); } /** * Analyzed a new board. * * @param board The new board to analyze. * @param white Flag to indicate, if white has the next move. */ public final short analyze( BitBoard board, boolean white) { setBoard( board); setMoveRight( white); return analyze(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -