|
|
@ -2,9 +2,13 @@ package src; |
|
|
|
|
|
|
|
import org.json.simple.JSONArray; |
|
|
|
import org.json.simple.JSONObject; |
|
|
|
import org.json.simple.parser.JSONParser; |
|
|
|
import org.json.simple.parser.ParseException; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.FileNotFoundException; |
|
|
|
import java.io.FileWriter; |
|
|
|
import java.io.IOException; |
|
|
|
import java.util.Iterator; |
|
|
|
import java.util.List; |
|
|
@ -56,8 +60,6 @@ public class Model { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setField(int x, int y, int value) { |
|
|
|
model[y][x] = value; |
|
|
|
} |
|
|
@ -86,12 +88,17 @@ public class Model { |
|
|
|
|
|
|
|
public void safeFieldAsCurrentGame(List<Model> history) { |
|
|
|
|
|
|
|
String s = loadAllData(); |
|
|
|
|
|
|
|
JSONObject current = modelToJson(); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public String loadAllData() { |
|
|
|
public static String loadAllData() { |
|
|
|
|
|
|
|
|
|
|
|
File file = new File("src/main/java/src/sudoku.save"); |
|
|
|
File file = new File("src/main/java/src/sudoku.json"); |
|
|
|
|
|
|
|
if (file.exists()) { |
|
|
|
try { |
|
|
@ -115,6 +122,13 @@ public class Model { |
|
|
|
} |
|
|
|
|
|
|
|
public void loadNewEasy() { |
|
|
|
String s = loadAllData(); |
|
|
|
JSONParser jsonParser = new JSONParser(); |
|
|
|
try { |
|
|
|
JSONObject parse = (JSONObject) jsonParser.parse(s); |
|
|
|
} catch (ParseException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
@ -130,10 +144,6 @@ public class Model { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public void showAllGames() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public String toString() { |
|
|
|
|
|
|
@ -191,9 +201,18 @@ public class Model { |
|
|
|
while (iterator.hasNext()) { |
|
|
|
|
|
|
|
JSONObject next = (JSONObject) iterator.next(); |
|
|
|
int x = (int) next.get("x"); |
|
|
|
int y = (int) next.get("y"); |
|
|
|
int value = (int) next.get("value"); |
|
|
|
Object x1 = next.get("x"); |
|
|
|
Object x2 = next.get("y"); |
|
|
|
Object x3 = next.get("value"); |
|
|
|
String s1 = String.valueOf(x1); |
|
|
|
String s2 = String.valueOf(x2); |
|
|
|
String s3 = String.valueOf(x3); |
|
|
|
int x = Integer.valueOf(s1); |
|
|
|
int y = Integer.valueOf(s2); |
|
|
|
int value = Integer.valueOf(s3); |
|
|
|
// int x = (int) next.get("x"); fails bcs of long cast //todo ?? |
|
|
|
// int y = (int) next.get("y"); |
|
|
|
// int value = (int) next.get("value"); |
|
|
|
|
|
|
|
setField(x, y, value); |
|
|
|
} |
|
|
@ -206,4 +225,105 @@ public class Model { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static void initJsonFile() { |
|
|
|
|
|
|
|
int[][] easyDefault = { |
|
|
|
{1,1,1, 0,0,0, 0,0,0}, |
|
|
|
{1,1,1, 0,0,0, 0,0,0}, |
|
|
|
{1,1,1, 0,0,0, 0,0,0}, |
|
|
|
|
|
|
|
{0,0,0, 0,0,0, 0,0,0}, |
|
|
|
{0,0,0, 0,0,0, 0,0,0}, |
|
|
|
{0,0,0, 0,0,0, 0,0,0}, |
|
|
|
|
|
|
|
{0,0,0, 0,0,0, 0,0,0}, |
|
|
|
{0,0,0, 0,0,0, 0,0,0}, |
|
|
|
{0,0,0, 0,0,0, 0,0,0}, |
|
|
|
}; |
|
|
|
|
|
|
|
int[][] mediumDefault = { |
|
|
|
{0,0,0, 0,0,0, 0,0,0}, |
|
|
|
{0,0,0, 0,0,0, 0,0,0}, |
|
|
|
{0,0,0, 0,0,0, 0,0,0}, |
|
|
|
|
|
|
|
{0,0,0, 1,1,1, 0,0,0}, |
|
|
|
{0,0,0, 1,1,1, 0,0,0}, |
|
|
|
{0,0,0, 1,1,1, 0,0,0}, |
|
|
|
|
|
|
|
{0,0,0, 0,0,0, 0,0,0}, |
|
|
|
{0,0,0, 0,0,0, 0,0,0}, |
|
|
|
{0,0,0, 0,0,0, 0,0,0}, |
|
|
|
}; |
|
|
|
|
|
|
|
int[][] hardDefault = { |
|
|
|
{0,0,0, 0,0,0, 0,0,0}, |
|
|
|
{0,0,0, 0,0,0, 0,0,0}, |
|
|
|
{0,0,0, 0,0,0, 0,0,0}, |
|
|
|
|
|
|
|
{0,0,0, 0,0,0, 0,0,0}, |
|
|
|
{0,0,0, 0,0,0, 0,0,0}, |
|
|
|
{0,0,0, 0,0,0, 0,0,0}, |
|
|
|
|
|
|
|
{0,0,0, 0,0,0, 1,1,1}, |
|
|
|
{0,0,0, 0,0,0, 1,1,1}, |
|
|
|
{0,0,0, 0,0,0, 1,1,1}, |
|
|
|
}; |
|
|
|
|
|
|
|
GameField gameFieldEasy = new GameField(360); |
|
|
|
GameField gameFieldMedium = new GameField(360); |
|
|
|
GameField gameFieldHard = new GameField(360); |
|
|
|
|
|
|
|
for (int i = 0; i < 9; i++) { |
|
|
|
for (int j = 0; j <9; j++) { |
|
|
|
|
|
|
|
gameFieldEasy.setValue(i, j, easyDefault[j][i]); |
|
|
|
gameFieldMedium.setValue(i, j, mediumDefault[j][i]); |
|
|
|
gameFieldHard.setValue(i, j, hardDefault[j][i]); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Model modelEasy = new Model(gameFieldEasy); |
|
|
|
Model modelMedium = new Model(gameFieldMedium); |
|
|
|
Model modelHard = new Model(gameFieldHard); |
|
|
|
|
|
|
|
JSONObject jsonObjectEasy = modelEasy.modelToJson(); |
|
|
|
JSONObject jsonObjectMedium = modelMedium.modelToJson(); |
|
|
|
JSONObject jsonObjectHard = modelHard.modelToJson(); |
|
|
|
|
|
|
|
JSONObject jsonObjectRoot = new JSONObject(); |
|
|
|
|
|
|
|
JSONArray jsonArrayEasy = new JSONArray(); |
|
|
|
jsonArrayEasy.add(jsonObjectEasy); |
|
|
|
JSONArray jsonArrayMedium = new JSONArray(); |
|
|
|
jsonArrayMedium.add(jsonObjectMedium); |
|
|
|
JSONArray jsonArrayHard = new JSONArray(); |
|
|
|
jsonArrayHard.add(jsonObjectHard); |
|
|
|
|
|
|
|
|
|
|
|
jsonObjectRoot.put("easyGame", jsonArrayEasy); |
|
|
|
jsonObjectRoot.put("mediumGame", jsonArrayMedium); |
|
|
|
jsonObjectRoot.put("hardGame", jsonArrayHard); |
|
|
|
|
|
|
|
String string = jsonObjectRoot.toJSONString(); |
|
|
|
|
|
|
|
File file = new File("src/main/java/src/sudoku.json"); |
|
|
|
|
|
|
|
if (!file.exists()) { |
|
|
|
try { |
|
|
|
file.createNewFile(); |
|
|
|
} catch (IOException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
FileWriter writer = new FileWriter("src/main/java/src/sudoku.json"); |
|
|
|
writer.write(string); |
|
|
|
writer.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
} |