OOP_2A3_Project/src/backend/Move.java

147 lines
5.1 KiB
Java

package backend;
import java.util.ArrayList;
public class Move {
private boolean canCastle(Board boardObj, Piece[][] board, int kingX, int kingY, int rookX, int rookY, boolean isWhite, int width, int height) {
Piece rook = board[rookX][rookY];
if (rook == null || rook.getType() != PieceType.Rook || rook.isWhite() != isWhite || rook.hasMoved()) {
return false;
}
// Pas de pièces entre roi et tour
int dir = (rookX > kingX) ? 1 : -1;
for (int i = kingX + dir; i != rookX; i += dir) {
if (board[i][kingY] != null) {
return false;
}
}
// Le roi ne doit pas être en échec ni traverser une case attaquée
for (int i = 0; i <= 2; i++) {
int stepX = kingX + i * dir;
if (boardObj.isSquareAttacked(stepX, kingY, !isWhite)) {
return false;
}
}
return !boardObj.isInCheck(isWhite);
}
public ArrayList<int[]> getValidMoves(Piece piece, Piece[][] board, int width, int height, int[] enPassantTarget, Board boardObj) {
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 = 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) {
moves.add(new int[]{x, twoStep});
}
}
for (int dx : new int[]{-1, 1}) {
int nx = x + dx;
int ny = y + dir;
if (inBounds(nx, ny, width, height)) {
if (board[nx][ny] != null && board[nx][ny].isWhite() != isWhite) {
moves.add(new int[]{nx, ny});
} 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});
}
}
}
}
}
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});
}
}
}
if (!piece.hasMoved()) {
int row = isWhite ? 0 : 7;
if (canCastle(boardObj, board, x, y, 7, row, isWhite, width, height)) {
moves.add(new int[]{x + 2, y});
}
if (canCastle(boardObj, board, x, y, 0, row, isWhite, width, height)) {
moves.add(new int[]{x - 2, y});
}
}
}
return moves;
}
private boolean inBounds(int x, int y, int width, int height) {
return x >= 0 && x < width && y >= 0 && y < height;
}
}