From e28809aa2635db589a78f601e299a3a720a436bc Mon Sep 17 00:00:00 2001 From: Tong Zheng Hong Date: Tue, 14 Nov 2023 11:12:04 +0800 Subject: [PATCH 1/4] Show storage file format in UG --- docs/UserGuide.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/UserGuide.md b/docs/UserGuide.md index 692715314..737b7993c 100644 --- a/docs/UserGuide.md +++ b/docs/UserGuide.md @@ -636,6 +636,21 @@ Every time a turn ends, ChessMaster will save the current state of the game. Whe user will be prompted to choose whether to load the saved game or begin a new one. ChessMaster only supports a single saved game, and starting a new one will delete the existing save file. +### Editing the storage file +Tasks data are saved automatically in a text (.txt) file `/data/ChessMaster.txt`. Users are welcome to update data directly by editing this data file to achieve a desired game state. + +The format of the saved game in the storage file is as follows: + +```json +[CURRENT_PLAYER_COLOR] // Either BLACK or WHITE +[GAME_DIFFICULTY] // Integer from 1 - 3 +[NEXT_TURN_COLOR] // Either BLACK or WHITE +[HUMAN_MOVE_HISTORY] // delimited by ',' +[CPU_MOVE_HISTORY] // delimited by ',' +[CHESS_BOARD_PIECE] // Next 8 lines of 8 characters each +[hasMoved_FLAGS] // Next 8 lines of 8 characters each +``` + ## FAQ > **Q**: How do I transfer my data to another computer? From 2ed2f11ea44e771a59a38ddac296732b5e529523 Mon Sep 17 00:00:00 2001 From: Tong Zheng Hong Date: Tue, 14 Nov 2023 11:12:19 +0800 Subject: [PATCH 2/4] Remove unnecessary var --- src/main/java/chessmaster/game/Game.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/chessmaster/game/Game.java b/src/main/java/chessmaster/game/Game.java index 24dee0042..37c96fdc7 100644 --- a/src/main/java/chessmaster/game/Game.java +++ b/src/main/java/chessmaster/game/Game.java @@ -32,7 +32,6 @@ public class Game { private Command command; private boolean hasEnded; - private boolean exit = false; public Game(Color playerColour, Color currentTurnColor, ChessBoard board, Storage storage, TextUI ui, int difficulty, Human human, CPU cpu) { From 640f78ce576f5176707313c9dca449ffd224a9a4 Mon Sep 17 00:00:00 2001 From: Tong Zheng Hong Date: Tue, 14 Nov 2023 11:13:02 +0800 Subject: [PATCH 3/4] Fix UI message for choose player color --- src/main/java/chessmaster/ui/UiMessages.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/chessmaster/ui/UiMessages.java b/src/main/java/chessmaster/ui/UiMessages.java index 97bbbbfa9..dc4d553bf 100644 --- a/src/main/java/chessmaster/ui/UiMessages.java +++ b/src/main/java/chessmaster/ui/UiMessages.java @@ -17,7 +17,8 @@ public class UiMessages { "Invalid input! Please enter either 'y' for yes or 'n' for no: "; public static final String CONTINUE_PREV_GAME_MESSAGE = "Great! Continuing previous game as %s at difficulty %d"; - public static final String CHOOSE_PLAYER_COLOR_MESSAGE = "Choose your starting color to start new game! [b/w/exit]"; + public static final String CHOOSE_PLAYER_COLOR_MESSAGE = + "Choose your starting color to start new game! [b/w/exit] "; public static final String CHOOSE_PLAYER_COLOR_ERROR_MESSAGE = "Invalid input! Please enter either 'b' for Black or 'w' for White: "; public static final String START_NEW_GAME_MESSAGE = "Great! Starting new game as %s"; From 6afb1081464b110b2f469a53ed0eeb6b31a34981 Mon Sep 17 00:00:00 2001 From: Tong Zheng Hong Date: Tue, 14 Nov 2023 11:13:29 +0800 Subject: [PATCH 4/4] Check empty spaces and delimiter in Storage class --- .../java/chessmaster/storage/Storage.java | 30 ++++++++++--------- src/main/java/chessmaster/user/Player.java | 2 +- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/main/java/chessmaster/storage/Storage.java b/src/main/java/chessmaster/storage/Storage.java index 94a68bb02..9ad76ee26 100644 --- a/src/main/java/chessmaster/storage/Storage.java +++ b/src/main/java/chessmaster/storage/Storage.java @@ -11,7 +11,7 @@ import chessmaster.pieces.ChessPiece; import chessmaster.user.CPU; import chessmaster.user.Human; - +import chessmaster.user.Player; import java.io.File; import java.io.FileNotFoundException; @@ -167,7 +167,7 @@ public ChessTile[][] loadBoard() throws ChessMasterException { int rowIndex = 0; ChessTile[][] boardTiles = new ChessTile[ChessBoard.SIZE][ChessBoard.SIZE]; while (rowIndex < ChessBoard.SIZE && fileScanner.hasNext()) { - String chessRowLine = fileScanner.nextLine(); + String chessRowLine = fileScanner.nextLine().trim(); if (chessRowLine.length() != ChessBoard.SIZE) { fileScanner.close(); throw new LoadBoardException(); @@ -195,7 +195,7 @@ public ChessTile[][] loadBoard() throws ChessMasterException { rowIndex = 0; while (rowIndex < ChessBoard.SIZE && fileScanner.hasNext()) { - String chessRowLine = fileScanner.nextLine(); + String chessRowLine = fileScanner.nextLine().trim(); if (chessRowLine.length() != ChessBoard.SIZE) { fileScanner.close(); throw new LoadBoardException(); @@ -231,7 +231,7 @@ public ChessBoard executeSavedMoves(Color playerColor, ChessBoard board, Human human, CPU cpu) throws ChessMasterException { - ArrayList moveStringList = new ArrayList(); + ArrayList moveStringList = new ArrayList(); ArrayList humanMoves = loadHumanMoves(); ArrayList cpuMoves = loadCPUMoves(); @@ -287,7 +287,7 @@ public ChessBoard executeSavedMoves(Color playerColor, //@@author onx001 - private boolean isPieceValid (ChessPiece initialPiece) { + private boolean isPieceValid(ChessPiece initialPiece) { if (initialPiece.isBlackKing()) { if (blackKingPresent) { return false; @@ -339,8 +339,8 @@ public Color loadPlayerColor() throws ChessMasterException { } if (fileScanner.hasNext()) { - String colorLine = fileScanner.nextLine(); - Color playerColor = Parser.parsePlayerColor(colorLine); + String colorLine = fileScanner.nextLine().trim(); + Color playerColor = Parser.parsePlayerColor(colorLine.toUpperCase()); fileScanner.close(); return playerColor; @@ -373,7 +373,7 @@ public int loadDifficulty() throws ChessMasterException { if (fileScanner.hasNext()) { try { - String difficultyLine = fileScanner.nextLine(); + String difficultyLine = fileScanner.nextLine().trim(); int difficulty = Parser.parseDifficulty(difficultyLine); fileScanner.close(); @@ -414,8 +414,8 @@ public Color loadCurrentColor() throws ChessMasterException { } if (fileScanner.hasNext()) { - String currentColorString = fileScanner.nextLine(); - Color color = Parser.parsePlayerColor(currentColorString); + String currentColorString = fileScanner.nextLine().trim(); + Color color = Parser.parsePlayerColor(currentColorString.toUpperCase()); fileScanner.close(); return color; } @@ -455,11 +455,12 @@ public ArrayList loadHumanMoves() throws ChessMasterException { ArrayList out = new ArrayList(); if (fileScanner.hasNext()) { - String[] movesArray = fileScanner.nextLine().split(", "); + String movesString = fileScanner.nextLine().trim(); + String[] movesArray = movesString.split(Player.MOVE_DELIMITER); Arrays.stream(movesArray) .sequential() .filter(x -> !x.equals("")) - .forEach(x -> out.add(x)); + .forEach(x -> out.add(x.trim())); } fileScanner.close(); @@ -492,11 +493,12 @@ public ArrayList loadCPUMoves() throws ChessMasterException { ArrayList out = new ArrayList(); if (fileScanner.hasNext()) { - String[] movesArray = fileScanner.nextLine().split(", "); + String movesString = fileScanner.nextLine().trim(); + String[] movesArray = movesString.split(Player.MOVE_DELIMITER); Arrays.stream(movesArray) .sequential() .filter(x -> !x.equals("")) - .forEach(x -> out.add(x)); + .forEach(x -> out.add(x.trim())); } fileScanner.close(); diff --git a/src/main/java/chessmaster/user/Player.java b/src/main/java/chessmaster/user/Player.java index 8b46fba33..3e8689b0d 100644 --- a/src/main/java/chessmaster/user/Player.java +++ b/src/main/java/chessmaster/user/Player.java @@ -10,7 +10,7 @@ public abstract class Player { - private static final String MOVE_DELIMITER = ", "; + public static final String MOVE_DELIMITER = ","; protected ArrayList moves; protected ArrayList pieces;