Compare commits

..

2 Commits

Author SHA1 Message Date
Valentine GIRAL 0c1e9f94a6 loading/saving system 2025-05-09 09:25:45 +02:00
Valentine GIRAL 8dffe28b81 loading/saving system 2025-05-09 09:23:44 +02:00
1 changed files with 60 additions and 40 deletions

View File

@ -174,29 +174,19 @@ public class Board {
if(selectedX == x && selectedY == y) {
selectedPosition = null;
highlightedPositions.clear(); //Unhighlight
return;
}
//To check if square valid
boolean valid = false;
for (int[] pos : highlightedPositions) {
if (pos[0]==x&&pos[1]==y) {
valid = true;
break;
}
// If a piece is selected and the user clicks a new position
else {
else {
// Check if the move is valid by checking if it exists in highlightedPositions
boolean isValidMove = false;
for (int[] pos : highlightedPositions) {
if (pos[0] == x && pos[1] == y) {
boolean isValidMove = false;
for (int[] pos : highlightedPositions) {
if (pos[0] == x && pos[1] == y) {
isValidMove = true;
break;
}
}
}
// Only move the piece if the destination is valid
if (isValidMove) {
movePiece(selectedX, selectedY, x, y);
@ -208,29 +198,13 @@ public class Board {
// Deselect the position after moving
this.selectedPosition = null;
highlightedPositions.clear();//Clear after move
}
// If a piece is selected and the user clicks a new position
if (valid) {
movePiece(selectedX, selectedY, x, y);
//update turn
this.turnNumber++;
this.turnWhite=!this.turnWhite;
}
else {
System.out.println("Blocked! This is not a valid move.");
}
// Deselect the position after moving
this.selectedPosition = null;
highlightedPositions.clear();//Clear after move
}
}
}
public ArrayList<int[]> getValidMoves(Piece p) {
// Calculate the valid moves for each Piece type
ArrayList<int[]> moves = new ArrayList<>();
int x = p.getX(), y = p.getY();
boolean isWhite = p.isWhite();
@ -308,19 +282,65 @@ public class Board {
}
/* saving-loading feature :*/
public String[] toFileRep() {
//TODO
return null;
ArrayList<String> fileLines = new ArrayList<>();
// First line: game state (turn number and current player)
fileLines.add(turnNumber + "," + (turnWhite ? "white" : "black"));
// Second line: board dimensions
fileLines.add(line + "," + col);
// Get all pieces on the board
ArrayList<Piece> pieces = getPieces();
// Add one line per piece with format: pieceType,x,y,color
for (Piece piece : pieces) {
StringBuilder pieceInfo = new StringBuilder();
pieceInfo.append(piece.getType()).append(",");
pieceInfo.append(piece.getX()).append(",");
pieceInfo.append(piece.getY()).append(",");
pieceInfo.append(piece.isWhite() ? "W" : "B");
fileLines.add(pieceInfo.toString());
}
return fileLines.toArray(new String[0]);
}
public Board(String[] array) {
//TODO
public Board(String[] fileRepresentation) {
// Parse game state from first line
String[] gameState = fileRepresentation[0].split(",");
turnNumber = Integer.parseInt(gameState[0]);
turnWhite = gameState[1].equals("white");
// Parse board dimensions from second line
String[] dimensions = fileRepresentation[1].split(",");
line = Integer.parseInt(dimensions[0]);
col = Integer.parseInt(dimensions[1]);
// Initialize the board with the specified dimensions
board = new Piece[line][col];
// Parse pieces from the remaining lines
for (int i = 2; i < fileRepresentation.length; i++) {
String[] pieceInfo = fileRepresentation[i].split(",");
PieceType pieceType = PieceType.valueOf(pieceInfo[0]);
int x = Integer.parseInt(pieceInfo[1]);
int y = Integer.parseInt(pieceInfo[2]);
boolean isWhite = pieceInfo[3].equals("W");
// Create the piece and place it on the board
board[y][x] = new Piece(isWhite,pieceType, x, y);
}
// Initialize other state variables
selectedPosition = null;
highlightedPositions = new ArrayList<>();
}
/* The following methods require more work ! */
public boolean isHighlighted(int x, int y) {
// Storing valid moves when piece is selected
for (int[] pos : highlightedPositions) {