added docstrings and hashmap that should save state of board each turn

This commit is contained in:
hugomanipoud2 2025-05-23 11:05:52 +02:00
parent 7fe7f6c17e
commit a0d4ae6634
3 changed files with 25 additions and 6 deletions

View File

@ -85,7 +85,8 @@ public class Board {
for (int i = 0; i <= 7; i++) {
pieces.add(new Piece(i, 6, PieceType.Pawn, true));
}
saveBoardState(); // to save first state of the board
}
public void cleanBoard() {
@ -258,6 +259,15 @@ public class Board {
return concernedPiece; // returns the piece
}
public void saveBoardState() {
ArrayList<Piece> boardCopy = new ArrayList<>(); // create arraylist to store in hashmap
for (Piece piece : pieces) {
boardCopy.add(new Piece(piece)); // works thank to copy constructor in piece class
}
boardHistory.put(turnNb, boardCopy); // put the turnNb as the key, and the arraylist data as value
}
public void undoLastMove() {
//TODO

View File

@ -31,13 +31,14 @@ public class Move {
if (board.isAPieceThere(toX, toY)){//checking if a piece is at the arrival coordinates
needPieceDeletion(toX, toY); //if a piece is at arrival coord, remove the piece
}
specialMoves.pawnPromotion(movingPiece, toY);
specialMoves.castlingShort(movingPiece, toX, toY);
specialMoves.castlingLong(movingPiece, toX, toY);
specialMoves.pawnPromotion(movingPiece, toY); // check if there is a case of pawn promotion
specialMoves.castlingShort(movingPiece, toX, toY); // check if there is a case of short castling
specialMoves.castlingLong(movingPiece, toX, toY); // check if there is a case of long castling
movingPiece.setX(toX); // change coordinates to the new coordinate
movingPiece.setY(toY);
board.setTurnNb(turnNb + 1);
System.out.println(board.toString());
board.setTurnNb(turnNb + 1); // adds + 1 to turn nb
board.saveBoardState(); // after updating the pieces on the board and the turnNb, saves the current state of the board in a hashmap
System.out.println(board.toString()); // prints the current state of board
}
break;
}

View File

@ -16,6 +16,14 @@ public class Piece {
//this.index = indexPiece;
}
// copy constructor to create pieces without precision of all specifications, just copies whatever piece you input
public Piece(Piece other) {
this.x = other.x;
this.y = other.y;
this.type = other.type;
this.color = other.color;
}
// xcoord of the piece
public int getX() {
return x;