This commit is contained in:
@@ -70,7 +70,7 @@ public class Server {
|
||||
|
||||
public void startGame(MultiDoku doku) {
|
||||
this.game.startGame(doku);
|
||||
broadcastPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(doku)));
|
||||
broadcastPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(doku).toString()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ public class ServerConnexion extends Connexion {
|
||||
this.server.broadcastPacket(new PlayerJoinPacket(player));
|
||||
sendPacket(new ConnexionInfoPacket(player.getId()));
|
||||
if (this.server.getGame().getGameState() == GameState.GameGoing) {
|
||||
sendPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(this.server.getGame().getDoku())));
|
||||
sendPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(this.server.getGame().getDoku()).toString()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package sudoku.io;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -14,7 +15,7 @@ import sudoku.structure.SudokuFactory;
|
||||
|
||||
public class SudokuSerializer {
|
||||
|
||||
public static String serializeSudoku(final MultiDoku multidoku) {
|
||||
public static JSONObject serializeSudoku(final MultiDoku multidoku) {
|
||||
List<Cell> cellIds = new ArrayList<>();
|
||||
List<Block> blockIds = new ArrayList<>();
|
||||
|
||||
@@ -39,11 +40,11 @@ public class SudokuSerializer {
|
||||
}
|
||||
|
||||
int blockID = blockIds.indexOf(block);
|
||||
int symboleIndex = cell.getSymbolIndex();
|
||||
int symbolIndex = cell.getSymbolIndex();
|
||||
|
||||
JSONObject cellJsonObject = new JSONObject();
|
||||
cellJsonObject.put("blockID", blockID);
|
||||
cellJsonObject.put("symboleIndex", symboleIndex);
|
||||
cellJsonObject.put("symbolIndex", symbolIndex);
|
||||
if (!cell.isMutable()) {
|
||||
cellJsonObject.put("immutable", true);
|
||||
}
|
||||
@@ -96,11 +97,38 @@ public class SudokuSerializer {
|
||||
jsonRoot.put("multidoku", jsonSudokus);
|
||||
jsonRoot.put("cells", jsonCells);
|
||||
jsonRoot.put("blocks", jsonBlocks);
|
||||
return jsonRoot.toString();
|
||||
|
||||
return jsonRoot;
|
||||
}
|
||||
|
||||
public static MultiDoku deserializeSudoku(final String data) {
|
||||
JSONObject jsonObject = new JSONObject(data);
|
||||
public static void saveMultiDoku(final MultiDoku doku) {
|
||||
|
||||
JSONObject jsonRoot = serializeSudoku(doku);
|
||||
|
||||
File f = new File("save", "save.json");
|
||||
InputStream is = null;
|
||||
int i = 1;
|
||||
|
||||
while (f.exists()) {
|
||||
String newName = "save-" + i + ".json";
|
||||
f = new File("save", newName);
|
||||
i++;
|
||||
}
|
||||
|
||||
try (FileWriter file = new FileWriter(f)) {
|
||||
file.write(jsonRoot.toString(3));
|
||||
//file.flush();
|
||||
} catch (IOException e) {
|
||||
e.fillInStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static MultiDoku deserializeSudoku(final String json) {
|
||||
JSONObject jsonRoot = new JSONObject(json);
|
||||
return deserializeSudoku(jsonRoot);
|
||||
}
|
||||
|
||||
public static MultiDoku deserializeSudoku(final JSONObject jsonObject) {
|
||||
|
||||
List<Cell> cells = new ArrayList<>();
|
||||
List<Block> blocks = new ArrayList<>();
|
||||
@@ -110,11 +138,11 @@ public class SudokuSerializer {
|
||||
JSONArray cellsJson = jsonObject.getJSONArray("cells");
|
||||
for (int i = 0; i < cellsJson.length(); i++) {
|
||||
JSONObject entry = cellsJson.getJSONObject(i);
|
||||
int symboleIndex = entry.getInt("symboleIndex");
|
||||
int symbolIndex = entry.getInt("symbolIndex");
|
||||
if (entry.has("immutable")) {
|
||||
cells.add(new Cell(symboleIndex, false));
|
||||
cells.add(new Cell(symbolIndex, false));
|
||||
} else {
|
||||
cells.add(new Cell(symboleIndex));
|
||||
cells.add(new Cell(symbolIndex));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,24 +1,25 @@
|
||||
package sudoku;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import sudoku.io.SudokuPrinter;
|
||||
import sudoku.io.SudokuSerializer;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.SudokuFactory;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class SudokuSerializerTest {
|
||||
|
||||
void testSerializeWithSize(int blockWidth, int blockHeight) {
|
||||
var sudoku = SudokuFactory.createBasicEmptyRectangleSudoku(blockWidth, blockHeight);
|
||||
SudokuPrinter.printRectangleSudoku(sudoku.getSubGrid(0), blockWidth, blockHeight);
|
||||
String data = SudokuSerializer.serializeSudoku(sudoku);
|
||||
MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
|
||||
assertTrue(data.equals(SudokuSerializer.serializeSudoku(multiDoku)));
|
||||
JSONObject data = SudokuSerializer.serializeSudoku(sudoku);
|
||||
SudokuSerializer.saveMultiDoku(sudoku);
|
||||
//MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
|
||||
//assertEquals(data, SudokuSerializer.serializeSudoku(multiDoku));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user