This commit is contained in:
bensefia 2025-05-13 15:44:47 +02:00
parent 6820b2a84c
commit ce8842c10c
1 changed files with 166 additions and 123 deletions

View File

@ -10,6 +10,7 @@ public class Board {
private ArrayList<Piece> pieces; private ArrayList<Piece> pieces;
private int turnNumber = 0; private int turnNumber = 0;
private boolean turnIsWhite = true; private boolean turnIsWhite = true;
private boolean gameOver = false;
private Integer selectedX = null; private Integer selectedX = null;
private Integer selectedY = null; private Integer selectedY = null;
@ -17,20 +18,31 @@ public class Board {
private Stack<Move> moveHistory = new Stack<>(); private Stack<Move> moveHistory = new Stack<>();
// Indique si la partie est terminée (échec et mat)
private boolean gameOver = false;
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
this.width = colNum; this.width = colNum;
this.height = lineNum; this.height = lineNum;
this.pieces = new ArrayList<>(); this.pieces = new ArrayList<>();
} }
public int getWidth() { return width; } public int getWidth() {
public int getHeight() { return height; } return this.width;
public int getTurnNumber() { return turnNumber; } }
public boolean isTurnWhite() { return turnIsWhite; }
public boolean isGameOver() { return gameOver; } public int getHeight() {
return this.height;
}
public int getTurnNumber() {
return turnNumber;
}
public boolean isTurnWhite() {
return turnIsWhite;
}
public boolean isGameOver() {
return gameOver;
}
public void setPiece(boolean isWhite, PieceType type, int x, int y) { public void setPiece(boolean isWhite, PieceType type, int x, int y) {
pieces.removeIf(p -> p.getX() == x && p.getY() == y); pieces.removeIf(p -> p.getX() == x && p.getY() == y);
@ -38,7 +50,6 @@ public class Board {
} }
public void populateBoard() { public void populateBoard() {
gameOver = false;
pieces.clear(); pieces.clear();
setPiece(true, PieceType.Rook, 0, 7); setPiece(true, PieceType.Rook, 0, 7);
setPiece(true, PieceType.Knight, 1, 7); setPiece(true, PieceType.Knight, 1, 7);
@ -49,6 +60,7 @@ public class Board {
setPiece(true, PieceType.Knight, 6, 7); setPiece(true, PieceType.Knight, 6, 7);
setPiece(true, PieceType.Rook, 7, 7); setPiece(true, PieceType.Rook, 7, 7);
for (int x = 0; x < 8; x++) setPiece(true, PieceType.Pawn, x, 6); for (int x = 0; x < 8; x++) setPiece(true, PieceType.Pawn, x, 6);
setPiece(false, PieceType.Rook, 0, 0); setPiece(false, PieceType.Rook, 0, 0);
setPiece(false, PieceType.Knight, 1, 0); setPiece(false, PieceType.Knight, 1, 0);
setPiece(false, PieceType.Bishop, 2, 0); setPiece(false, PieceType.Bishop, 2, 0);
@ -61,15 +73,12 @@ public class Board {
} }
public void cleanBoard() { public void cleanBoard() {
gameOver = false;
pieces.clear(); pieces.clear();
selectedX = null; selectedX = null;
selectedY = null; selectedY = null;
highlightedPositions.clear(); highlightedPositions.clear();
if (isInCheck(turnIsWhite)) {
System.out.println("Échec !");
}
moveHistory.clear(); moveHistory.clear();
gameOver = false;
} }
public ArrayList<Piece> getPieces() { public ArrayList<Piece> getPieces() {
@ -78,6 +87,7 @@ public class Board {
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
if (gameOver) return; if (gameOver) return;
Piece clickedPiece = getPieceAt(x, y); Piece clickedPiece = getPieceAt(x, y);
if (selectedX == null || selectedY == null) { if (selectedX == null || selectedY == null) {
@ -89,7 +99,6 @@ public class Board {
} else { } else {
Piece selectedPiece = getPieceAt(selectedX, selectedY); Piece selectedPiece = getPieceAt(selectedX, selectedY);
if (selectedPiece != null && selectedPiece.isWhite() == turnIsWhite) { if (selectedPiece != null && selectedPiece.isWhite() == turnIsWhite) {
<<<<<<< HEAD
// simulate move // simulate move
Board simulation = new Board(this); Board simulation = new Board(this);
@ -123,24 +132,6 @@ public class Board {
if (isCheckmate(!turnIsWhite)) { if (isCheckmate(!turnIsWhite)) {
gameOver = true; gameOver = true;
} }
=======
Piece captured = getPieceAt(x, y);
// Détection de la capture du roi (sécuritaire)
if (captured != null && captured.getType() == PieceType.King) {
gameOver = true;
System.out.println("Game over: King captured.");
}
pieces.removeIf(p -> p.getX() == x && p.getY() == y);
pieces.removeIf(p -> p.getX() == selectedX && p.getY() == selectedY);
pieces.add(new Piece(selectedPiece.isWhite(), selectedPiece.getType(), x, y));
moveHistory.push(new Move(selectedPiece, selectedX, selectedY, x, y, captured));
turnNumber++;
turnIsWhite = !turnIsWhite;
// Détection d'échec et mat
if (isInCheck(turnIsWhite) && !hasLegalMoves(turnIsWhite)) {
gameOver = true;
System.out.println("Game over: Checkmate.");
>>>>>>> branch 'master' of https://gitarero.ecam.fr/ilian.bensefia/OOP_2B6_PROJECT.git
} }
} }
@ -153,17 +144,67 @@ public class Board {
public void undoLastMove() { public void undoLastMove() {
if (moveHistory.isEmpty()) return; if (!moveHistory.isEmpty()) {
Move lastMove = moveHistory.pop(); Move lastMove = moveHistory.pop();
pieces.removeIf(p -> p.getX() == lastMove.getToX() && p.getY() == lastMove.getToY()); pieces.removeIf(p -> p.getX() == lastMove.getToX() && p.getY() == lastMove.getToY());
pieces.add(new Piece(lastMove.getMovedPiece().isWhite(), lastMove.getMovedPiece().getType(), lastMove.getFromX(), lastMove.getFromY())); pieces.add(new Piece(
if (lastMove.getCapturedPiece() != null) pieces.add(lastMove.getCapturedPiece()); lastMove.getMovedPiece().isWhite(),
turnNumber--; lastMove.getMovedPiece().getType(),
turnIsWhite = !turnIsWhite; lastMove.getFromX(),
selectedX = null; lastMove.getFromY()
selectedY = null; ));
highlightedPositions.clear(); if (lastMove.getCapturedPiece() != null) {
gameOver = false; pieces.add(lastMove.getCapturedPiece());
}
turnNumber--;
turnIsWhite = !turnIsWhite;
selectedX = null;
selectedY = null;
highlightedPositions.clear();
gameOver = false;
}
}
public boolean isCheckmate(boolean forWhite) {
for (Piece piece : pieces) {
if (piece.isWhite() == forWhite) {
ArrayList<int[]> moves = getLegalMovesFor(piece);
for (int[] move : moves) {
Board copy = new Board(this);
Piece captured = copy.getPieceAt(move[0], move[1]);
copy.pieces.removeIf(p -> p.getX() == piece.getX() && p.getY() == piece.getY());
copy.pieces.removeIf(p -> p.getX() == move[0] && p.getY() == move[1]);
copy.pieces.add(new Piece(piece.isWhite(), piece.getType(), move[0], move[1]));
if (!copy.isKingInCheck(forWhite)) {
return false;
}
}
}
}
return isKingInCheck(forWhite);
}
public boolean isKingInCheck(boolean isWhite) {
Piece king = null;
for (Piece p : pieces) {
if (p.getType() == PieceType.King && p.isWhite() == isWhite) {
king = p;
break;
}
}
if (king == null) return true;
for (Piece enemy : pieces) {
if (enemy.isWhite() != isWhite) {
ArrayList<int[]> moves = getLegalMovesFor(enemy);
for (int[] pos : moves) {
if (pos[0] == king.getX() && pos[1] == king.getY()) {
return true;
}
}
}
}
return false;
} }
public boolean isSelected(int x, int y) { public boolean isSelected(int x, int y) {
@ -171,7 +212,9 @@ public class Board {
} }
public boolean isHighlighted(int x, int y) { public boolean isHighlighted(int x, int y) {
for (int[] pos : highlightedPositions) if (pos[0] == x && pos[1] == y) return true; for (int[] pos : highlightedPositions) {
if (pos[0] == x && pos[1] == y) return true;
}
return false; return false;
} }
@ -182,77 +225,110 @@ public class Board {
private ArrayList<int[]> getLegalMovesFor(Piece piece) { private ArrayList<int[]> getLegalMovesFor(Piece piece) {
ArrayList<int[]> moves = new ArrayList<>(); ArrayList<int[]> moves = new ArrayList<>();
int x = piece.getX(), y = piece.getY(); int x = piece.getX();
int y = piece.getY();
boolean isWhite = piece.isWhite(); boolean isWhite = piece.isWhite();
switch (piece.getType()) { switch (piece.getType()) {
case Pawn: case Pawn:
int dir = isWhite ? -1 : 1; int dir = isWhite ? -1 : 1;
int start = isWhite ? 6 : 1; int startRow = isWhite ? 6 : 1;
if (inBounds(x, y + dir) && getPieceAt(x, y + dir) == null) { int oneStepY = y + dir;
moves.add(new int[]{x, y + dir}); int twoStepY = y + 2 * dir;
if (y == start && inBounds(x, y + 2 * dir) && getPieceAt(x, y + 2 * dir) == null)
moves.add(new int[]{x, y + 2 * dir}); if (inBounds(x, oneStepY) && getPieceAt(x, oneStepY) == null) {
} moves.add(new int[]{x, oneStepY});
for (int dx : new int[]{-1, 1}) { if (y == startRow && getPieceAt(x, twoStepY) == null) {
if (inBounds(x + dx, y + dir)) { moves.add(new int[]{x, twoStepY});
Piece p = getPieceAt(x + dx, y + dir);
if (p != null && p.isWhite() != isWhite) moves.add(new int[]{x + dx, y + dir});
} }
} }
if (inBounds(x - 1, oneStepY)) {
Piece left = getPieceAt(x - 1, oneStepY);
if (left != null && left.isWhite() != isWhite)
moves.add(new int[]{x - 1, oneStepY});
}
if (inBounds(x + 1, oneStepY)) {
Piece right = getPieceAt(x + 1, oneStepY);
if (right != null && right.isWhite() != isWhite)
moves.add(new int[]{x + 1, oneStepY});
}
break; break;
case Rook: case Rook:
addSlideMoves(moves, piece, 1, 0); addSlideMoves(moves, piece, 1, 0);
addSlideMoves(moves, piece, -1, 0); addSlideMoves(moves, piece, -1, 0);
addSlideMoves(moves, piece, 0, 1); addSlideMoves(moves, piece, 0, 1);
addSlideMoves(moves, piece, 0, -1); addSlideMoves(moves, piece, 0, -1);
break; break;
case Bishop: case Bishop:
addSlideMoves(moves, piece, 1, 1); addSlideMoves(moves, piece, 1, 1);
addSlideMoves(moves, piece, 1, -1); addSlideMoves(moves, piece, 1, -1);
addSlideMoves(moves, piece, -1, 1); addSlideMoves(moves, piece, -1, 1);
addSlideMoves(moves, piece, -1, -1); addSlideMoves(moves, piece, -1, -1);
break; break;
case Queen: case Queen:
for (int dx = -1; dx <= 1; dx++) for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) for (int dy = -1; dy <= 1; dy++) {
if (dx != 0 || dy != 0) if (dx != 0 || dy != 0) {
addSlideMoves(moves, piece, dx, dy); addSlideMoves(moves, piece, dx, dy);
break; }
case Knight:
int[][] d = {{2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}};
for (int[] dd : d) {
int nx = x + dd[0], ny = y + dd[1];
if (inBounds(nx, ny)) {
Piece p = getPieceAt(nx, ny);
if (p == null || p.isWhite() != isWhite) moves.add(new int[]{nx, ny});
} }
} }
break; break;
case Knight:
int[][] deltas = {
{2, 1}, {1, 2}, {-1, 2}, {-2, 1},
{-2, -1}, {-1, -2}, {1, -2}, {2, -1}
};
for (int[] d : deltas) {
int nx = x + d[0];
int ny = y + d[1];
if (inBounds(nx, ny)) {
Piece p = getPieceAt(nx, ny);
if (p == null || p.isWhite() != isWhite)
moves.add(new int[]{nx, ny});
}
}
break;
case King: case King:
for (int dx = -1; dx <= 1; dx++) for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) { for (int dy = -1; dy <= 1; dy++) {
if (dx == 0 && dy == 0) continue; if (dx == 0 && dy == 0) continue;
int nx = x + dx, ny = y + dy; int nx = x + dx;
int ny = y + dy;
if (inBounds(nx, ny)) { if (inBounds(nx, ny)) {
Piece p = getPieceAt(nx, ny); Piece p = getPieceAt(nx, ny);
if (p == null || p.isWhite() != isWhite) moves.add(new int[]{nx, ny}); if (p == null || p.isWhite() != isWhite)
moves.add(new int[]{nx, ny});
} }
} }
}
break; break;
} }
return moves; return moves;
} }
private void addSlideMoves(ArrayList<int[]> moves, Piece piece, int dx, int dy) { private void addSlideMoves(ArrayList<int[]> moves, Piece piece, int dx, int dy) {
int x = piece.getX(), y = piece.getY(); int x = piece.getX();
boolean white = piece.isWhite(); int y = piece.getY();
int nx = x + dx, ny = y + dy; boolean isWhite = piece.isWhite();
int nx = x + dx;
int ny = y + dy;
while (inBounds(nx, ny)) { while (inBounds(nx, ny)) {
Piece p = getPieceAt(nx, ny); Piece p = getPieceAt(nx, ny);
if (p == null) { if (p == null) {
moves.add(new int[]{nx, ny}); moves.add(new int[]{nx, ny});
} else { } else {
if (p.isWhite() != white) moves.add(new int[]{nx, ny}); if (p.isWhite() != isWhite)
moves.add(new int[]{nx, ny});
break; break;
} }
nx += dx; nx += dx;
@ -264,44 +340,13 @@ public class Board {
return x >= 0 && y >= 0 && x < width && y < height; return x >= 0 && y >= 0 && x < width && y < height;
} }
private Piece getPieceAt(int x, int y) { public Piece getPieceAt(int x, int y) {
for (Piece p : pieces) if (p.getX() == x && p.getY() == y) return p; for (Piece p : pieces) {
if (p.getX() == x && p.getY() == y) return p;
}
return null; return null;
} }
private boolean isInCheck(boolean whiteKing) {
Piece king = null;
for (Piece p : pieces) if (p.getType() == PieceType.King && p.isWhite() == whiteKing) { king = p; break; }
if (king == null) return false;
int kx = king.getX(), ky = king.getY();
for (Piece p : pieces) {
if (p.isWhite() != whiteKing) {
for (int[] m : getLegalMovesFor(p)) if (m[0] == kx && m[1] == ky) return true;
}
}
return false;
}
public boolean hasLegalMoves(boolean white) {
for (Piece p : pieces) {
if (p.isWhite() == white) {
for (int[] m : getLegalMovesFor(p)) {
Board copy = new Board(this);
Piece captured = copy.getPieceAt(m[0], m[1]);
copy.pieces.removeIf(pc -> pc.getX() == m[0] && pc.getY() == m[1]);
copy.pieces.removeIf(pc -> pc.getX() == p.getX() && pc.getY() == p.getY());
copy.pieces.add(new Piece(p.isWhite(), p.getType(), m[0], m[1]));
if (!copy.isInCheck(white)) return true;
}
}
}
return false;
}
public boolean isCheckmate(boolean whiteKing) {
return isInCheck(whiteKing) && !hasLegalMoves(whiteKing);
}
public String[] toFileRep() { public String[] toFileRep() {
String[] output = new String[height + 1]; String[] output = new String[height + 1];
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
@ -309,7 +354,11 @@ public class Board {
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++) {
Piece p = getPieceAt(x, y); Piece p = getPieceAt(x, y);
if (x > 0) row.append(","); if (x > 0) row.append(",");
row.append(p == null ? "" : (p.isWhite() ? "w" : "b") + p.getType().getSummary()); if (p == null) {
row.append("");
} else {
row.append(p.isWhite() ? "w" : "b").append(p.getType().getSummary());
}
} }
output[y] = row.toString(); output[y] = row.toString();
} }
@ -332,7 +381,6 @@ public class Board {
} }
} }
turnIsWhite = array[8].equalsIgnoreCase("W"); turnIsWhite = array[8].equalsIgnoreCase("W");
gameOver = false;
} }
public Board(Board board) { public Board(Board board) {
@ -342,32 +390,27 @@ public class Board {
this.turnNumber = board.getTurnNumber(); this.turnNumber = board.getTurnNumber();
this.turnIsWhite = board.isTurnWhite(); this.turnIsWhite = board.isTurnWhite();
this.moveHistory = new Stack<>(); this.moveHistory = new Stack<>();
this.gameOver = board.isGameOver();
} }
public void playMove(Move move) { public void playMove(Move move) {
if (move == null || gameOver) return; if (move == null || gameOver) return;
Piece captured = move.getCapturedPiece();
if (captured != null && captured.getType() == PieceType.King) {
gameOver = true;
System.out.println("Game over: King captured.");
}
pieces.removeIf(p -> p.getX() == move.getToX() && p.getY() == move.getToY()); pieces.removeIf(p -> p.getX() == move.getToX() && p.getY() == move.getToY());
pieces.removeIf(p -> p.getX() == move.getFromX() && p.getY() == move.getFromY()); pieces.removeIf(p -> p.getX() == move.getFromX() && p.getY() == move.getFromY());
pieces.add(new Piece(move.getMovedPiece().isWhite(), move.getMovedPiece().getType(), move.getToX(), move.getToY())); pieces.add(new Piece(
move.getMovedPiece().isWhite(),
move.getMovedPiece().getType(),
move.getToX(),
move.getToY()
));
moveHistory.push(move); moveHistory.push(move);
turnNumber++; turnNumber++;
turnIsWhite = !turnIsWhite; turnIsWhite = !turnIsWhite;
selectedX = null; selectedX = null;
selectedY = null; selectedY = null;
highlightedPositions.clear(); highlightedPositions.clear();
if (isInCheck(turnIsWhite) && !hasLegalMoves(turnIsWhite)) {
if (isCheckmate(!turnIsWhite)) {
gameOver = true; gameOver = true;
System.out.println("Game over: Checkmate.");
} }
} }
} }
<<<<<<< HEAD
// loadhg
=======
>>>>>>> branch 'master' of https://gitarero.ecam.fr/ilian.bensefia/OOP_2B6_PROJECT.git