Message + trying not eat the king

This commit is contained in:
clement 2025-05-13 16:27:50 +02:00
parent 2f0f0c8d91
commit 8b225616fe
1 changed files with 76 additions and 104 deletions

View File

@ -24,25 +24,48 @@ public class Board {
this.pieces = new ArrayList<>();
}
public int getWidth() {
return this.width;
/**
* Constructeur de copie : clone l'état du plateau
*/
public Board(Board other) {
this.width = other.width;
this.height = other.height;
this.pieces = new ArrayList<>();
for (Piece p : other.pieces) {
this.pieces.add(new Piece(p.isWhite(), p.getType(), p.getX(), p.getY()));
}
this.turnNumber = other.turnNumber;
this.turnIsWhite = other.turnIsWhite;
this.gameOver = other.gameOver;
this.selectedX = other.selectedX;
this.selectedY = other.selectedY;
this.highlightedPositions = new ArrayList<>(other.highlightedPositions);
this.moveHistory = new Stack<>();
this.moveHistory.addAll(other.moveHistory);
}
public int getHeight() {
return this.height;
public Board(String[] array) {
this.width = 8;
this.height = 8;
this.pieces = new ArrayList<>();
for (int y = 0; y < 8; y++) {
String[] cells = array[y].split(",");
for (int x = 0; x < 8; x++) {
if (cells[x].length() == 2) {
boolean isWhite = cells[x].charAt(0) == 'w';
PieceType type = PieceType.fromSummary(cells[x].charAt(1));
setPiece(isWhite, type, x, y);
}
}
}
turnIsWhite = array[8].equalsIgnoreCase("W");
}
public int getTurnNumber() {
return turnNumber;
}
public boolean isTurnWhite() {
return turnIsWhite;
}
public boolean isGameOver() {
return gameOver;
}
public int getWidth() { return width; }
public int getHeight() { return 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) {
pieces.removeIf(p -> p.getX() == x && p.getY() == y);
@ -51,24 +74,24 @@ public class Board {
public void populateBoard() {
pieces.clear();
setPiece(true, PieceType.Rook, 0, 7);
setPiece(true, PieceType.Knight, 1, 7);
setPiece(true, PieceType.Bishop, 2, 7);
setPiece(true, PieceType.Queen, 3, 7);
setPiece(true, PieceType.King, 4, 7);
setPiece(true, PieceType.Bishop, 5, 7);
setPiece(true, PieceType.Knight, 6, 7);
setPiece(true, PieceType.Rook, 7, 7);
setPiece(true, PieceType.Rook, 0, 7);
setPiece(true, PieceType.Knight, 1, 7);
setPiece(true, PieceType.Bishop, 2, 7);
setPiece(true, PieceType.Queen, 3, 7);
setPiece(true, PieceType.King, 4, 7);
setPiece(true, PieceType.Bishop, 5, 7);
setPiece(true, PieceType.Knight, 6, 7);
setPiece(true, PieceType.Rook, 7, 7);
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.Bishop, 2, 0);
setPiece(false, PieceType.Queen, 3, 0);
setPiece(false, PieceType.King, 4, 0);
setPiece(false, PieceType.Queen, 3, 0);
setPiece(false, PieceType.King, 4, 0);
setPiece(false, PieceType.Bishop, 5, 0);
setPiece(false, PieceType.Knight, 6, 0);
setPiece(false, PieceType.Rook, 7, 0);
setPiece(false, PieceType.Rook, 7, 0);
for (int x = 0; x < 8; x++) setPiece(false, PieceType.Pawn, x, 1);
}
@ -81,9 +104,7 @@ public class Board {
gameOver = false;
}
public ArrayList<Piece> getPieces() {
return pieces;
}
public ArrayList<Piece> getPieces() { return pieces; }
public void userTouch(int x, int y) {
if (gameOver) return;
@ -99,7 +120,6 @@ public class Board {
} else {
Piece selectedPiece = getPieceAt(selectedX, selectedY);
if (selectedPiece != null && selectedPiece.isWhite() == turnIsWhite) {
// simulate move
Board simulation = new Board(this);
simulation.pieces.removeIf(p -> p.getX() == selectedX && p.getY() == selectedY);
@ -107,20 +127,23 @@ public class Board {
simulation.pieces.add(new Piece(
selectedPiece.isWhite(),
selectedPiece.getType(),
x,
y
x, y
));
// check if move is legal (king remains safe)
if (!simulation.isKingInCheck(turnIsWhite)) {
Piece captured = getPieceAt(x, y);
pieces.removeIf(p -> p.getX() == x && p.getY() == y);
// remove captured only if not a king
if (captured != null && captured.getType() != PieceType.King) {
pieces.removeIf(p -> p.getX() == x && p.getY() == y);
}
// remove the moving piece
pieces.removeIf(p -> p.getX() == selectedX && p.getY() == selectedY);
// place the moved piece
pieces.add(new Piece(
selectedPiece.isWhite(),
selectedPiece.getType(),
x,
y
x, y
));
Move move = new Move(selectedPiece, selectedX, selectedY, x, y, captured);
@ -134,8 +157,6 @@ public class Board {
}
}
}
// deselect in all cases
selectedX = null;
selectedY = null;
highlightedPositions.clear();
@ -143,7 +164,6 @@ public class Board {
printBoard();
}
public void undoLastMove() {
if (!moveHistory.isEmpty()) {
Move lastMove = moveHistory.pop();
@ -151,8 +171,7 @@ public class Board {
pieces.add(new Piece(
lastMove.getMovedPiece().isWhite(),
lastMove.getMovedPiece().getType(),
lastMove.getFromX(),
lastMove.getFromY()
lastMove.getFromX(), lastMove.getFromY()
));
if (lastMove.getCapturedPiece() != null) {
pieces.add(lastMove.getCapturedPiece());
@ -194,7 +213,6 @@ public class Board {
}
}
if (king == null) return true;
for (Piece enemy : pieces) {
if (enemy.isWhite() != isWhite) {
ArrayList<int[]> moves = getLegalMovesFor(enemy);
@ -229,48 +247,41 @@ public class Board {
int x = piece.getX();
int y = piece.getY();
boolean isWhite = piece.isWhite();
switch (piece.getType()) {
case Pawn:
int dir = isWhite ? -1 : 1;
int startRow = isWhite ? 6 : 1;
int oneStepY = y + dir;
int twoStepY = y + 2 * dir;
if (inBounds(x, oneStepY) && getPieceAt(x, oneStepY) == null) {
moves.add(new int[]{x, oneStepY});
if (y == startRow && getPieceAt(x, twoStepY) == null) {
moves.add(new int[]{x, twoStepY});
}
}
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;
case Rook:
addSlideMoves(moves, piece, 1, 0);
addSlideMoves(moves, piece, -1, 0);
addSlideMoves(moves, piece, 0, 1);
addSlideMoves(moves, piece, 0, -1);
break;
case Bishop:
addSlideMoves(moves, piece, 1, 1);
addSlideMoves(moves, piece, 1, -1);
addSlideMoves(moves, piece, -1, 1);
addSlideMoves(moves, piece, -1, -1);
break;
case Queen:
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
@ -280,12 +291,8 @@ public class Board {
}
}
break;
case Knight:
int[][] deltas = {
{2, 1}, {1, 2}, {-1, 2}, {-2, 1},
{-2, -1}, {-1, -2}, {1, -2}, {2, -1}
};
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];
@ -296,7 +303,6 @@ public class Board {
}
}
break;
case King:
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
@ -312,28 +318,21 @@ public class Board {
}
break;
}
return moves;
}
private void addSlideMoves(ArrayList<int[]> moves, Piece piece, int dx, int dy) {
int x = piece.getX();
int y = piece.getY();
boolean isWhite = piece.isWhite();
int nx = x + dx;
int ny = y + dy;
int x = piece.getX(); int y = piece.getY(); boolean isWhite = piece.isWhite();
int nx = x + dx; int ny = y + dy;
while (inBounds(nx, ny)) {
Piece p = getPieceAt(nx, ny);
if (p == null) {
moves.add(new int[]{nx, ny});
} else {
if (p.isWhite() != isWhite)
moves.add(new int[]{nx, ny});
if (p.isWhite() != isWhite) moves.add(new int[]{nx, ny});
break;
}
nx += dx;
ny += dy;
nx += dx; ny += dy;
}
}
@ -356,7 +355,6 @@ public class Board {
top.append((char)('A' + x));
}
output[0] = top.toString();
for (int y = 0; y < height; y++) {
int rank = height - y;
StringBuilder row = new StringBuilder();
@ -368,58 +366,33 @@ public class Board {
row.append(".");
} else {
char c = p.getType().getSummary();
row.append(p.isWhite()
? Character.toUpperCase(c)
: Character.toLowerCase(c));
row.append(p.isWhite() ? Character.toUpperCase(c) : Character.toLowerCase(c));
}
}
output[y + 1] = row.toString();
}
output[height + 1] = "Turn: " + (turnIsWhite ? "White" : "Black");
return output;
}
public void printBoard() {
for (String line : toFileRep()) {
System.out.println(line);
}
}
public Board(String[] array) {
this.width = 8;
this.height = 8;
this.pieces = new ArrayList<>();
for (int y = 0; y < 8; y++) {
String[] cells = array[y].split(",");
for (int x = 0; x < 8; x++) {
if (cells[x].length() == 2) {
boolean isWhite = cells[x].charAt(0) == 'w';
PieceType type = PieceType.fromSummary(cells[x].charAt(1));
setPiece(isWhite, type, x, y);
}
}
}
turnIsWhite = array[8].equalsIgnoreCase("W");
}
public Board(Board board) {
this.width = board.getWidth();
this.height = board.getHeight();
this.pieces = new ArrayList<>(board.getPieces());
this.turnNumber = board.getTurnNumber();
this.turnIsWhite = board.isTurnWhite();
this.moveHistory = new Stack<>();
for (String line : toFileRep()) System.out.println(line);
}
public void playMove(Move move) {
if (move == null || gameOver) return;
pieces.removeIf(p -> p.getX() == move.getToX() && p.getY() == move.getToY());
// remove captured only if not a king
Piece captured = getPieceAt(move.getToX(), move.getToY());
if (captured != null && captured.getType() != PieceType.King) {
pieces.removeIf(p -> p.getX() == move.getToX() && p.getY() == move.getToY());
}
// remove the moving piece
pieces.removeIf(p -> p.getX() == move.getFromX() && p.getY() == move.getFromY());
// add moved piece
pieces.add(new Piece(
move.getMovedPiece().isWhite(),
move.getMovedPiece().getType(),
move.getToX(),
move.getToY()
move.getToX(), move.getToY()
));
moveHistory.push(move);
turnNumber++;
@ -433,4 +406,3 @@ public class Board {
}
}
}
//test