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

📄 jcmovelistgenerator.java

📁 这是自己在学习编写下象棋时参考过的很好的程序。
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
           {             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 );         }         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 );         }       }       // And perform the usual trick to abort the loop when we no longer       // have any pieces to look for       pieces ^= jcBoard.SquareBits[ square ];       if ( pieces == 0 )         return true;     }     return true;   }   private boolean ComputeBlackQueenMoves( jcBoard theBoard )   {     if ( !ComputeBlackRookMoves( theBoard, jcBoard.BLACK_QUEEN ) ) return false;     if ( !ComputeBlackBishopMoves( theBoard, jcBoard.BLACK_QUEEN ) ) return false;     return true;   }   private boolean ComputeBlackKingMoves( jcBoard theBoard )   {     // Fetch the bitboard containing position of the king     long pieces = theBoard.GetBitBoard( jcBoard.BLACK_KING );     // Find it!  There is only one king, so look for it and stop     int square;     for( square = 0; square < 64; square++ )     {       if ( ( jcBoard.SquareBits[ square ] & pieces ) != 0 )         break;     }     // Find its moves     for( int i = 0; i < KingMoves[ square ].length; i++ )     {       // Get the destination square       int dest = KingMoves[ 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_KING;       // 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 );     }     // Now, let's consider castling...     // Kingside first     if ( theBoard.GetCastlingStatus( jcBoard.CASTLE_KINGSIDE + jcPlayer.SIDE_BLACK ) )     {       // First, check whether there are empty squares between king and rook       if ( ( ( theBoard.GetBitBoard( jcBoard.ALL_BLACK_PIECES ) & jcBoard.EMPTYSQUARES_BLACK_KINGSIDE ) == 0 ) &&            ( ( theBoard.GetBitBoard( jcBoard.ALL_WHITE_PIECES ) & jcBoard.EMPTYSQUARES_BLACK_KINGSIDE ) == 0 ) )       {         jcMove mov = new jcMove();         mov.MovingPiece = jcBoard.BLACK_KING;         mov.SourceSquare = 4;         mov.DestinationSquare = 6;         mov.MoveType = jcMove.MOVE_CASTLING_KINGSIDE;         mov.CapturedPiece = jcBoard.EMPTY_SQUARE;         Moves.add( mov );       }     }     if ( theBoard.GetCastlingStatus( jcBoard.CASTLE_QUEENSIDE + jcPlayer.SIDE_BLACK ) )     {       if ( ( ( theBoard.GetBitBoard( jcBoard.ALL_BLACK_PIECES ) & jcBoard.EMPTYSQUARES_BLACK_QUEENSIDE ) == 0 ) &&            ( ( theBoard.GetBitBoard( jcBoard.ALL_WHITE_PIECES ) & jcBoard.EMPTYSQUARES_BLACK_QUEENSIDE ) == 0 ) )       {         jcMove mov = new jcMove();         mov.MovingPiece = jcBoard.BLACK_KING;         mov.SourceSquare = 4;         mov.DestinationSquare = 2;         mov.MoveType = jcMove.MOVE_CASTLING_QUEENSIDE;         mov.CapturedPiece = jcBoard.EMPTY_SQUARE;         Moves.add( mov );       }     }     return true;   }   private boolean ComputeBlackRookMoves( 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 black piece, so let's start looking at the top     // 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 ray = 0; ray < RookMoves[ square ].length; ray++ )         {           for( int i = 0; i < RookMoves[ square ][ ray ].length; i++ )           {             // Get the destination square             int dest = RookMoves[ 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_BLACK_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_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;               }               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 ComputeBlackBishopMoves( 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 black piece, so let's start looking at the top     // 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 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_BLACK_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_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, add the move to the list and interrupt the ray               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 ComputeBlackKnightMoves( jcBoard theBoard )   {     // Fetch the bitboard containing positions of these pieces     long pieces = theBoard.GetBitBoard( jcBoard.BLACK_KNIGHT );     // If there are no pieces of this type, no need to work very hard!     if ( pieces == 0 )     {       return true;     }     // This is a black piece, so let's start looking at the top

⌨️ 快捷键说明

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