This commit is contained in:
manon 2025-05-13 16:22:59 +02:00
commit 5756f21914
3 changed files with 58 additions and 71 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);
@ -258,10 +262,32 @@ public class Board {
return false;
}
public void undoLastMove() {
//TODO
public void getLastMove (int x, int y) {// it saves the current state before making a move
previousStates.add(new Board(this)); // Use the existing constructor to create a copy
getLastMove(selectedX, selectedY);
}
public void undoLastMove() {
if (!previousStates.isEmpty()) {
Board previousState = previousStates.remove(previousStates.size() - 1); // Get the last state
this.colNum = previousState.colNum;
this.lineNum = previousState.lineNum;
this.turnNumber = previousState.turnNumber;
this.isWhiteTurn = previousState.isWhiteTurn;
this.pieces = new ArrayList<>(previousState.pieces); // Restore pieces
// Reset selected positions and highlighted squares
this.selectedX = previousState.selectedX;
this.selectedY = previousState.selectedY;
this.highlightedSquares = new ArrayList<>(previousState.highlightedSquares);
} else {
System.out.println("There are no moves to undo.");
}
}
public Board(Board board) {
this.colNum = board.colNum;
@ -320,67 +346,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) {
@ -402,7 +386,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);
@ -421,15 +405,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);

View File

@ -109,6 +109,7 @@ public class Game extends Thread {
board.undoLastMove();
}
public void toggleAI(boolean isWhite) {
this.activationAIFlags[isWhite?1:0] = !this.activationAIFlags[isWhite?1:0];
}