Everything works with "En passant" and the moves are in their own class

This commit is contained in:
Bastien Druillette 2025-05-13 16:19:50 +02:00
parent 4771b67898
commit cfad9cd39e
1 changed files with 63 additions and 15 deletions

View File

@ -3,24 +3,24 @@ package backend;
import java.util.ArrayList;
public class Move {
public ArrayList<int[]> getValidMoves(Piece piece, Piece[][] board, int width, int height, int[] enPassantTarget) {
ArrayList<int[]> moves = new ArrayList<>();
int x = piece.getX();
int y = piece.getY();
PieceType type = piece.getType();
boolean isWhite = piece.isWhite();
if (type == PieceType.Pawn) {
int dir = piece.isWhite() ? 1 : -1;
int startRow = piece.isWhite() ? 1 : 6;
int dir = isWhite ? 1 : -1;
int startRow = isWhite ? 1 : 6;
int oneStep = y + dir;
if (inBounds(x, oneStep, width, height) && board[x][oneStep] == null) {
moves.add(new int[]{x, oneStep});
int twoStep = y + 2 * dir;
if (y == startRow && inBounds(x, twoStep, width, height) &&
board[x][twoStep] == null && board[x][oneStep] == null) {
if (y == startRow && inBounds(x, twoStep, width, height) && board[x][twoStep] == null && board[x][oneStep] == null) {
moves.add(new int[]{x, twoStep});
}
}
@ -29,16 +29,11 @@ public class Move {
int nx = x + dx;
int ny = y + dir;
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});
}
else if (board[nx][ny] == null && enPassantTarget != null &&
nx == enPassantTarget[0] && ny == enPassantTarget[1]) {
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()) {
} else if (enPassantTarget != null && enPassantTarget[0] == nx && enPassantTarget[1] == ny) {
int pawnY = y;
if (board[nx][pawnY] != null && board[nx][pawnY].getType() == PieceType.Pawn && board[nx][pawnY].isWhite() != isWhite) {
moves.add(new int[]{nx, ny});
}
}
@ -46,10 +41,63 @@ 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;
}
private boolean inBounds(int x, int y, int width, int height) {
return x >= 0 && x < width && y >= 0 && y < height;
}
}
}