Starlight-30036225 / ChessTCP

FILLMELATER
0 stars 0 forks source link

En-passant #21

Closed Starlight-30036225 closed 9 months ago

Starlight-30036225 commented 9 months ago

While the code checks if en-passant is possible, executing it works differently. Currently the move can be suggested, so a pawn will be able to move accordingly but there are so far no checks to know when one has been completed.

This is important as this is not an ordinary take, the pawn does not move onto the pawn it is capturing so the current method of just replacing the piece will not work here.

Starlight-30036225 commented 9 months ago

` @Override public boolean move(Piece[][] board, String NewLocation) {

    int newx = Character.getNumericValue(NewLocation.charAt(0));
    int newy = Character.getNumericValue(NewLocation.charAt(1));
    //I dont need to use too many checks here, as the only chance to move into an empty space with a piece behind it is en-passant
    boolean TryingToPass =  ((board[newx][newy] == null) &&
            (board[newx][newy - direction] != null)&& (board[newx][newy - direction] != this));

    boolean doubleMove = (Math.abs(newy - y) == 2);
    if (super.move(board, NewLocation)){
        FirstMove = false;
        if (TryingToPass) {
            board[newx][newy - direction] = null;
        }
        Vulnerable = doubleMove;
        return true;
    }
    return false;
}`

Inside the overloaded move function inside the pawn I conduct two checks: -Checks if the move is an attempt at en-passant: Fortunately i dont need to do too many checks for this, as most of this is handled by checking if the move is possible. The only time a pawn can move onto an empty space, with a piece behind it is when doing en-passant.

-If the move is a double move: Really simple, just check if the distance is 2

If the move succeeds, and is enpassant it turns the piece behind the pawn to null.

Starlight-30036225 commented 9 months ago

image image image

Simple