Added en passant move

This commit is contained in:
Filip Znachor 2023-04-22 18:59:37 +02:00
parent 2476e1c6d3
commit 10633d9d1b
6 changed files with 49 additions and 17 deletions

View file

@ -246,4 +246,8 @@ public abstract class AbstractPiece implements IPiece {
return player;
}
public int getMoveCount() {
return moveCount;
}
}

View file

@ -1,4 +1,4 @@
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
/**
@ -12,11 +12,11 @@ public class Chess {
*/
public static void main(String[] args) {
JFrame okno = new JFrame();
okno.setTitle("Chess");
okno.setSize(800, 600);
okno.setMinimumSize(new Dimension(800, 600));
okno.setBackground(Color.GRAY);
JFrame window = new JFrame();
window.setTitle("Chess");
window.setSize(800, 600);
window.setMinimumSize(new Dimension(800, 600));
window.setBackground(Color.GRAY);
Chessboard chessboard = new Chessboard();
@ -25,12 +25,12 @@ public class Chess {
chessboard.setPlayer1(new Player(chessboard, startPositions[0], colors[0]));
chessboard.setPlayer2(new Player(chessboard, startPositions[1], colors[1]));
okno.add(chessboard);
okno.pack();
window.add(chessboard);
window.pack();
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okno.setLocationRelativeTo(null);
okno.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
window.setVisible(true);
}

View file

@ -52,9 +52,9 @@ public class Chessboard extends JPanel {
*/
public IPiece[][] pieces = new IPiece[SQUARE_COUNT][SQUARE_COUNT];
private boolean[][] possibleMoves;
public boolean[][] possibleMoves = new boolean[SQUARE_COUNT][SQUARE_COUNT];
private boolean[][] lastMove;
public boolean[][] lastMove = new boolean[SQUARE_COUNT][SQUARE_COUNT];
private Player player1;

View file

@ -66,5 +66,7 @@ public interface IPiece {
boolean isEndangered();
int getMoveCount();
}

View file

@ -50,7 +50,7 @@ public class King extends AbstractPiece {
return moves;
}
public boolean checkCastling(int direction) {
private boolean checkCastling(int direction) {
boolean rookNoMove = (player.getLeftRook().moveCount == 0 && direction == -1) || (player.getRightRook().moveCount == 0 && direction == 1);
if(moveCount == 0 && !player.inCheck() && rookNoMove) {
for (int pX = x + direction; (pX > 0 && pX < 7); pX += direction) {

View file

@ -28,21 +28,47 @@ public class Pawn extends AbstractPiece {
@Override
public boolean[][] getPossibleMoves(boolean attack) {
boolean[][] moves = new boolean[chessboard.SQUARE_COUNT][chessboard.SQUARE_COUNT];
int directionY = player.getStartPosition() == StartPosition.TOP ? 1 : -1;
for(int i=-1; i<=1; i++) {
for (int i = -1; i <= 1; i++) {
boolean isPieceThere = chessboard.getPiece(new PiecePosition(x+i, y+directionY)) != null;
if((i != 0 && (attack || isPieceThere)) || (i == 0 && (!attack && !isPieceThere))) setPossibleMove(moves, x+i, y+directionY);
}
boolean nextTwoPlacesEmpty = chessboard.getPiece(new PiecePosition(x, y+directionY*2)) == null && chessboard.getPiece(new PiecePosition(x, y+directionY)) == null;
if(moveCount == 0 && nextTwoPlacesEmpty) setPossibleMove(moves, x, y + directionY*2);
boolean firstPlaceEmpty = chessboard.getPiece(new PiecePosition(x, y+directionY*2)) == null;
boolean secondPlaceEmpty = chessboard.getPiece(new PiecePosition(x, y+directionY)) == null;
if(moveCount == 0 && firstPlaceEmpty && secondPlaceEmpty) setPossibleMove(moves, x, y + directionY*2);
for (int directionX : new int[]{-1, 1}) {
if(checkEnPassant(directionX)) setPossibleMove(moves, x+directionX, y+directionY);
}
return moves;
}
private boolean checkEnPassant(int directionX) {
IPiece piece = chessboard.getPiece(new PiecePosition(x+directionX, y));
if(piece == null) return false;
int pieceY = piece.getPlayer().getStartPosition() == StartPosition.TOP ? 3 : 4;
return chessboard.lastMove[y][x+directionX] && y == pieceY && piece.getMoveCount() == 1;
}
@Override
public void move(PiecePosition pos) {
for (int directionX : new int[]{-1, 1}) {
if(checkEnPassant(directionX) && pos.x == x+directionX) {
chessboard.removePiece(x+directionX, y);
}
}
super.move(pos);
int changeY = color == PieceColor.WHITE ? 0 : chessboard.SQUARE_COUNT-1;
if(y == changeY) new Queen(player, x, y);
}
}