The castleing worksssssss

This commit is contained in:
Bastien Druillette 2025-05-13 17:21:16 +02:00
parent cfad9cd39e
commit 826ab91069
3 changed files with 188 additions and 83 deletions

View File

@ -144,7 +144,8 @@ public class Board {
selectedX = x;
selectedY = y;
hasSelectedPiece = true;
highlightedSquares = moveHelper.getValidMoves(clicked, board, width, height, enPassantTarget);
highlightedSquares = moveHelper.getValidMoves(clicked, board, width, height, enPassantTarget, this);
}
} else {
// Check if clicked again on the same square to unselect
@ -168,6 +169,28 @@ public class Board {
board[selectedX][selectedY] = null;
selectedPiece.setX(x);
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 (isEnPassant) {
@ -232,6 +255,36 @@ public class Board {
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() {
// TODO
}

View File

@ -4,7 +4,33 @@ import java.util.ArrayList;
public class Move {
public ArrayList<int[]> getValidMoves(Piece piece, Piece[][] board, int width, int height, int[] enPassantTarget) {
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();
@ -42,8 +68,10 @@ 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}};
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)) {
@ -75,13 +103,17 @@ public class Move {
}
break;
}
nx += d[0]; ny += d[1];
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}};
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)) {
@ -91,12 +123,23 @@ public class Move {
}
}
// Tu peux ajouter ici le roque (castling) si tu veux, mais tu dois gérer hasMoved, menace, etc.
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;
}

View File

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