Everything works with "En passant" and the moves are in their own class
This commit is contained in:
parent
4771b67898
commit
cfad9cd39e
|
|
@ -4,23 +4,23 @@ import java.util.ArrayList;
|
||||||
|
|
||||||
public class Move {
|
public class Move {
|
||||||
|
|
||||||
|
|
||||||
public ArrayList<int[]> getValidMoves(Piece piece, Piece[][] board, int width, int height, int[] enPassantTarget) {
|
public ArrayList<int[]> getValidMoves(Piece piece, Piece[][] board, int width, int height, int[] enPassantTarget) {
|
||||||
ArrayList<int[]> moves = new ArrayList<>();
|
ArrayList<int[]> moves = new ArrayList<>();
|
||||||
int x = piece.getX();
|
int x = piece.getX();
|
||||||
int y = piece.getY();
|
int y = piece.getY();
|
||||||
PieceType type = piece.getType();
|
PieceType type = piece.getType();
|
||||||
|
boolean isWhite = piece.isWhite();
|
||||||
|
|
||||||
if (type == PieceType.Pawn) {
|
if (type == PieceType.Pawn) {
|
||||||
int dir = piece.isWhite() ? 1 : -1;
|
int dir = isWhite ? 1 : -1;
|
||||||
int startRow = piece.isWhite() ? 1 : 6;
|
int startRow = isWhite ? 1 : 6;
|
||||||
|
|
||||||
int oneStep = y + dir;
|
int oneStep = y + dir;
|
||||||
if (inBounds(x, oneStep, width, height) && board[x][oneStep] == null) {
|
if (inBounds(x, oneStep, width, height) && board[x][oneStep] == null) {
|
||||||
moves.add(new int[]{x, oneStep});
|
moves.add(new int[]{x, oneStep});
|
||||||
|
|
||||||
int twoStep = y + 2 * dir;
|
int twoStep = y + 2 * dir;
|
||||||
if (y == startRow && inBounds(x, twoStep, width, height) &&
|
if (y == startRow && inBounds(x, twoStep, width, height) && board[x][twoStep] == null && board[x][oneStep] == null) {
|
||||||
board[x][twoStep] == null && board[x][oneStep] == null) {
|
|
||||||
moves.add(new int[]{x, twoStep});
|
moves.add(new int[]{x, twoStep});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -29,16 +29,11 @@ public class Move {
|
||||||
int nx = x + dx;
|
int nx = x + dx;
|
||||||
int ny = y + dir;
|
int ny = y + dir;
|
||||||
if (inBounds(nx, ny, width, height)) {
|
if (inBounds(nx, ny, width, height)) {
|
||||||
if (board[nx][ny] != null && board[nx][ny].isWhite() != piece.isWhite()) {
|
if (board[nx][ny] != null && board[nx][ny].isWhite() != isWhite) {
|
||||||
moves.add(new int[]{nx, ny});
|
moves.add(new int[]{nx, ny});
|
||||||
}
|
} else if (enPassantTarget != null && enPassantTarget[0] == nx && enPassantTarget[1] == ny) {
|
||||||
else if (board[nx][ny] == null && enPassantTarget != null &&
|
int pawnY = y;
|
||||||
nx == enPassantTarget[0] && ny == enPassantTarget[1]) {
|
if (board[nx][pawnY] != null && board[nx][pawnY].getType() == PieceType.Pawn && board[nx][pawnY].isWhite() != isWhite) {
|
||||||
int capturedY = y;
|
|
||||||
if (inBounds(nx, capturedY, width, height) &&
|
|
||||||
board[nx][capturedY] != null &&
|
|
||||||
board[nx][capturedY].getType() == PieceType.Pawn &&
|
|
||||||
board[nx][capturedY].isWhite() != piece.isWhite()) {
|
|
||||||
moves.add(new int[]{nx, ny});
|
moves.add(new int[]{nx, ny});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -46,6 +41,59 @@ public class Move {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else if (type == PieceType.Knight) {
|
||||||
|
int[][] jumps = {{2, 1}, {1, 2}, {-1, 2}, {-2, 1},
|
||||||
|
{-2, -1}, {-1, -2}, {1, -2}, {2, -1}};
|
||||||
|
for (int[] j : jumps) {
|
||||||
|
int nx = x + j[0], ny = y + j[1];
|
||||||
|
if (inBounds(nx, ny, width, height)) {
|
||||||
|
if (board[nx][ny] == null || board[nx][ny].isWhite() != isWhite) {
|
||||||
|
moves.add(new int[]{nx, ny});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (type == PieceType.Bishop || type == PieceType.Rook || type == PieceType.Queen) {
|
||||||
|
int[][] dirs;
|
||||||
|
if (type == PieceType.Bishop) {
|
||||||
|
dirs = new int[][]{{1,1}, {-1,1}, {-1,-1}, {1,-1}};
|
||||||
|
} else if (type == PieceType.Rook) {
|
||||||
|
dirs = new int[][]{{1,0}, {-1,0}, {0,1}, {0,-1}};
|
||||||
|
} else { // Queen
|
||||||
|
dirs = new int[][]{{1,0}, {-1,0}, {0,1}, {0,-1}, {1,1}, {-1,1}, {-1,-1}, {1,-1}};
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int[] d : dirs) {
|
||||||
|
int nx = x + d[0], ny = y + d[1];
|
||||||
|
while (inBounds(nx, ny, width, height)) {
|
||||||
|
if (board[nx][ny] == null) {
|
||||||
|
moves.add(new int[]{nx, ny});
|
||||||
|
} else {
|
||||||
|
if (board[nx][ny].isWhite() != isWhite) {
|
||||||
|
moves.add(new int[]{nx, ny});
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
nx += d[0]; ny += d[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (type == PieceType.King) {
|
||||||
|
int[][] dirs = {{1,0}, {-1,0}, {0,1}, {0,-1}, {1,1}, {-1,1}, {-1,-1}, {1,-1}};
|
||||||
|
for (int[] d : dirs) {
|
||||||
|
int nx = x + d[0], ny = y + d[1];
|
||||||
|
if (inBounds(nx, ny, width, height)) {
|
||||||
|
if (board[nx][ny] == null || board[nx][ny].isWhite() != isWhite) {
|
||||||
|
moves.add(new int[]{nx, ny});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tu peux ajouter ici le roque (castling) si tu veux, mais tu dois gérer hasMoved, menace, etc.
|
||||||
|
}
|
||||||
|
|
||||||
return moves;
|
return moves;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue