Merge branch 'master' of https://gitarero.ecam.fr/keshini.nistar/OOP_Groupe_1A3_Project.git
This commit is contained in:
commit
1aa23f6145
|
|
@ -1,6 +1,7 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Board {
|
public class Board {
|
||||||
private int width;
|
private int width;
|
||||||
|
|
@ -11,6 +12,9 @@ public class Board {
|
||||||
private boolean isTurnWhite=true;
|
private boolean isTurnWhite=true;
|
||||||
private ArrayList<int[]> highlightedSquares= new ArrayList<>(); //stores highlighted board positions
|
private ArrayList<int[]> highlightedSquares= new ArrayList<>(); //stores highlighted board positions
|
||||||
private Move lastMove; // Tracks the last move; needed for en-passant rule
|
private Move lastMove; // Tracks the last move; needed for en-passant rule
|
||||||
|
private List<Board> previousStates = new ArrayList<>(); // i added this
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public Board(int colNum, int lineNum) {
|
public Board(int colNum, int lineNum) {
|
||||||
this.width=colNum;
|
this.width=colNum;
|
||||||
|
|
@ -18,6 +22,40 @@ public class Board {
|
||||||
this.board= new Piece[width][height];
|
this.board= new Piece[width][height];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy-constructor: take a complete snapshot of another Board.
|
||||||
|
*/
|
||||||
|
public Board(Board other) {
|
||||||
|
// 1) copy dimensions
|
||||||
|
this.width = other.width;
|
||||||
|
this.height = other.height;
|
||||||
|
|
||||||
|
// 2) deep-copy the board array
|
||||||
|
this.board = new Piece[width][height];
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
for (int y = 0; y < height; y++) {
|
||||||
|
this.board[x][y] = other.board[x][y];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3) copy any “selected” piece
|
||||||
|
this.chosenPiece = other.chosenPiece;
|
||||||
|
|
||||||
|
// 4) turn state
|
||||||
|
this.turnNumber = other.turnNumber;
|
||||||
|
this.isTurnWhite = other.isTurnWhite;
|
||||||
|
|
||||||
|
// 5) highlighted squares
|
||||||
|
this.highlightedSquares = new ArrayList<>(other.highlightedSquares);
|
||||||
|
|
||||||
|
// 6) lastMove (for en-passant)
|
||||||
|
this.lastMove = other.lastMove;
|
||||||
|
|
||||||
|
// 7) start with an empty history
|
||||||
|
this.previousStates = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
|
|
@ -149,6 +187,7 @@ public class Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void movePiece(int oldX, int oldY, int newX, int newY) {
|
public void movePiece(int oldX, int oldY, int newX, int newY) {
|
||||||
|
previousStates.add(new Board(this));
|
||||||
if (board[oldX][oldY] != null) { //checks if there is a piece at that position
|
if (board[oldX][oldY] != null) { //checks if there is a piece at that position
|
||||||
Piece piece = board[oldX][oldY]; // we get the piece from the board
|
Piece piece = board[oldX][oldY]; // we get the piece from the board
|
||||||
board[newX][newY] = piece; // we place the piece on the new position
|
board[newX][newY] = piece; // we place the piece on the new position
|
||||||
|
|
@ -205,19 +244,80 @@ return false; }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverts the board state back one move.
|
||||||
|
*/
|
||||||
public void undoLastMove() {
|
public void undoLastMove() {
|
||||||
//TODO
|
if (previousStates.isEmpty()) {
|
||||||
|
System.out.println("There are no moves to undo.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1) Pop off the most recent snapshot
|
||||||
|
Board prev = previousStates.remove(previousStates.size() - 1);
|
||||||
|
|
||||||
|
// 2) Restore the board array
|
||||||
|
this.board = new Piece[width][height];
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
for (int y = 0; y < height; y++) {
|
||||||
|
this.board[x][y] = prev.board[x][y];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3) Restore all other game‐state fields
|
||||||
|
this.chosenPiece = prev.chosenPiece;
|
||||||
|
this.turnNumber = prev.turnNumber;
|
||||||
|
this.isTurnWhite = prev.isTurnWhite;
|
||||||
|
this.lastMove = prev.lastMove;
|
||||||
|
this.highlightedSquares = new ArrayList<>(prev.highlightedSquares);
|
||||||
|
|
||||||
|
// 4) **Critical**: reset each Piece’s own coordinates to match its array slot
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
for (int y = 0; y < height; y++) {
|
||||||
|
Piece p = this.board[x][y];
|
||||||
|
if (p != null) {
|
||||||
|
p.setX(x);
|
||||||
|
p.setY(y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5) Clear any lingering selection or highlights
|
||||||
|
this.chosenPiece = null;
|
||||||
|
this.highlightedSquares.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private void updateTurnLabel() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Board(Board board) {
|
private void repaint() {
|
||||||
//TODO
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*public Board(Board board) {
|
||||||
|
//TODO
|
||||||
|
|
||||||
|
}*/
|
||||||
|
|
||||||
public void playMove(Move move) {
|
public void playMove(Move move) {
|
||||||
//TODO
|
//TODO
|
||||||
|
|
||||||
}
|
}
|
||||||
|
/** Expose the raw 2D array for testing or drawing. */
|
||||||
|
public Piece[][] getBoardArray() {
|
||||||
|
return board;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -106,5 +106,14 @@ public class Game extends Thread {
|
||||||
public void toggleAI(boolean isWhite) {
|
public void toggleAI(boolean isWhite) {
|
||||||
this.activationAIFlags[isWhite?1:0] = !this.activationAIFlags[isWhite?1:0];
|
this.activationAIFlags[isWhite?1:0] = !this.activationAIFlags[isWhite?1:0];
|
||||||
}
|
}
|
||||||
|
/** In Game.java **/
|
||||||
|
public boolean isTurnWhite() {
|
||||||
|
return board.isTurnWhite();
|
||||||
|
}
|
||||||
|
/** So the UI can reach into the model’s Board for undo/turn info. */
|
||||||
|
public Board getBoard() {
|
||||||
|
return board; // assuming your Game keeps its Board in a field named “board”
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
package windowInterface;
|
package windowInterface;
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
import java.awt.GridLayout;
|
import java.awt.GridLayout;
|
||||||
|
|
||||||
|
|
@ -174,10 +177,16 @@ public class MyInterface extends JFrame {
|
||||||
if(game == null) {
|
if(game == null) {
|
||||||
System.out.println("error : can't undo while no game present");
|
System.out.println("error : can't undo while no game present");
|
||||||
} else {
|
} else {
|
||||||
game.undoLastMove();
|
game.getBoard().undoLastMove();
|
||||||
}
|
panelDraw.repaint();
|
||||||
|
turnLabel.setText("Turn : " + (game.getBoard().isTurnWhite() ? "White" : "Black"));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
public void clicAIToggle(boolean isWhite) {
|
public void clicAIToggle(boolean isWhite) {
|
||||||
if(game == null) {
|
if(game == null) {
|
||||||
System.out.println("error : can't activate AI while no game present");
|
System.out.println("error : can't activate AI while no game present");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue