The castleing worksssssss #1
|
|
@ -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) {
|
||||||
|
|
@ -232,6 +255,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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,98 +4,141 @@ import java.util.ArrayList;
|
||||||
|
|
||||||
public class Move {
|
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) {
|
||||||
ArrayList<int[]> moves = new ArrayList<>();
|
Piece rook = board[rookX][rookY];
|
||||||
int x = piece.getX();
|
if (rook == null || rook.getType() != PieceType.Rook || rook.isWhite() != isWhite || rook.hasMoved()) {
|
||||||
int y = piece.getY();
|
return false;
|
||||||
PieceType type = piece.getType();
|
}
|
||||||
boolean isWhite = piece.isWhite();
|
|
||||||
|
|
||||||
if (type == PieceType.Pawn) {
|
// Pas de pièces entre roi et tour
|
||||||
int dir = isWhite ? 1 : -1;
|
int dir = (rookX > kingX) ? 1 : -1;
|
||||||
int startRow = isWhite ? 1 : 6;
|
for (int i = kingX + dir; i != rookX; i += dir) {
|
||||||
|
if (board[i][kingY] != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int oneStep = y + dir;
|
// Le roi ne doit pas être en échec ni traverser une case attaquée
|
||||||
if (inBounds(x, oneStep, width, height) && board[x][oneStep] == null) {
|
for (int i = 0; i <= 2; i++) {
|
||||||
moves.add(new int[]{x, oneStep});
|
int stepX = kingX + i * dir;
|
||||||
|
if (boardObj.isSquareAttacked(stepX, kingY, !isWhite)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int twoStep = y + 2 * dir;
|
return !boardObj.isInCheck(isWhite);
|
||||||
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) {
|
public ArrayList<int[]> getValidMoves(Piece piece, Piece[][] board, int width, int height, int[] enPassantTarget, Board boardObj) {
|
||||||
int[][] jumps = {{2, 1}, {1, 2}, {-1, 2}, {-2, 1},
|
ArrayList<int[]> moves = new ArrayList<>();
|
||||||
{-2, -1}, {-1, -2}, {1, -2}, {2, -1}};
|
int x = piece.getX();
|
||||||
for (int[] j : jumps) {
|
int y = piece.getY();
|
||||||
int nx = x + j[0], ny = y + j[1];
|
PieceType type = piece.getType();
|
||||||
if (inBounds(nx, ny, width, height)) {
|
boolean isWhite = piece.isWhite();
|
||||||
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) {
|
if (type == PieceType.Pawn) {
|
||||||
int[][] dirs;
|
int dir = isWhite ? 1 : -1;
|
||||||
if (type == PieceType.Bishop) {
|
int startRow = isWhite ? 1 : 6;
|
||||||
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 oneStep = y + dir;
|
||||||
int nx = x + d[0], ny = y + d[1];
|
if (inBounds(x, oneStep, width, height) && board[x][oneStep] == null) {
|
||||||
while (inBounds(nx, ny, width, height)) {
|
moves.add(new int[]{x, oneStep});
|
||||||
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 twoStep = y + 2 * dir;
|
||||||
int[][] dirs = {{1,0}, {-1,0}, {0,1}, {0,-1}, {1,1}, {-1,1}, {-1,-1}, {1,-1}};
|
if (y == startRow && inBounds(x, twoStep, width, height) && board[x][twoStep] == null && board[x][oneStep] == null) {
|
||||||
for (int[] d : dirs) {
|
moves.add(new int[]{x, twoStep});
|
||||||
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.
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue