This commit is contained in:
clara 2025-05-13 14:56:00 +02:00
commit eaba4bb370
6 changed files with 118 additions and 62 deletions

View File

@ -4,9 +4,7 @@ import backend.Piece;
import backend.PieceType;
import windowInterface.MyInterface;
public class Main {

View File

@ -34,6 +34,11 @@ public class Board {
public boolean isTurnWhite() {
return this.isWhiteTurn;
}
public void resetTurn() {
this.turnNumber = 0;
this.isWhiteTurn = true;
}
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
Piece newPiece = new Piece(x, y, isWhite, type);
@ -180,18 +185,17 @@ public class Board {
selectedY = -1;
highlightedSquares.clear();
if (isKingInCheck(true)) {
System.out.println("White is in check!");
}
if (isKingInCheck(false)) {
System.out.println("Black is in check!");
}
if (isCheckmate(true)) {
System.out.println("White is in checkmate!");
}
if (isCheckmate(false)) {
System.out.println("Black is in checkmate!");
// After move completed, check for check and checkmate
for (int i = 0; i < 2; i++) {
boolean isWhite = (i == 0);
if (isKingInCheck(isWhite)) {
System.out.println((isWhite ? "White" : "Black") + " is in check!");
if (isCheckmate(isWhite)) {
System.out.println((isWhite ? "White" : "Black") + " is in checkmate!");
}
}
}
}
@ -341,59 +345,100 @@ public class Board {
boolean kingInCheck = isKingInCheck(whiteKing);
boolean hasEscape = false;
if (kingInCheck) {
for (int i = 0; i < pieces.size(); i++) {
Piece piece = pieces.get(i);
// 1. If the king is not in check, it's never checkmate
if (!kingInCheck) {
return false;
}
if (piece.isWhite() == whiteKing) {
ArrayList<int[]> rawMoves = getValidMoves(piece);
// 2. Try every possible move of every piece belonging to the checked side
for (int i = 0; i < pieces.size(); i++) {
Piece piece = pieces.get(i);
for (int j = 0; j < rawMoves.size(); j++) {
int[] move = rawMoves.get(j);
int newX = move[0];
int newY = move[1];
if (piece.isWhite() == whiteKing) {
ArrayList<int[]> rawMoves = getValidMoves(piece);
// Simulate the board
Board simBoard = new Board(this);
for (int j = 0; j < rawMoves.size(); j++) {
int[] move = rawMoves.get(j);
int newX = move[0];
int newY = move[1];
// Find the same piece in the simulated board
Piece simPiece = null;
for (int k = 0; k < simBoard.getPieces().size(); k++) {
Piece p = simBoard.getPieces().get(k);
if (p.getX() == piece.getX() && p.getY() == piece.getY()
&& p.getType() == piece.getType()
&& p.isWhite() == piece.isWhite()) {
simPiece = p;
}
// 3. Simulate this move on a copied board
Board simBoard = new Board(this);
// 4. Find the corresponding piece on the cloned board
Piece simPiece = null;
for (int k = 0; k < simBoard.getPieces().size(); k++) {
Piece p = simBoard.getPieces().get(k);
if (p.getX() == piece.getX() && p.getY() == piece.getY()
&& p.getType() == piece.getType()
&& p.isWhite() == piece.isWhite()) {
simPiece = p;
}
}
// 5. Apply the move and check if king is still in check
if (simPiece != null) {
Piece captured = simBoard.getPieceAt(newX, newY);
if (captured != null) {
simBoard.getPieces().remove(captured);
}
if (simPiece != null) {
Piece captured = simBoard.getPieceAt(newX, newY);
if (captured != null) {
simBoard.getPieces().remove(captured);
}
simPiece.setX(newX);
simPiece.setY(newY);
simPiece.setX(newX);
simPiece.setY(newY);
boolean stillInCheck = simBoard.isKingInCheck(whiteKing);
if (!stillInCheck) {
hasEscape = true;
}
if (!simBoard.isKingInCheck(whiteKing)) {
hasEscape = true;
System.out.println("ESCAPE FOUND: " + piece.getType() + " from (" + piece.getX() + "," + piece.getY() + ") to (" + newX + "," + newY + ")");
}
}
}
}
}
boolean checkmate = false;
if (kingInCheck && !hasEscape) {
checkmate = true;
}
return checkmate;
// 6. If the king is in check and no move avoids it checkmate
return kingInCheck && !hasEscape;
}
/*
private void enPassant(Board board, List<Move> moves) {
int x = this.x;
int y = this.y;
if (isWhite() == true && this.y == 3) {
if(isEnPassant(board, new Piece(x - 1, y)))
moves.add(new Move(this, new Piece(x - 1, y - 1),
board.getPieceAt(new Piece(x - 1, y))));
if(canCaptureEnPassant(board, new Piece(x + 1, y)))
moves.add(new Move(this, new Piece(x + 1, y - 1),
board.getPieceAt(new Piece(x + 1, y))));
}
if (isWhite() == false && this.y == 4) {
if(isEnPassant(board, new Piece(x - 1, y)))
moves.add(new Move(this, new Piece(x - 1, y + 1),
board.getPieceAt(new Piece(x - 1, y))));
if(canCaptureEnPassant(board, new Piece(x + 1, y)))
moves.add(new Move(this, new Piece(x + 1, y + 1),
board.getPieceAt(new Piece(x + 1, y))));
}
}
* Checks if the pawn can capture another pawn by en passant
* @param pt location of the other pawn
* @return true if can be captured
private boolean isEnPassant(Board board, Point pt) {
Piece temp = board.getPieceAt(pt);
if(temp != null) {
if (temp instanceof Pawn && temp.getColor() != this.color)
if (((Pawn)temp).enPassantOk)
return true;
return false;
}
*/
}

View File

@ -83,6 +83,7 @@ public class Game extends Thread {
public void setDefaultSetup() {
board.cleanBoard();
board.populateBoard();
board.resetTurn();
}
public void setBoard(String[] array) {

View File

@ -181,7 +181,7 @@ public class MoveConditions {
if (target == null || target.isWhite() != isWhite) {
Board simBoard = new Board(board);
// Find this same King in the simulated board
// Safely locate the cloned king
Piece simKing = null;
for (int j = 0; j < simBoard.getPieces().size(); j++) {
Piece p = simBoard.getPieces().get(j);
@ -201,8 +201,7 @@ public class MoveConditions {
simKing.setX(newX);
simKing.setY(newY);
boolean stillInCheck = simBoard.isKingInCheck(isWhite);
if (!stillInCheck) {
if (!simBoard.isKingInCheck(isWhite)) {
moves.add(new int[]{newX, newY});
}
}
@ -212,6 +211,7 @@ public class MoveConditions {
return moves;
}
}

View File

@ -1,4 +1,4 @@
package backend;
package backend;
public class Piece {
private int x;

View File

@ -1,6 +1,12 @@
/*package backend;
public class SpecialMoves {
<<<<<<< HEAD
private Piece piece;
private Board board;
private Piece piece;
private Board board;
private int x = piece.getX();
@ -21,12 +27,14 @@ public class SpecialMoves {
int[][] offsets = {{1, 2}, {-1, 2}};
}*/
/* public boolean checkCoordinates(int x, int y, PieceType type, boolean isWhite) {
if (type == PieceType.Pawn || isWhite == true || y == 3) {
>>>>>>> branch 'master' of https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git
int[][] offsets = {{1, 0}, {-1, 0}};
}
if (type == PieceType.Pawn || isWhite == false || y == 4) {
if (type == PieceType.Pawn && isWhite == false && y == 4) {
int[][] offsets = {{1, 0}, {-1, 0}};
}
@ -36,13 +44,17 @@ public class SpecialMoves {
public boolean isEnpassant() {
boolean isWhite = piece.isWhite();
<<<<<<< HEAD
if (boolean isWhite == true && PieceType getType() == PieceType.Pawn) { //no idea honestly
=======
if (isWhite == true || PieceType getType() == PieceType.Pawn) { //no idea honestly
>>>>>>> branch 'master' of https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git
if ()
}
if (boolean isWhite == false|| //PieceType type == Pawn) {
if (boolean isWhite == false && //PieceType type == Pawn) {
if ()
}
}
}
}*/