bhlangonijr / chesslib

chess library for legal move generation, FEN/PGN parsing and more
Apache License 2.0
225 stars 78 forks source link

MoveGenerator for a specific piece #18

Closed Spriithy closed 5 years ago

Spriithy commented 5 years ago

Hi, is it possible to this to generate all valid moves for a given piece ? I've only found general purpose move generations in class MoveGenerator. Thanks

bhlangonijr commented 5 years ago

Hello, yes indeed in MoveGenerator itself.

Ex.: MoveList moves = new MoveList(); MoveGenerator.generateBishopMoves(board, moves); or MoveGenerator.generatePawnMoves(board, moves); and MoveGenerator.generatePawnCaptures(board, moves);

but remember these are pseudo-legal moves and need to be checked for legality.

emiljanQ3 commented 4 years ago

Hi!

I'm looking for the same functionality, or at least something similar. The methods generateBishopMoves for example does not generate the moves for a specific bishop but for both bishops at the same time if I'm interpreting your code correctly. While this part of the code seems to be what generates moves for a specific bishop, a specific source: List<Square> target = Bitboard.bbToSquareList( Bitboard.getBishopAttacks(board.getBitboard(), sqSource) & ~board.getBitboard(side)); for (Square sqTarget : target) { moves.add(new Move(sqSource, sqTarget, Piece.NONE)); } (Sorry about the wierd formatting, I'm not usally commenting on GitHub and don't know how to format it better.)

While I will be able to quickly write something that generates moves for a specific piece based on your code it would be really nice to have that functionality out of the box in MoveGenerator.

Cheers and thanks for a great library!

bhlangonijr commented 4 years ago

Hi!

I'm looking for the same functionality, or at least something similar. The methods generateBishopMoves for example does not generate the moves for a specific bishop but for both bishops at the same time if I'm interpreting your code correctly. While this part of the code seems to be what generates moves for a specific bishop, a specific source: List<Square> target = Bitboard.bbToSquareList( Bitboard.getBishopAttacks(board.getBitboard(), sqSource) & ~board.getBitboard(side)); for (Square sqTarget : target) { moves.add(new Move(sqSource, sqTarget, Piece.NONE)); } (Sorry about the wierd formatting, I'm not usally commenting on GitHub and don't know how to format it better.)

While I will be able to quickly write something that generates moves for a specific piece based on your code it would be really nice to have that functionality out of the box in MoveGenerator.

Cheers and thanks for a great library!

Hey there, As a temporary solution you could duplicate the method and add a bitboard mask parameter where you can specify the pieces you want to generate the moves for:

    public static void generateBishopMoves(Board board, long mask, MoveList moves) {
        Side side = board.getSideToMove();
        long pieces = mask & board.getBitboard(Piece.make(side, PieceType.BISHOP));
        if (pieces != 0L) {
            List<Square> source = Bitboard.bbToSquareList(pieces);
            for (Square sqSource : source) {
                List<Square> target = Bitboard.bbToSquareList(
                        Bitboard.getBishopAttacks(board.getBitboard(), sqSource)
                                & ~board.getBitboard(side));
                for (Square sqTarget : target) {
                    moves.add(new Move(sqSource, sqTarget, Piece.NONE));
                }
            }
        }
    }

you can specify the bishop on any square like:

    List<Square> sq = getPieceLocation(Piece.make(side, PieceType.BISHOP));
    long bishopBb = sq.get(0).getBitboard(); // let's randomly take the first bishop
    generateBishopMoves(board, bishopBb, moves);