⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jcmovelistgenerator.java

📁 这是自己在学习编写下象棋时参考过的很好的程序。
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
   private boolean ComputeWhiteBishopMoves( jcBoard theBoard, int pieceType )   {     // Fetch the bitboard containing positions of these pieces     long pieces = theBoard.GetBitBoard( pieceType );     // If there are no pieces of this type, no need to work very hard!     if ( pieces == 0 )     {       return true;     }     // This is a white piece, so let's start looking at the bottom     // of the board     for( int square = 63; square >= 0; square-- )     {       if ( ( pieces & jcBoard.SquareBits[ square ] ) != 0 )       {         // There is a piece here; find its moves         for( int ray = 0; ray < BishopMoves[ square ].length; ray++ )         {           for( int i = 0; i < BishopMoves[ square ][ ray ].length; i++ )           {             // Get the destination square             int dest = BishopMoves[ square ][ ray ][ i ];             // Is it occupied by a friendly piece?  If so, can't move there             // AND we must discontinue the current ray             if ( ( theBoard.GetBitBoard( jcBoard.ALL_WHITE_PIECES ) &                  jcBoard.SquareBits[ dest ] ) != 0 )               break;             // Otherwise, the move is legal, so we must prepare to add it             jcMove mov = new jcMove();             mov.SourceSquare = square;             mov.DestinationSquare = dest;             mov.MovingPiece = pieceType;             // Is the destination occupied by an enemy?  If so, we have a capture             if ( ( theBoard.GetBitBoard( jcBoard.ALL_BLACK_PIECES ) &                  jcBoard.SquareBits[ dest ] ) != 0 )             {               mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY;               mov.CapturedPiece = theBoard.FindBlackPiece( dest );               // If the piece we find is a king, abort because the board               // position is illegal!               if ( mov.CapturedPiece == jcBoard.BLACK_KING )               {                 return false;               }               Moves.add( mov );               break;             }             // otherwise, it is a simple move             else             {               mov.MoveType = jcMove.MOVE_NORMAL;               mov.CapturedPiece = jcBoard.EMPTY_SQUARE;               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 ComputeWhiteKnightMoves( jcBoard theBoard )   {     // Fetch the bitboard containing positions of these pieces     long pieces = theBoard.GetBitBoard( jcBoard.WHITE_KNIGHT );     // If there are no pieces of this type, no need to work very hard!     if ( pieces == 0 )     {       return true;     }     // This is a white piece, so let's start looking at the bottom     // of the board     for( int square = 63; square >= 0; 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_WHITE_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.WHITE_KNIGHT;           // Is the destination occupied by an enemy?  If so, we have a capture           if ( ( theBoard.GetBitBoard( jcBoard.ALL_BLACK_PIECES ) &                jcBoard.SquareBits[ dest ] ) != 0 )           {             mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY;             mov.CapturedPiece = theBoard.FindBlackPiece( dest );             // If the piece we find is a king, abort because the board             // position is illegal!             if ( mov.CapturedPiece == jcBoard.BLACK_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 ComputeWhitePawnMoves( jcBoard theBoard )   {     // Fetch the bitboard containing positions of these pieces     long pieces = theBoard.GetBitBoard( jcBoard.WHITE_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 white piece, so let's start looking at the bottom     // of the board... But only consider positions where a pawn can     // actually dwell!     int dest;     for( int square = 55; square >= 8; 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 > 15 )         {           jcMove mov = new jcMove();           mov.SourceSquare = square;           mov.DestinationSquare = dest;           mov.MovingPiece = jcBoard.WHITE_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 >= 48 )           {             dest -= 8;             if ( ( allPieces & jcBoard.SquareBits[ dest ] ) == 0 )             {               mov = new jcMove();               mov.SourceSquare = square;               mov.DestinationSquare = dest;               mov.MovingPiece = jcBoard.WHITE_PAWN;               mov.MoveType = jcMove.MOVE_NORMAL;               Moves.add( mov );             }           }         }         else  // if square < 16         {           // We are now looking at pawn promotion!           jcMove mov = new jcMove();           mov.SourceSquare = square;           mov.DestinationSquare = dest;           mov.MovingPiece = jcBoard.WHITE_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.WHITE_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.WHITE_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.WHITE_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 - 7;         // Try an ordinary capture first         if ( ( theBoard.GetBitBoard( jcBoard.ALL_BLACK_PIECES ) & jcBoard.SquareBits[ dest ] ) != 0 )         {           jcMove mov = new jcMove();           mov.SourceSquare = square;           mov.DestinationSquare = dest;           mov.MovingPiece = jcBoard.WHITE_PAWN;           mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY;           if ( dest < 8 )             mov.MoveType += jcMove.MOVE_PROMOTION_QUEEN;           mov.CapturedPiece = theBoard.FindBlackPiece( dest );           Moves.add( mov );           // Other promotion captures           if ( dest < 8 )           {             mov = new jcMove();             mov.SourceSquare = square;             mov.DestinationSquare = dest;             mov.MovingPiece = jcBoard.WHITE_PAWN;             mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY + jcMove.MOVE_PROMOTION_KNIGHT;             mov.CapturedPiece = theBoard.FindBlackPiece( dest );             Moves.add( mov );             mov = new jcMove();             mov.SourceSquare = square;             mov.DestinationSquare = dest;             mov.MovingPiece = jcBoard.WHITE_PAWN;             mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY + jcMove.MOVE_PROMOTION_BISHOP;             mov.CapturedPiece = theBoard.FindBlackPiece( dest );             Moves.add( mov );             mov = new jcMove();             mov.SourceSquare = square;             mov.DestinationSquare = dest;             mov.MovingPiece = jcBoard.WHITE_PAWN;             mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY + jcMove.MOVE_PROMOTION_ROOK;             mov.CapturedPiece = theBoard.FindBlackPiece( 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.WHITE_PAWN;           mov.MoveType = jcMove.MOVE_CAPTURE_EN_PASSANT;           mov.CapturedPiece = jcBoard.BLACK_PAWN;           Moves.add( mov );         }       }       else if ( ( square % 8 ) == 7 )       {         dest = square - 9;         // Try an ordinary capture first         if ( ( theBoard.GetBitBoard( jcBoard.ALL_BLACK_PIECES ) & jcBoard.SquareBits[ dest ] ) != 0 )         {           jcMove mov = new jcMove();           mov.SourceSquare = square;           mov.DestinationSquare = dest;           mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY;           if ( dest < 8 )             mov.MoveType += jcMove.MOVE_PROMOTION_QUEEN;           mov.MovingPiece = jcBoard.WHITE_PAWN;           mov.CapturedPiece = theBoard.FindBlackPiece( dest );           Moves.add( mov );           // Other promotion captures           if ( dest < 8 )           {             mov = new jcMove();             mov.SourceSquare = square;             mov.DestinationSquare = dest;             mov.MovingPiece = jcBoard.WHITE_PAWN;             mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY + jcMove.MOVE_PROMOTION_KNIGHT;             mov.CapturedPiece = theBoard.FindBlackPiece( dest );             Moves.add( mov );             mov = new jcMove();             mov.SourceSquare = square;             mov.DestinationSquare = dest;             mov.MovingPiece = jcBoard.WHITE_PAWN;             mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY + jcMove.MOVE_PROMOTION_BISHOP;             mov.CapturedPiece = theBoard.FindBlackPiece( dest );             Moves.add( mov );             mov = new jcMove();             mov.SourceSquare = square;             mov.DestinationSquare = dest;             mov.MovingPiece = jcBoard.WHITE_PAWN;             mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY + jcMove.MOVE_PROMOTION_ROOK;             mov.CapturedPiece = theBoard.FindBlackPiece( 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.WHITE_PAWN;           mov.CapturedPiece = jcBoard.BLACK_PAWN;           Moves.add( mov );         }       }       else       {         dest = square - 7;         // Try an ordinary capture first         if ( ( theBoard.GetBitBoard( jcBoard.ALL_BLACK_PIECES ) & jcBoard.SquareBits[ dest ] ) != 0 )         {           jcMove mov = new jcMove();           mov.SourceSquare = square;           mov.DestinationSquare = dest;           mov.MovingPiece = jcBoard.WHITE_PAWN;           mov.MoveType = jcMove.MOVE_CAPTURE_ORDINARY;           if ( dest < 8 )             mov.MoveType += jcMove.MOVE_PROMOTION_QUEEN;           mov.CapturedPiece = theBoard.FindBlackPiece( dest );           Moves.add( mov );           // Other promotion captures           if ( dest < 8 )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -