From 92ee8a45f431fcbb7a18ddb503d5fce78ddb70ad Mon Sep 17 00:00:00 2001 From: Lucie Date: Thu, 1 May 2025 15:31:39 +0200 Subject: [PATCH] toFileRep & Board(String[] array) --- src/backend/Board.java | 69 +++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index a8e0e4a..24c32b0 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -107,8 +107,9 @@ public class Board { str += "B"; str += board[i][j].getType().getSummary(); } - - str += ","; + if (j != 7) { + str += ","; + } } str += "\n"; } @@ -125,7 +126,6 @@ public class Board { } } } - return pieces; } @@ -180,7 +180,6 @@ public class Board { if (board[j][i+1] != null) { moves.add(new int[] {i+1, j}); metPiece = true; - } else { i += 1; @@ -194,7 +193,6 @@ public class Board { if (board[j+1][i] != null) { moves.add(new int[] {i, j+1}); metPiece = true; - } else { j += 1; @@ -208,7 +206,6 @@ public class Board { if (board[j][i-1] != null) { moves.add(new int[] {i-1, j}); metPiece = true; - } else { i -= 1; @@ -273,7 +270,6 @@ public class Board { if (board[j+1][i+1] != null) { moves.add(new int[] {i+1, j+1}); metPiece = true; - } else { i += 1; @@ -288,7 +284,6 @@ public class Board { if (board[j+1][i-1] != null) { moves.add(new int[] {i-1, j+1}); metPiece = true; - } else { i -= 1; @@ -303,7 +298,6 @@ public class Board { if (board[j-1][i+1] != null) { moves.add(new int[] {i+1, j-1}); metPiece = true; - } else { i += 1; @@ -335,7 +329,6 @@ public class Board { if (board[j][i+1] != null) { moves.add(new int[] {i+1, j}); metPiece = true; - } else { i += 1; @@ -349,7 +342,6 @@ public class Board { if (board[j+1][i] != null) { moves.add(new int[] {i, j+1}); metPiece = true; - } else { j += 1; @@ -363,7 +355,6 @@ public class Board { if (board[j][i-1] != null) { moves.add(new int[] {i-1, j}); metPiece = true; - } else { i -= 1; @@ -390,7 +381,6 @@ public class Board { if (board[j+1][i+1] != null) { moves.add(new int[] {i+1, j+1}); metPiece = true; - } else { i += 1; @@ -405,7 +395,6 @@ public class Board { if (board[j+1][i-1] != null) { moves.add(new int[] {i-1, j+1}); metPiece = true; - } else { i -= 1; @@ -420,7 +409,6 @@ public class Board { if (board[j-1][i+1] != null) { moves.add(new int[] {i+1, j-1}); metPiece = true; - } else { i += 1; @@ -522,18 +510,6 @@ public class Board { } } } - - /*while (metPiece == false && i < 8 && j < 8) { - if (x == i && y == j) { - if (board[j][i] != null) { - } - moves.add(new int[] {i, j}); - } - else if (x != this.x && y == this.y) { - if (board[y][x] != null) { - } - } - }*/ return moves; } @@ -555,14 +531,45 @@ public class Board { /* saving-loading feature :*/ public String[] toFileRep() { - //TODO - return null; + String myFile = this.toString(); + String turn = "B"; + if (this.isTurnWhite() == true) { + turn = "W"; + } + myFile += turn; + String[] myArray = myFile.split("\n"); + return myArray; } - + public Board(String[] array) { - //TODO + this.colNum = 8; + this.lineNum = 8; + this.board = new Piece[lineNum][colNum]; + this.x = -1; + this.y = -1; + this.turns = 0; + this.whiteTurn = true; + String[] line; + boolean white; + char character; + PieceType type; + for (int i = 0; i < 8; i++) { + line = array[i].split(","); + for (int j = 0; j < 8; j++) { + if (!line[j].equals(" ")) { + white = line[j].charAt(0) == 'W'; + character = line[j].charAt(1); + type = PieceType.fromSummary(character); + setPiece(white, type, j, i); + } + } + } + if (array[8] == "B") { + whiteTurn = false; + } } + /* The following methods require more work ! */