en-passant method for white and black pawns
This commit is contained in:
parent
22dfad92d7
commit
b6d2dc4045
|
|
@ -15,6 +15,7 @@ public class Board {
|
||||||
private int ym;
|
private int ym;
|
||||||
private int turnNumber;
|
private int turnNumber;
|
||||||
private boolean turnColor;
|
private boolean turnColor;
|
||||||
|
private Move lastMove; // new field
|
||||||
private ArrayList<ArrayList<Boolean>> possibleMoves = new ArrayList<>();
|
private ArrayList<ArrayList<Boolean>> possibleMoves = new ArrayList<>();
|
||||||
private LinkedList<BoardHistory> boardHistory = new LinkedList<>();
|
private LinkedList<BoardHistory> boardHistory = new LinkedList<>();
|
||||||
|
|
||||||
|
|
@ -129,6 +130,13 @@ public class Board {
|
||||||
boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
|
boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
|
||||||
Piece pieceToMove = this.board.get(this.ym).get(this.xm);
|
Piece pieceToMove = this.board.get(this.ym).get(this.xm);
|
||||||
boolean isCastlingMove = pieceToMove.getType() == PieceType.King && Math.abs(x - xm) == 2;
|
boolean isCastlingMove = pieceToMove.getType() == PieceType.King && Math.abs(x - xm) == 2;
|
||||||
|
|
||||||
|
if (pieceToMove instanceof Pawn && x != xm && board.get(y).get(x) == null) {
|
||||||
|
// En passant capture
|
||||||
|
board.get(ym).set(x, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//check for castling mode:
|
//check for castling mode:
|
||||||
if (isCastlingMove) {
|
if (isCastlingMove) {
|
||||||
|
|
@ -156,6 +164,7 @@ public class Board {
|
||||||
board.get(this.ym).set(this.xm,null);
|
board.get(this.ym).set(this.xm,null);
|
||||||
this.setPiece(x,y,pieceToMove.getType(),pieceToMove.isWhite());
|
this.setPiece(x,y,pieceToMove.getType(),pieceToMove.isWhite());
|
||||||
Piece movedPiece = this.getPiece(x, y);
|
Piece movedPiece = this.getPiece(x, y);
|
||||||
|
lastMove = new Move(xm, ym, x, y);
|
||||||
if (movedPiece instanceof Pawn) {
|
if (movedPiece instanceof Pawn) {
|
||||||
int promotionRow = movedPiece.isWhite() ? 0 : 7;
|
int promotionRow = movedPiece.isWhite() ? 0 : 7;
|
||||||
if (y == promotionRow) {
|
if (y == promotionRow) {
|
||||||
|
|
@ -215,6 +224,9 @@ public class Board {
|
||||||
// System.out.println("a"); // Debug
|
// System.out.println("a"); // Debug
|
||||||
select = false;
|
select = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
possibleMoves = board.get(y).get(x).getPossibleMoves(board, lastMove);//same as kingCheck
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSelected(int x, int y) {
|
public boolean isSelected(int x, int y) {
|
||||||
|
|
@ -285,6 +297,13 @@ public class Board {
|
||||||
boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
|
boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
|
||||||
Piece pieceToMove = this.board.get(ym).get(xm);
|
Piece pieceToMove = this.board.get(ym).get(xm);
|
||||||
boolean isCastlingMove = pieceToMove.getType() == PieceType.King && Math.abs(x - xm) == 2;
|
boolean isCastlingMove = pieceToMove.getType() == PieceType.King && Math.abs(x - xm) == 2;
|
||||||
|
lastMove = new Move(xm, ym, x, y);
|
||||||
|
|
||||||
|
if (pieceToMove instanceof Pawn && x != xm && board.get(y).get(x) == null) {
|
||||||
|
// En passant capture
|
||||||
|
board.get(ym).set(x, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//check for castling mode:
|
//check for castling mode:
|
||||||
if (isCastlingMove) {
|
if (isCastlingMove) {
|
||||||
|
|
|
||||||
|
|
@ -31,4 +31,9 @@ public class Move {
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "(" + fromX + "," + fromY + ") → (" + toX + "," + toY + ")";
|
return "(" + fromX + "," + fromY + ") → (" + toX + "," + toY + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Pawn getMovedPiece() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue