Merge branch 'master' of

https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git
This commit is contained in:
manon 2025-05-13 14:09:12 +02:00
commit 9b1098ed7b
3 changed files with 29 additions and 20 deletions

View File

@ -5,6 +5,11 @@ import backend.PieceType;
import windowInterface.MyInterface; import windowInterface.MyInterface;
<<<<<<< HEAD
=======
>>>>>>> branch 'master' of https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git
public class Main { public class Main {
@ -25,13 +30,11 @@ public class Main {
MyInterface mjf = new MyInterface(); MyInterface mjf = new MyInterface();
mjf.setVisible(true); mjf.setVisible(true);
//print the table for the memory //print the table for the memory
} }
} }

View File

@ -199,43 +199,46 @@ public class Board {
} }
/* saving-loading feature :*/ /* saving-loading feature :*/
//this public method returns String[]
public String[] toFileRep() { //converts the game into a chain of characters : state of the game
ArrayList<String> lines = new ArrayList<>();//creates a list arraylist to store each line of the save data as a string
public String[] toFileRep() { //number of tour
ArrayList<String> lines = new ArrayList<>(); lines.add(String.valueOf(turnNumber));//It's converted to a string using String.valueOf
// Line 0 : number tour //color of the player
lines.add(String.valueOf(turnNumber)); lines.add(String.valueOf(isWhiteTurn));//same
// Line 1 : color of the player // piece type position and color
lines.add(String.valueOf(isWhiteTurn)); for (Piece piece : pieces) { //loop through all pieces of the game
// Line 2+: each piece
for (Piece piece : pieces) {
String line = piece.getType() + "," + piece.getX() + "," + piece.getY() + "," + piece.isWhite(); String line = piece.getType() + "," + piece.getX() + "," + piece.getY() + "," + piece.isWhite();
lines.add(line); lines.add(line);
} }
return lines.toArray(new String[0]); return lines.toArray(new String[0]); //Converts the ArrayList to a fixed-size String[] and returns it
} }
//constructor for the Board class.
public Board(String[] array) { public Board(String[] array) { //takes the previous string and reconstruct the game state from it
this.colNum = 8; this.colNum = 8;//dimensions initialized
this.lineNum = 8; this.lineNum = 8;
this.turnNumber = Integer.parseInt(array[0]); this.turnNumber = Integer.parseInt(array[0]);//array[0] is the turn number (converted from string to int),
this.isWhiteTurn = Boolean.parseBoolean(array[1]);
this.pieces = new ArrayList<>(); this.isWhiteTurn = Boolean.parseBoolean(array[1]);//array[1] is the current players turn (converted from string to boolean).
this.pieces = new ArrayList<>();//initialize empty list to hold all pieces
for (int i = 2; i < array.length; i++) { for (int i = 2; i < array.length; i++) {
String[] parts = array[i].split(","); String[] parts = array[i].split(",");
PieceType type = PieceType.valueOf(parts[0]); PieceType type = PieceType.valueOf(parts[0]);
int x = Integer.parseInt(parts[1]); int x = Integer.parseInt(parts[1]);
int y = Integer.parseInt(parts[2]); int y = Integer.parseInt(parts[2]);
boolean isWhite = Boolean.parseBoolean(parts[3]); boolean isWhite = Boolean.parseBoolean(parts[3]);
pieces.add(new Piece(x, y, isWhite, type)); pieces.add(new Piece(x, y, isWhite, type)); //Creates a new Piece with the extracted data and adds it to the pieces list.
} }
} }

View File

@ -23,6 +23,8 @@ public class Game extends Thread {
aiPlayer = new AutoPlayer(); aiPlayer = new AutoPlayer();
} }
public int getWidth() { public int getWidth() {
return board.getWidth(); return board.getWidth();
} }
@ -85,6 +87,7 @@ public class Game extends Thread {
public void setBoard(String[] array) { public void setBoard(String[] array) {
board = new Board(array); board = new Board(array);
} }
public Iterable<Piece> getPieces() { public Iterable<Piece> getPieces() {