From be44e497c489e8f554c19080a31860028379e585 Mon Sep 17 00:00:00 2001 From: clara Date: Tue, 13 May 2025 11:41:04 +0200 Subject: [PATCH] Modifications about the memory, trying to solve the pbm : cant' play after having load what was saved --- .settings/org.eclipse.core.resources.prefs | 1 + src/Main.java | 17 +++++++- src/backend/Board.java | 46 +++++++++++----------- src/backend/Piece.java | 4 ++ src/backend/SpecialMoves.java | 6 +-- 5 files changed, 46 insertions(+), 28 deletions(-) diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index 0cc7c41..3069545 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -1,3 +1,4 @@ eclipse.preferences.version=1 +encoding//src/Main.java=UTF-8 encoding//src/backend/Board.java=UTF-8 encoding/=windows-1252 diff --git a/src/Main.java b/src/Main.java index 5fa6d15..d35d291 100644 --- a/src/Main.java +++ b/src/Main.java @@ -5,6 +5,13 @@ import backend.PieceType; import windowInterface.MyInterface; + +import java.io.PrintWriter; +import java.io.IOException; +import java.io.FileReader; +import java.io.BufferedReader; +import java.util.ArrayList; + public class Main { @@ -25,6 +32,14 @@ public class Main { MyInterface mjf = new MyInterface(); mjf.setVisible(true); + + + //print the table for the memory + } -} + } + + + + diff --git a/src/backend/Board.java b/src/backend/Board.java index c0fcb90..57b3403 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -202,44 +202,42 @@ public class Board { /* saving-loading feature :*/ public String[] toFileRep() { - - ArrayList lines = new ArrayList<>(); + ArrayList lines = new ArrayList<>(); - - lines.add(String.valueOf(turnNumber)); //nombre xe tours - lines.add(String.valueOf(isWhiteTurn)); //couleur du joueur + // Line 0 : number tour + lines.add(String.valueOf(turnNumber)); - - for (Piece piece : pieces) { //ajoute la pièce à la mémoire - String line = piece.getType() + "," + piece.getX() + "," + piece.getY() + "," + piece.isWhite(); - lines.add(line); - } - System.out.println("The modifications are: " + lines); - return lines.toArray(new String[0]); - - } + // Line 1 : color of the player + lines.add(String.valueOf(isWhiteTurn)); + + // Line 2+: each piece + for (Piece piece : pieces) { + String line = piece.getType() + "," + piece.getX() + "," + piece.getY() + "," + piece.isWhite(); + lines.add(line); + } + + return lines.toArray(new String[0]); + } public Board(String[] array) { - this.colNum = 8; - this.lineNum = 8; + this.colNum = 8; + this.lineNum = 8; - this.turnNumber = Integer.parseInt(array[0]); this.isWhiteTurn = Boolean.parseBoolean(array[1]); this.pieces = new ArrayList<>(); - for (int i = 2; i < array.length; i++) { String[] parts = array[i].split(","); - PieceType type = PieceType.valueOf(parts[0]); - int x = Integer.parseInt(parts[1]); - int y = Integer.parseInt(parts[2]); - boolean isWhite = Boolean.parseBoolean(parts[3]); + PieceType type = PieceType.valueOf(parts[0]); + int x = Integer.parseInt(parts[1]); + int y = Integer.parseInt(parts[2]); + boolean isWhite = Boolean.parseBoolean(parts[3]); - pieces.add(new Piece(x, y, isWhite, type));} - System.out.println("The modifications are: " + pieces); + pieces.add(new Piece(x, y, isWhite, type)); + } } /* The following methods require more work ! */ diff --git a/src/backend/Piece.java b/src/backend/Piece.java index d1e5c07..bc84ba0 100644 --- a/src/backend/Piece.java +++ b/src/backend/Piece.java @@ -38,4 +38,8 @@ public class Piece { this.y = y; } + public String toString() { + return (isWhite ? "White " : "Black ") + type + " at (" + x + "," + y + ")"; + } + } diff --git a/src/backend/SpecialMoves.java b/src/backend/SpecialMoves.java index f9c386a..5042660 100644 --- a/src/backend/SpecialMoves.java +++ b/src/backend/SpecialMoves.java @@ -1,4 +1,4 @@ -package backend; +/*package backend; public class SpecialMoves { private Piece piece; @@ -21,7 +21,7 @@ public class SpecialMoves { int[][] offsets = {{1, 2}, {-1, 2}}; }*/ - public boolean checkCoordinates(int x, int y, PieceType type, boolean isWhite) { + /* public boolean checkCoordinates(int x, int y, PieceType type, boolean isWhite) { if (type == PieceType.Pawn || isWhite == true || y == 3) { int[][] offsets = {{1, 0}, {-1, 0}}; @@ -45,4 +45,4 @@ public class SpecialMoves { } } -} +}*/