Merge pull request 'The castleing worksssssss' (#1) from castling-saved into master

Reviewed-on: #1
This commit is contained in:
Bastien DRUILLETTE 2025-05-14 07:52:16 +02:00
commit bcbb48dbd5
3 changed files with 188 additions and 83 deletions

View File

@ -144,7 +144,8 @@ public class Board {
selectedX = x; selectedX = x;
selectedY = y; selectedY = y;
hasSelectedPiece = true; hasSelectedPiece = true;
highlightedSquares = moveHelper.getValidMoves(clicked, board, width, height, enPassantTarget); highlightedSquares = moveHelper.getValidMoves(clicked, board, width, height, enPassantTarget, this);
} }
} else { } else {
// Check if clicked again on the same square to unselect // Check if clicked again on the same square to unselect
@ -168,6 +169,28 @@ public class Board {
board[selectedX][selectedY] = null; board[selectedX][selectedY] = null;
selectedPiece.setX(x); selectedPiece.setX(x);
selectedPiece.setY(y); selectedPiece.setY(y);
selectedPiece.setMoved(true); // Marque la pièce comme ayant bougé
// Déplacement de la tour si roque
if (selectedPiece.getType() == PieceType.King && Math.abs(x - selectedX) == 2) {
if (x > selectedX) {
// Petit roque (côté roi)
Piece rook = board[7][y];
board[5][y] = rook;
board[7][y] = null;
rook.setX(5);
rook.setMoved(true);
} else {
// Grand roque (côté dame)
Piece rook = board[0][y];
board[3][y] = rook;
board[0][y] = null;
rook.setX(3);
rook.setMoved(true);
}
}
// If en passant, remove the captured pawn // If en passant, remove the captured pawn
if (isEnPassant) { if (isEnPassant) {
@ -253,6 +276,36 @@ public class Board {
} }
return false; return false;
} }
public boolean isInCheck(boolean white) {
// Trouver le roi
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
Piece p = board[x][y];
if (p != null && p.getType() == PieceType.King && p.isWhite() == white) {
return isSquareAttacked(x, y, !white);
}
}
}
return false;
}
public boolean isSquareAttacked(int x, int y, boolean byWhite) {
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
Piece p = board[i][j];
if (p != null && p.isWhite() == byWhite) {
ArrayList<int[]> moves = moveHelper.getValidMoves(p, board, width, height, enPassantTarget, this);
for (int[] m : moves) {
if (m[0] == x && m[1] == y) {
return true;
}
}
}
}
}
return false;
}
public void undoLastMove() { public void undoLastMove() {
// TODO // TODO

View File

@ -3,99 +3,142 @@ package backend;
import java.util.ArrayList; import java.util.ArrayList;
public class Move { 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;
}
public ArrayList<int[]> getValidMoves(Piece piece, Piece[][] board, int width, int height, int[] enPassantTarget) { // Pas de pièces entre roi et tour
ArrayList<int[]> moves = new ArrayList<>(); int dir = (rookX > kingX) ? 1 : -1;
int x = piece.getX(); for (int i = kingX + dir; i != rookX; i += dir) {
int y = piece.getY(); if (board[i][kingY] != null) {
PieceType type = piece.getType(); return false;
boolean isWhite = piece.isWhite(); }
}
if (type == PieceType.Pawn) { // Le roi ne doit pas être en échec ni traverser une case attaquée
int dir = isWhite ? 1 : -1; for (int i = 0; i <= 2; i++) {
int startRow = isWhite ? 1 : 6; int stepX = kingX + i * dir;
if (boardObj.isSquareAttacked(stepX, kingY, !isWhite)) {
return false;
}
}
int oneStep = y + dir; return !boardObj.isInCheck(isWhite);
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}) { public ArrayList<int[]> getValidMoves(Piece piece, Piece[][] board, int width, int height, int[] enPassantTarget, Board boardObj) {
int nx = x + dx; ArrayList<int[]> moves = new ArrayList<>();
int ny = y + dir; int x = piece.getX();
if (inBounds(nx, ny, width, height)) { int y = piece.getY();
if (board[nx][ny] != null && board[nx][ny].isWhite() != isWhite) { PieceType type = piece.getType();
moves.add(new int[]{nx, ny}); boolean 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});
}
}
}
}
}
else if (type == PieceType.Knight) { if (type == PieceType.Pawn) {
int[][] jumps = {{2, 1}, {1, 2}, {-1, 2}, {-2, 1}, int dir = isWhite ? 1 : -1;
{-2, -1}, {-1, -2}, {1, -2}, {2, -1}}; int startRow = isWhite ? 1 : 6;
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 oneStep = y + dir;
int[][] dirs; if (inBounds(x, oneStep, width, height) && board[x][oneStep] == null) {
if (type == PieceType.Bishop) { moves.add(new int[]{x, oneStep});
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 twoStep = y + 2 * dir;
int nx = x + d[0], ny = y + d[1]; if (y == startRow && inBounds(x, twoStep, width, height) && board[x][twoStep] == null && board[x][oneStep] == null) {
while (inBounds(nx, ny, width, height)) { moves.add(new int[]{x, twoStep});
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) { for (int dx : new int[]{-1, 1}) {
int[][] dirs = {{1,0}, {-1,0}, {0,1}, {0,-1}, {1,1}, {-1,1}, {-1,-1}, {1,-1}}; int nx = x + dx;
for (int[] d : dirs) { int ny = y + dir;
int nx = x + d[0], ny = y + d[1]; if (inBounds(nx, ny, width, height)) {
if (inBounds(nx, ny, width, height)) { if (board[nx][ny] != null && board[nx][ny].isWhite() != 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) {
} 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});
}
}
}
}
}
// Tu peux ajouter ici le roque (castling) si tu veux, mais tu dois gérer hasMoved, menace, etc. 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;
}
return moves;
}
private boolean inBounds(int x, int y, int width, int height) { private boolean inBounds(int x, int y, int width, int height) {
return x >= 0 && x < width && y >= 0 && y < height; return x >= 0 && x < width && y >= 0 && y < height;

View File

@ -82,5 +82,14 @@ public boolean isWhite() {
return isWhite; return isWhite;
}
private boolean hasMoved = false;
public boolean hasMoved() {
return hasMoved;
}
public void setMoved(boolean moved) {
this.hasMoved = moved;
} }
} }