📄 jcmovelistgenerator.java
字号:
// of the board for( int square = 0; square < 64; square++ ) { if ( ( pieces & jcBoard.SquareBits[ square ] ) != 0 ) { // There is a piece here; find its moves for( int i = 0; i < KnightMoves[ square ].length; i++ ) { // Get the destination square int dest = KnightMoves[ square ][ i ]; // Is it occupied by a friendly piece? If so, can't move there if ( ( theBoard.GetBitBoard( jcBoard.ALL_BLACK_PIECES ) & jcBoard.SquareBits[ dest ] ) != 0 ) continue; // Otherwise, the move is legal, so we must prepare to add it jcMove mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_KNIGHT; // Is the destination occupied by an enemy? If so, we have a capture if ( ( theBoard.GetBitBoard( jcBoard.ALL_WHITE_PIECES ) & jcBoard.SquareBits[ dest ] ) != 0 ) { mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY; mov.CapturedPiece = theBoard.FindWhitePiece( dest ); // If the piece we find is a king, abort because the board // position is illegal! if ( mov.CapturedPiece == jcBoard.WHITE_KING ) { return false; } } // otherwise, it is a simple move else { mov.MoveType = jcMove.MOVE_NORMAL; mov.CapturedPiece = jcBoard.EMPTY_SQUARE; } // And we add the move to the list Moves.add( mov ); } // Turn off the bit in the temporary bitboard; this way, we can // detect whether we have found the last of this type of piece // and short-circuit the loop pieces ^= jcBoard.SquareBits[ square ]; if ( pieces == 0 ) return true; } } // We should never get here, but the return statement is added to prevent // obnoxious compiler warnings return true; } private boolean ComputeBlackPawnMoves( jcBoard theBoard ) { // Fetch the bitboard containing positions of these pieces long pieces = theBoard.GetBitBoard( jcBoard.BLACK_PAWN ); // If there are no pieces of this type, no need to work very hard! if ( pieces == 0 ) { return true; } // a small optimization long allPieces = theBoard.GetBitBoard( jcBoard.ALL_BLACK_PIECES ) | theBoard.GetBitBoard( jcBoard.ALL_WHITE_PIECES ); // This is a black piece, so let's start looking at the top // of the board... But only consider positions where a pawn can // actually dwell! int dest; for( int square = 8; square < 56; square++ ) { if ( ( pieces & jcBoard.SquareBits[ square ] ) == 0 ) continue; // First, try a normal pawn pushing dest = square + 8; if ( ( allPieces & jcBoard.SquareBits[ dest ] ) == 0 ) { // Unless this push results in a promotion... if ( square < 48 ) { jcMove mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_NORMAL; Moves.add( mov ); // Is there a chance to perform a double push? Only if the piece // is in its original square if ( square < 16 ) { dest += 8; if ( ( allPieces & jcBoard.SquareBits[ dest ] ) == 0 ) { mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_NORMAL; Moves.add( mov ); } } } else // if square >= 48 { // We are now looking at pawn promotion! jcMove mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_PROMOTION_QUEEN + jcMove.MOVE_NORMAL; Moves.add( mov ); mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_PROMOTION_KNIGHT + jcMove.MOVE_NORMAL; Moves.add( mov ); mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_PROMOTION_ROOK + jcMove.MOVE_NORMAL; Moves.add( mov ); mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_PROMOTION_BISHOP + jcMove.MOVE_NORMAL; Moves.add( mov ); } } // Now, let's try a capture // Three cases: the pawn is on the 1st file, the 8th file, or elsewhere if ( ( square % 8 ) == 0 ) { dest = square + 9; // Try an ordinary capture first if ( ( theBoard.GetBitBoard( jcBoard.ALL_WHITE_PIECES ) & jcBoard.SquareBits[ dest ] ) != 0 ) { jcMove mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY; if ( dest >= 56 ) mov.MoveType += jcMove.MOVE_PROMOTION_QUEEN; mov.CapturedPiece = theBoard.FindWhitePiece( dest ); Moves.add( mov ); // Other promotion captures if ( dest >= 56 ) { mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY + jcMove.MOVE_PROMOTION_KNIGHT; mov.CapturedPiece = theBoard.FindWhitePiece( dest ); Moves.add( mov ); mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY + jcMove.MOVE_PROMOTION_BISHOP; mov.CapturedPiece = theBoard.FindWhitePiece( dest ); Moves.add( mov ); mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY + jcMove.MOVE_PROMOTION_ROOK; mov.CapturedPiece = theBoard.FindWhitePiece( dest ); Moves.add( mov ); } } // Now, try an en passant capture else if ( ( theBoard.GetEnPassantPawn() & jcBoard.SquareBits[ dest ] ) != 0 ) { jcMove mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_CAPTURE_EN_PASSANT; mov.CapturedPiece = jcBoard.WHITE_PAWN; Moves.add( mov ); } } else if ( ( square % 8 ) == 7 ) { dest = square + 7; // Try an ordinary capture first if ( ( theBoard.GetBitBoard( jcBoard.ALL_WHITE_PIECES ) & jcBoard.SquareBits[ dest ] ) != 0 ) { jcMove mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY; if ( dest >= 56 ) mov.MoveType += jcMove.MOVE_PROMOTION_QUEEN; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.CapturedPiece = theBoard.FindWhitePiece( dest ); Moves.add( mov ); // Other promotion captures if ( dest >= 56 ) { mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY + jcMove.MOVE_PROMOTION_KNIGHT; mov.CapturedPiece = theBoard.FindWhitePiece( dest ); Moves.add( mov ); mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY + jcMove.MOVE_PROMOTION_BISHOP; mov.CapturedPiece = theBoard.FindWhitePiece( dest ); Moves.add( mov ); mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY + jcMove.MOVE_PROMOTION_ROOK; mov.CapturedPiece = theBoard.FindWhitePiece( dest ); Moves.add( mov ); } } // Now, try an en passant capture else if ( ( theBoard.GetEnPassantPawn() & jcBoard.SquareBits[ dest ] ) != 0 ) { jcMove mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MoveType = jcMove.MOVE_CAPTURE_EN_PASSANT; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.CapturedPiece = jcBoard.WHITE_PAWN; Moves.add( mov ); } } else { dest = square + 9; // Try an ordinary capture first if ( ( theBoard.GetBitBoard( jcBoard.ALL_WHITE_PIECES ) & jcBoard.SquareBits[ dest ] ) != 0 ) { jcMove mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY; if ( dest >= 56 ) mov.MoveType += jcMove.MOVE_PROMOTION_QUEEN; mov.CapturedPiece = theBoard.FindWhitePiece( dest ); Moves.add( mov ); // Other promotion captures if ( dest >= 56 ) { mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY + jcMove.MOVE_PROMOTION_KNIGHT; mov.CapturedPiece = theBoard.FindWhitePiece( dest ); Moves.add( mov ); mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY + jcMove.MOVE_PROMOTION_BISHOP; mov.CapturedPiece = theBoard.FindWhitePiece( dest ); Moves.add( mov ); mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY + jcMove.MOVE_PROMOTION_ROOK; mov.CapturedPiece = theBoard.FindWhitePiece( dest ); Moves.add( mov ); } } // Now, try an en passant capture else if ( ( theBoard.GetEnPassantPawn() & jcBoard.SquareBits[ dest ] ) != 0 ) { jcMove mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MoveType = jcMove.MOVE_CAPTURE_EN_PASSANT; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.CapturedPiece = jcBoard.WHITE_PAWN; Moves.add( mov ); } dest = square + 7; // Try an ordinary capture first if ( ( theBoard.GetBitBoard( jcBoard.ALL_WHITE_PIECES ) & jcBoard.SquareBits[ dest ] ) != 0 ) { jcMove mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY; if ( dest >= 56 ) mov.MoveType += jcMove.MOVE_PROMOTION_QUEEN; mov.CapturedPiece = theBoard.FindWhitePiece( dest ); Moves.add( mov ); // Other promotion captures if ( dest >= 56 ) { mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY + jcMove.MOVE_PROMOTION_KNIGHT; mov.CapturedPiece = theBoard.FindWhitePiece( dest ); Moves.add( mov ); mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY + jcMove.MOVE_PROMOTION_BISHOP; mov.CapturedPiece = theBoard.FindWhitePiece( dest ); Moves.add( mov ); mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY + jcMove.MOVE_PROMOTION_ROOK; mov.CapturedPiece = theBoard.FindWhitePiece( dest ); Moves.add( mov ); } } // Now, try an en passant capture else if ( ( theBoard.GetEnPassantPawn() & jcBoard.SquareBits[ dest ] ) != 0 ) { jcMove mov = new jcMove(); mov.SourceSquare = square; mov.DestinationSquare = dest; mov.MoveType = jcMove.MOVE_CAPTURE_EN_PASSANT; mov.MovingPiece = jcBoard.BLACK_PAWN; mov.CapturedPiece = jcBoard.WHITE_PAWN; Moves.add( mov ); } } // And perform the usual trick to abort the loop when we no longer // have any pieces to look for pieces ^= jcBoard.SquareBits[ square ];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -