Merge branch 'master' of

https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project
This commit is contained in:
clara 2025-05-13 15:36:51 +02:00
parent fa417dc015
commit f28e0e3af9
2 changed files with 33 additions and 69 deletions

View File

@ -1,5 +1,5 @@
2
true
1
false
Rook,0,0,false
Knight,1,0,false
Bishop,2,0,false
@ -13,14 +13,14 @@ Pawn,1,1,false
Pawn,2,1,false
Pawn,3,1,false
Pawn,4,1,false
Pawn,5,2,false
Pawn,5,1,false
Pawn,6,1,false
Pawn,7,1,false
Pawn,0,6,true
Pawn,1,6,true
Pawn,2,6,true
Pawn,2,4,true
Pawn,3,6,true
Pawn,4,4,true
Pawn,4,6,true
Pawn,5,6,true
Pawn,6,6,true
Pawn,7,6,true

View File

@ -2,6 +2,7 @@ package backend;
import java.util.ArrayList;
public class Board {
private int colNum;
private int lineNum;
@ -12,11 +13,14 @@ public class Board {
private int selectedY = -1;
ArrayList<int[]> highlightedSquares = new ArrayList<>();
private ArrayList<Board> previousStates;
public Board(int colNum, int lineNum) {
this.colNum = colNum;
this.lineNum = lineNum;
this.turnNumber = 0;
this.isWhiteTurn = true; // White starts first
this.previousStates = new ArrayList<>(); // Initialize the ArrayList
}
public int getWidth() {
@ -140,7 +144,7 @@ public class Board {
System.out.println("Selecting piece at: " + x + ", " + y);
selectedX = x;
selectedY = y;
highlightedSquares = getValidMoves(clickedPiece, true);
highlightedSquares = getValidMoves(clickedPiece);
System.out.println("Valid moves highlighted for selected piece.");
} else {
System.out.println("No valid piece to select at: " + x + ", " + y);
@ -259,7 +263,7 @@ public class Board {
}
public void undoLastMove() {
//TODO
}
@ -286,67 +290,25 @@ public class Board {
}
public ArrayList<int[]> getValidMoves(Piece piece, boolean filterKingCheck) {
MoveConditions moveHelper = new MoveConditions(piece, this);
ArrayList<int[]> rawMoves = new ArrayList<>();
if (piece.getType() == PieceType.Pawn) {
rawMoves = moveHelper.getPawnMoves();
} else if (piece.getType() == PieceType.Knight) {
rawMoves = moveHelper.getKnightMoves();
} else if (piece.getType() == PieceType.Rook) {
rawMoves = moveHelper.getRookMoves();
} else if (piece.getType() == PieceType.Bishop) {
rawMoves = moveHelper.getBishopMoves();
} else if (piece.getType() == PieceType.Queen) {
rawMoves = moveHelper.getQueenMoves();
} else if (piece.getType() == PieceType.King) {
return moveHelper.getKingMoves(); // Already safe-filtered
}
if (!filterKingCheck) {
return rawMoves; // No filtering if we're in a check-check
}
ArrayList<int[]> legalMoves = new ArrayList<>();
for (int i = 0; i < rawMoves.size(); i++) {
int[] move = rawMoves.get(i);
int newX = move[0];
int newY = move[1];
Board simBoard = new Board(this);
Piece simPiece = null;
for (int j = 0; j < simBoard.getPieces().size(); j++) {
Piece p = simBoard.getPieces().get(j);
if (p.getX() == piece.getX() && p.getY() == piece.getY()
&& p.getType() == piece.getType()
&& p.isWhite() == piece.isWhite()) {
simPiece = p;
}
}
if (simPiece != null) {
Piece captured = simBoard.getPieceAt(newX, newY);
if (captured != null) {
simBoard.getPieces().remove(captured);
}
simPiece.setX(newX);
simPiece.setY(newY);
if (!simBoard.isKingInCheck(piece.isWhite())) {
legalMoves.add(new int[]{newX, newY});
}
}
}
return legalMoves;
}
public ArrayList<int[]> getValidMoves(Piece piece) {
return getValidMoves(piece, true); // Default to safe mode
MoveConditions moveHelper = new MoveConditions(piece, this);
switch (piece.getType()) {
case Pawn:
return moveHelper.getPawnMoves();
case Knight:
return moveHelper.getKnightMoves();
case Rook:
return moveHelper.getRookMoves();
case Bishop:
return moveHelper.getBishopMoves();
case Queen:
return moveHelper.getQueenMoves();
case King:
return moveHelper.getKingMoves();
default:
return new ArrayList<>();
}
}
public boolean isKingInCheck(boolean whiteKing) {
@ -368,7 +330,7 @@ public class Board {
for (int i = 0; i < pieces.size(); i++) {
Piece p = pieces.get(i);
if (p.isWhite() != whiteKing && p.getType() != PieceType.King) {
ArrayList<int[]> moves = getValidMoves(p, false);
ArrayList<int[]> moves = getValidMoves(p);
for (int j = 0; j < moves.size(); j++) {
int[] move = moves.get(j);
@ -387,15 +349,17 @@ public class Board {
boolean kingInCheck = isKingInCheck(whiteKing);
boolean hasEscape = false;
// 1. If the king is not in check, it's never checkmate
if (!kingInCheck) {
return false;
}
// 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);
if (piece.isWhite() == whiteKing) {
ArrayList<int[]> rawMoves = getValidMoves(piece, false);
ArrayList<int[]> rawMoves = getValidMoves(piece);
for (int j = 0; j < rawMoves.size(); j++) {
int[] move = rawMoves.get(j);