This commit is contained in:
@@ -10,6 +10,7 @@ import java.util.List;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import sudoku.constraint.Constraint;
|
||||
import sudoku.structure.Block;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
@@ -73,6 +74,7 @@ public class SudokuSerializer {
|
||||
JSONObject jsonSudoku = new JSONObject();
|
||||
JSONArray cellsJsonArray = new JSONArray();
|
||||
JSONArray blocksJsonArray = new JSONArray();
|
||||
JSONArray constraintsJsonArray = new JSONArray();
|
||||
|
||||
Sudoku sudoku = multidoku.getSubGrid(i);
|
||||
|
||||
@@ -90,6 +92,13 @@ public class SudokuSerializer {
|
||||
blocksJsonArray.put(blockID);
|
||||
}
|
||||
|
||||
// serialize constraints
|
||||
|
||||
for (Constraint cons : sudoku.getConstraints()) {
|
||||
constraintsJsonArray.put(cons.ordinal());
|
||||
}
|
||||
|
||||
jsonSudoku.put("constraints", constraintsJsonArray);
|
||||
jsonSudoku.put("cells", cellsJsonArray);
|
||||
jsonSudoku.put("blocks", blocksJsonArray);
|
||||
jsonSudoku.put("blockWidth", sudoku.getBlockWidth());
|
||||
@@ -211,9 +220,11 @@ public class SudokuSerializer {
|
||||
JSONObject sudokuJsonObject = multidokuJsonObject.getJSONObject(i);
|
||||
JSONArray sudokuCellsJsonArray = sudokuJsonObject.getJSONArray("cells");
|
||||
JSONArray sudokuBlocksJsonArray = sudokuJsonObject.getJSONArray("blocks");
|
||||
JSONArray sudokuConstraintsJsonArray = sudokuJsonObject.getJSONArray("constraints");
|
||||
|
||||
List<Cell> sudokuCells = new ArrayList<>();
|
||||
List<Block> sudokuBlocks = new ArrayList<>();
|
||||
List<Constraint> sudokuConstraints = new ArrayList<>();
|
||||
|
||||
for (int j = 0; j < sudokuCellsJsonArray.length(); j++) {
|
||||
int cellID = sudokuCellsJsonArray.getInt(j);
|
||||
@@ -225,7 +236,12 @@ public class SudokuSerializer {
|
||||
sudokuBlocks.add(blocks.get(blockID));
|
||||
}
|
||||
|
||||
Sudoku s = new Sudoku(sudokuCells, sudokuBlocks, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
for (int j = 0; j < sudokuConstraintsJsonArray.length(); j++) {
|
||||
int constraintID = sudokuConstraintsJsonArray.getInt(j);
|
||||
sudokuConstraints.add(Constraint.values()[constraintID]);
|
||||
}
|
||||
|
||||
Sudoku s = new Sudoku(sudokuCells, sudokuBlocks, sudokuConstraints);
|
||||
s.setBlockWidth(sudokuJsonObject.getInt("blockWidth"));
|
||||
sudokus.add(s);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user