Implemented en passant

This commit is contained in:
Kai S. K. Engelbart 2019-07-28 13:51:10 +02:00
parent cac235a0db
commit 984bedfafe
3 changed files with 33 additions and 5 deletions

View File

@ -109,6 +109,11 @@ public class Board implements Cloneable {
// TODO: Select promotion // TODO: Select promotion
setDest(move, new Queen(piece.getColor(), this)); setDest(move, new Queen(piece.getColor(), this));
break; break;
case EN_PASSANT:
setDest(move, piece);
setPos(move, null);
boardArr[move.dest.x][move.dest.y - move.ySign] = null;
break;
case CASTLING: case CASTLING:
// Move the king // Move the king
setDest(move, piece); setDest(move, piece);
@ -160,6 +165,11 @@ public class Board implements Cloneable {
setPos(move, new Pawn(getDest(move).getColor(), this)); setPos(move, new Pawn(getDest(move).getColor(), this));
setDest(move, capturedPiece); setDest(move, capturedPiece);
break; break;
case EN_PASSANT:
setPos(move, getDest(move));
setDest(move, null);
boardArr[move.dest.x][move.dest.y - move.ySign] = new Pawn(getPos(move).getColor().opposite(), this);
break;
case CASTLING: case CASTLING:
// Move the king // Move the king
setPos(move, getDest(move)); setPos(move, getDest(move));
@ -504,4 +514,9 @@ public class Board implements Cloneable {
* @return The active color for the next move * @return The active color for the next move
*/ */
public Color getActiveColor() { return log.getActiveColor(); } public Color getActiveColor() { return log.getActiveColor(); }
/**
* @return The current en passant square, or null if there isn't any
*/
public Position getEnPassantSquare() { return log.getLast() == null ? null : log.getLast().enPassant; }
} }

View File

@ -17,10 +17,10 @@ public class Pawn extends Piece {
@Override @Override
public boolean isValidMove(Move move) { public boolean isValidMove(Move move) {
// TODO: en passant
boolean step = move.isVertical() && move.yDist == 1; boolean step = move.isVertical() && move.yDist == 1;
boolean doubleStep = move.isVertical() && move.yDist == 2; boolean doubleStep = move.isVertical() && move.yDist == 2;
boolean strafe = move.isDiagonal() && move.xDist == 1; boolean strafe = move.isDiagonal() && move.xDist == 1;
boolean enPassant = false;
if (getColor() == Color.WHITE) doubleStep &= move.pos.y == 6; if (getColor() == Color.WHITE) doubleStep &= move.pos.y == 6;
else doubleStep &= move.pos.y == 1; else doubleStep &= move.pos.y == 1;
@ -28,7 +28,14 @@ public class Pawn extends Piece {
if (move.ySign == 1 && move.pos.y == 6 || move.ySign == -1 && move.pos.y == 1) if (move.ySign == 1 && move.pos.y == 6 || move.ySign == -1 && move.pos.y == 1)
move.type = Move.Type.PAWN_PROMOTION; move.type = Move.Type.PAWN_PROMOTION;
return (step ^ doubleStep ^ strafe) && move.ySign == (getColor() == Color.WHITE ? -1 : 1) && isFreePath(move); // Mark the move as en passant if necessary
if (strafe && move.dest.equals(board.getEnPassantSquare())) {
enPassant = true;
move.type = Move.Type.EN_PASSANT;
}
return enPassant || (step ^ doubleStep ^ strafe) && move.ySign == (getColor() == Color.WHITE ? -1 : 1)
&& isFreePath(move);
} }
@Override @Override
@ -72,10 +79,16 @@ public class Pawn extends Piece {
if (isFreePath(move)) moves.add(move); if (isFreePath(move)) moves.add(move);
} }
// Mark moves as pawn promotions if necessary // Mark moves as pawn promotion if necessary
if (sign == 1 && pos.y == 6 || sign == -1 && pos.y == 1) if (sign == 1 && pos.y == 6 || sign == -1 && pos.y == 1)
moves.parallelStream().forEach(m -> m.type = Move.Type.PAWN_PROMOTION); moves.parallelStream().forEach(m -> m.type = Move.Type.PAWN_PROMOTION);
// Add en passant move if necessary
if (board.getEnPassantSquare() != null) {
Move move = new Move(pos, board.getEnPassantSquare(), Move.Type.EN_PASSANT);
if (move.isDiagonal() && move.xDist == 1) moves.add(move);
}
return moves; return moves;
} }

View File

@ -86,7 +86,7 @@ public class Game {
} }
public void reset() { public void reset() {
players.forEach((k, v) -> v.cancelMove()); players.values().forEach(Player::cancelMove);
board.initializeDefaultPositions(); board.initializeDefaultPositions();
boardComponent.repaint(); boardComponent.repaint();
overlayComponent.clearDots(); overlayComponent.clearDots();
@ -94,7 +94,7 @@ public class Game {
} }
/** /**
* Removed all connections between the game and the ui. * Removed all connections between the game and the UI.
*/ */
public void disconnect() { public void disconnect() {
players.values().forEach(Player::disconnect); players.values().forEach(Player::disconnect);