Disservin / chess-library

C++ chess library
https://disservin.github.io/chess-library/
MIT License
73 stars 21 forks source link

enpassant moves not in legalmoves #86

Closed wthibault closed 6 months ago

wthibault commented 6 months ago

I have in interactive application and collect to and from squares in the UI then check for legality, but enpassant don't appear in the movelist... It seems like this is handled in pgn input handling though? I have no pgn.

            chess::Move Move = chess::Move::make(MoveFromSquare, MoveToSquare);
            // if legal, 
            //   make it on the board
            chess::Movelist LegalMoves;
            chess::movegen::legalmoves(LegalMoves, Board);
            if (LegalMoves.find(Move) != -1)
            {
Disservin commented 6 months ago

Have a look at the signature of the Move::make function https://disservin.github.io/chess-library/pages/move.html#general-implementation.

    template <uint16_t MoveType = 0>
    Move make(
        Square source,
        Square target,
        PieceType pt = PieceType::KNIGHT
    );

When you are creating a move this way you have to specify the MoveType and and PieceType (this is actually the promotion piece, knight is the default value because of the move encoding). If you just have the from and to square, you should consider using https://disservin.github.io/chess-library/pages/move.html#other-formats, though you need to handle to promotions slightly different.. i.e.

Move uciToMove(const Board& board, const std::string& uci);

uci is just e2e4 or e7e8q, see https://backscattering.de/chess/uci/#move.

wthibault commented 6 months ago

Thanks! that works.

Disservin commented 6 months ago

Don't forget about the promotion case :)