This commit is contained in:
cleme 2025-05-23 09:30:55 +02:00
parent da7f9739fc
commit cf8a0f4d80
3 changed files with 43 additions and 31 deletions

View File

@ -1,2 +1,2 @@
/backend/
/windowInterface/
/backend/

View File

@ -2,6 +2,7 @@ package backend;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Board {
private int width;
@ -11,10 +12,12 @@ public class Board {
private ArrayList<Piece> pieces;
private List<Move.Position> highlightedPositions = new ArrayList<>();
// Add this for undo functionality
private Stack<Board> boardHistory = new Stack<>();
private Integer selectedX = null;
private Integer selectedY = null;
public Board(int colNum, int lineNum) {
this.width = colNum;
this.height = lineNum;
@ -22,37 +25,30 @@ public class Board {
this.isWhiteTurn = true;
this.pieces = new ArrayList<>();
this.highlightedPositions = new ArrayList<>();
this.boardHistory = new Stack<>();
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getTurnNumber() {
return turnNumber;
}
public boolean isTurnWhite() {
return isWhiteTurn;
}
public void advanceTurn() {
turnNumber++;
isWhiteTurn = !isWhiteTurn;
}
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
for (int i = 0; i < pieces.size(); i++) {
Piece p = pieces.get(i);
@ -65,7 +61,6 @@ public class Board {
pieces.add(newPiece);
}
public void removePiece(int x, int y) {
for (int i = 0; i < pieces.size(); i++) {
Piece p = pieces.get(i);
@ -76,7 +71,6 @@ public class Board {
}
}
public void populateBoard() {
cleanBoard();
@ -114,12 +108,10 @@ public class Board {
pieces.add(new Piece(7, 7, rook, true));
}
public void cleanBoard() {
pieces.clear();
}
public String toString() {
String result = "";
for (int y = 0; y < height; y++) {
@ -142,7 +134,6 @@ public class Board {
return result;
}
public ArrayList<Piece> getPieces() {
ArrayList<Piece> result = new ArrayList<>();
for (Piece p : this.pieces) {
@ -151,7 +142,6 @@ public class Board {
return result;
}
public Piece getPieceAt(int x, int y) {
for (Piece p : pieces) {
if (p.getX() == x && p.getY() == y) {
@ -161,7 +151,6 @@ public class Board {
return null;
}
public void userTouch(int x, int y) {
Piece clickedPiece = getPieceAt(x, y);
@ -180,6 +169,9 @@ public class Board {
Piece selectedPiece = getPieceAt(selectedX, selectedY);
if (selectedPiece != null) {
if (isHighlighted(x, y)) {
// SAVE STATE BEFORE MOVE
saveCurrentState();
Move move = new Move(selectedX, selectedY, x, y, this);
if (move.isValid()) {
move.execute();
@ -218,7 +210,6 @@ public class Board {
return false;
}
public String[] toFileRep() {
String[] fileRep = new String[height + 1];
@ -239,11 +230,11 @@ public class Board {
return fileRep;
}
public Board(String[] array) {
this.width = 8;
this.height = 8;
this.pieces = new ArrayList<>();
this.boardHistory = new Stack<>();
for (int y = 0; y < height; y++) {
if (y >= array.length) {
@ -293,7 +284,6 @@ public class Board {
}
}
public boolean isHighlighted(int x, int y) {
for (Move.Position pos : highlightedPositions) {
if (pos.x == x && pos.y == y) {
@ -303,12 +293,6 @@ public class Board {
return false;
}
public void undoLastMove() {
// TODO
}
public Board(Board board) {
this.width = board.width;
this.height = board.height;
@ -328,14 +312,15 @@ public class Board {
// Copy highlighted positions
this.highlightedPositions = new ArrayList<>();
this.highlightedPositions.addAll(board.highlightedPositions);
}
// Initialize board history for copy constructor
this.boardHistory = new Stack<>();
}
private void clearHighlights() {
highlightedPositions.clear();
}
private void calculateHighlights() {
if (selectedX == null || selectedY == null) {
return;
@ -355,13 +340,40 @@ public class Board {
highlightedPositions.addAll(validMoves);
}
public void playMove(Move move) {
if (move.isValid() && !move.putsOwnKingInCheck()) {
// SAVE STATE BEFORE MOVE
saveCurrentState();
move.execute();
// Play the move sound
// Play the move sound
Sound.getInstance().playMoveSound();
}
}
private void saveCurrentState() {
boardHistory.push(new Board(this));
}
public void undoLastMove() {
if (!boardHistory.isEmpty()) {
Board previousBoard = boardHistory.pop();
this.width = previousBoard.width;
this.height = previousBoard.height;
this.turnNumber = previousBoard.turnNumber;
this.isWhiteTurn = previousBoard.isWhiteTurn;
this.pieces.clear();
for (Piece p : previousBoard.getPieces()) {
this.pieces.add(new Piece(p.getX(), p.getY(), p.getType(), p.isWhite()));
}
selectedX = null;
selectedY = null;
clearHighlights();
}
}
public boolean canUndo() {
return !boardHistory.isEmpty();
}
}