fix: serialize
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
package sudoku.io;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
@@ -15,16 +16,15 @@ import sudoku.structure.Block;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
import sudoku.structure.SudokuFactory;
|
||||
|
||||
public class SudokuSerializer {
|
||||
|
||||
public static JSONObject serializeSudoku(final MultiDoku multidoku) {
|
||||
List<Cell> cellIds = new ArrayList<>();
|
||||
List<Cell> cellIds = new ArrayList<>();
|
||||
List<Block> blockIds = new ArrayList<>();
|
||||
|
||||
JSONObject jsonRoot = new JSONObject();
|
||||
JSONArray jsonCells = new JSONArray();
|
||||
JSONObject jsonRoot = new JSONObject();
|
||||
JSONArray jsonCells = new JSONArray();
|
||||
JSONArray jsonBlocks = new JSONArray();
|
||||
JSONArray jsonSudokus = new JSONArray(multidoku.getNbSubGrids());
|
||||
|
||||
@@ -33,9 +33,10 @@ public class SudokuSerializer {
|
||||
|
||||
// init cells
|
||||
for (Cell cell : sudoku.getCells()) {
|
||||
if (!cellIds.contains(cell)) {
|
||||
cellIds.add(cell);
|
||||
}
|
||||
if (cellIds.contains(cell))
|
||||
continue;
|
||||
|
||||
cellIds.add(cell);
|
||||
|
||||
Block block = cell.getBlock();
|
||||
if (!blockIds.contains(block)) {
|
||||
@@ -43,6 +44,7 @@ public class SudokuSerializer {
|
||||
}
|
||||
|
||||
int blockID = blockIds.indexOf(block);
|
||||
assert(blockID >= 0);
|
||||
int symbolIndex = cell.getSymbolIndex();
|
||||
|
||||
JSONObject cellJsonObject = new JSONObject();
|
||||
@@ -58,16 +60,17 @@ public class SudokuSerializer {
|
||||
|
||||
// init blocks
|
||||
|
||||
for (Block blockId : blockIds) {
|
||||
JSONObject blockJsonObject = new JSONObject();
|
||||
JSONArray cellsJsonArray = new JSONArray();
|
||||
for (Cell cell : blockId.getCells()) {
|
||||
int cellID = cellIds.indexOf(cell);
|
||||
cellsJsonArray.put(cellID);
|
||||
}
|
||||
blockJsonObject.put("cellIDs", cellsJsonArray);
|
||||
jsonBlocks.put(blockJsonObject);
|
||||
}
|
||||
for (Block blockId : blockIds) {
|
||||
JSONObject blockJsonObject = new JSONObject();
|
||||
JSONArray cellsJsonArray = new JSONArray();
|
||||
for (Cell cell : blockId.getCells()) {
|
||||
int cellID = cellIds.indexOf(cell);
|
||||
assert (cellID >= 0);
|
||||
cellsJsonArray.put(cellID);
|
||||
}
|
||||
blockJsonObject.put("cellIDs", cellsJsonArray);
|
||||
jsonBlocks.put(blockJsonObject);
|
||||
}
|
||||
|
||||
for (int i = 0; i < multidoku.getNbSubGrids(); i++) {
|
||||
// serialise sub grid
|
||||
@@ -82,6 +85,7 @@ public class SudokuSerializer {
|
||||
|
||||
for (Cell cell : sudoku.getCells()) {
|
||||
int cellID = cellIds.indexOf(cell);
|
||||
assert (cellID >= 0);
|
||||
cellsJsonArray.put(cellID);
|
||||
}
|
||||
|
||||
@@ -89,6 +93,7 @@ public class SudokuSerializer {
|
||||
|
||||
for (Block block : sudoku.getBlocks()) {
|
||||
int blockID = blockIds.indexOf(block);
|
||||
assert (blockID >= 0);
|
||||
blocksJsonArray.put(blockID);
|
||||
}
|
||||
|
||||
@@ -114,6 +119,7 @@ public class SudokuSerializer {
|
||||
|
||||
/**
|
||||
* Save a serialized MultiDoku in a JSON file.
|
||||
*
|
||||
* @param doku MultiDoku, MultiDoku to save.
|
||||
* @return String, the path of the save.
|
||||
*/
|
||||
@@ -139,6 +145,7 @@ public class SudokuSerializer {
|
||||
|
||||
/**
|
||||
* Get a MultiDoku from a pre-existing json save file.
|
||||
*
|
||||
* @param numberSave int, number of the save file to open.
|
||||
* @return MultiDoku, MultoDoku contained in the file.
|
||||
* @throws Exception when the given save file does not exist.
|
||||
@@ -158,20 +165,6 @@ public class SudokuSerializer {
|
||||
throw new Exception("This save does not exist.");
|
||||
} else {
|
||||
fileContent = new String(Files.readAllBytes(Paths.get("save/" + fileName)));
|
||||
/*
|
||||
try {
|
||||
FileReader file = new FileReader(f);
|
||||
char[] rawFileContent = {};
|
||||
int length = 1000;
|
||||
while (file.read(rawFileContent, 0, length) != -1) {
|
||||
rawFileContent = new char[]{};
|
||||
length = length * 10;
|
||||
}
|
||||
fileContent = new String(rawFileContent);
|
||||
} catch (IOException e) {
|
||||
throw new Exception("Error reading file.");
|
||||
}
|
||||
*/
|
||||
return deserializeSudoku(fileContent);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user