From e27d4dad7ea7d836734f7206e8596b681a0315bc Mon Sep 17 00:00:00 2001 From: fdai6499 Date: Sat, 21 Jan 2023 06:23:32 +0100 Subject: [PATCH] implemented initJsonFile and test --- src/main/java/src/Model.java | 142 +++++++++++++++++++++++++++++++--- src/main/java/src/sudoku.json | 1 + src/test/java/ModelTest.java | 61 +++++++++++++++ 3 files changed, 193 insertions(+), 11 deletions(-) create mode 100644 src/main/java/src/sudoku.json diff --git a/src/main/java/src/Model.java b/src/main/java/src/Model.java index 6fda6f3..bc21831 100644 --- a/src/main/java/src/Model.java +++ b/src/main/java/src/Model.java @@ -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 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); + } + } } diff --git a/src/main/java/src/sudoku.json b/src/main/java/src/sudoku.json new file mode 100644 index 0000000..6d5978b --- /dev/null +++ b/src/main/java/src/sudoku.json @@ -0,0 +1 @@ +{"mediumGame":[{"values":[{"x":0,"y":0,"value":0},{"x":1,"y":0,"value":0},{"x":2,"y":0,"value":0},{"x":3,"y":0,"value":0},{"x":4,"y":0,"value":0},{"x":5,"y":0,"value":0},{"x":6,"y":0,"value":0},{"x":7,"y":0,"value":0},{"x":8,"y":0,"value":0},{"x":0,"y":1,"value":0},{"x":1,"y":1,"value":0},{"x":2,"y":1,"value":0},{"x":3,"y":1,"value":0},{"x":4,"y":1,"value":0},{"x":5,"y":1,"value":0},{"x":6,"y":1,"value":0},{"x":7,"y":1,"value":0},{"x":8,"y":1,"value":0},{"x":0,"y":2,"value":0},{"x":1,"y":2,"value":0},{"x":2,"y":2,"value":0},{"x":3,"y":2,"value":0},{"x":4,"y":2,"value":0},{"x":5,"y":2,"value":0},{"x":6,"y":2,"value":0},{"x":7,"y":2,"value":0},{"x":8,"y":2,"value":0},{"x":0,"y":3,"value":0},{"x":1,"y":3,"value":0},{"x":2,"y":3,"value":0},{"x":3,"y":3,"value":1},{"x":4,"y":3,"value":1},{"x":5,"y":3,"value":1},{"x":6,"y":3,"value":0},{"x":7,"y":3,"value":0},{"x":8,"y":3,"value":0},{"x":0,"y":4,"value":0},{"x":1,"y":4,"value":0},{"x":2,"y":4,"value":0},{"x":3,"y":4,"value":1},{"x":4,"y":4,"value":1},{"x":5,"y":4,"value":1},{"x":6,"y":4,"value":0},{"x":7,"y":4,"value":0},{"x":8,"y":4,"value":0},{"x":0,"y":5,"value":0},{"x":1,"y":5,"value":0},{"x":2,"y":5,"value":0},{"x":3,"y":5,"value":1},{"x":4,"y":5,"value":1},{"x":5,"y":5,"value":1},{"x":6,"y":5,"value":0},{"x":7,"y":5,"value":0},{"x":8,"y":5,"value":0},{"x":0,"y":6,"value":0},{"x":1,"y":6,"value":0},{"x":2,"y":6,"value":0},{"x":3,"y":6,"value":0},{"x":4,"y":6,"value":0},{"x":5,"y":6,"value":0},{"x":6,"y":6,"value":0},{"x":7,"y":6,"value":0},{"x":8,"y":6,"value":0},{"x":0,"y":7,"value":0},{"x":1,"y":7,"value":0},{"x":2,"y":7,"value":0},{"x":3,"y":7,"value":0},{"x":4,"y":7,"value":0},{"x":5,"y":7,"value":0},{"x":6,"y":7,"value":0},{"x":7,"y":7,"value":0},{"x":8,"y":7,"value":0},{"x":0,"y":8,"value":0},{"x":1,"y":8,"value":0},{"x":2,"y":8,"value":0},{"x":3,"y":8,"value":0},{"x":4,"y":8,"value":0},{"x":5,"y":8,"value":0},{"x":6,"y":8,"value":0},{"x":7,"y":8,"value":0},{"x":8,"y":8,"value":0}]}],"easyGame":[{"values":[{"x":0,"y":0,"value":1},{"x":1,"y":0,"value":1},{"x":2,"y":0,"value":1},{"x":3,"y":0,"value":0},{"x":4,"y":0,"value":0},{"x":5,"y":0,"value":0},{"x":6,"y":0,"value":0},{"x":7,"y":0,"value":0},{"x":8,"y":0,"value":0},{"x":0,"y":1,"value":1},{"x":1,"y":1,"value":1},{"x":2,"y":1,"value":1},{"x":3,"y":1,"value":0},{"x":4,"y":1,"value":0},{"x":5,"y":1,"value":0},{"x":6,"y":1,"value":0},{"x":7,"y":1,"value":0},{"x":8,"y":1,"value":0},{"x":0,"y":2,"value":1},{"x":1,"y":2,"value":1},{"x":2,"y":2,"value":1},{"x":3,"y":2,"value":0},{"x":4,"y":2,"value":0},{"x":5,"y":2,"value":0},{"x":6,"y":2,"value":0},{"x":7,"y":2,"value":0},{"x":8,"y":2,"value":0},{"x":0,"y":3,"value":0},{"x":1,"y":3,"value":0},{"x":2,"y":3,"value":0},{"x":3,"y":3,"value":0},{"x":4,"y":3,"value":0},{"x":5,"y":3,"value":0},{"x":6,"y":3,"value":0},{"x":7,"y":3,"value":0},{"x":8,"y":3,"value":0},{"x":0,"y":4,"value":0},{"x":1,"y":4,"value":0},{"x":2,"y":4,"value":0},{"x":3,"y":4,"value":0},{"x":4,"y":4,"value":0},{"x":5,"y":4,"value":0},{"x":6,"y":4,"value":0},{"x":7,"y":4,"value":0},{"x":8,"y":4,"value":0},{"x":0,"y":5,"value":0},{"x":1,"y":5,"value":0},{"x":2,"y":5,"value":0},{"x":3,"y":5,"value":0},{"x":4,"y":5,"value":0},{"x":5,"y":5,"value":0},{"x":6,"y":5,"value":0},{"x":7,"y":5,"value":0},{"x":8,"y":5,"value":0},{"x":0,"y":6,"value":0},{"x":1,"y":6,"value":0},{"x":2,"y":6,"value":0},{"x":3,"y":6,"value":0},{"x":4,"y":6,"value":0},{"x":5,"y":6,"value":0},{"x":6,"y":6,"value":0},{"x":7,"y":6,"value":0},{"x":8,"y":6,"value":0},{"x":0,"y":7,"value":0},{"x":1,"y":7,"value":0},{"x":2,"y":7,"value":0},{"x":3,"y":7,"value":0},{"x":4,"y":7,"value":0},{"x":5,"y":7,"value":0},{"x":6,"y":7,"value":0},{"x":7,"y":7,"value":0},{"x":8,"y":7,"value":0},{"x":0,"y":8,"value":0},{"x":1,"y":8,"value":0},{"x":2,"y":8,"value":0},{"x":3,"y":8,"value":0},{"x":4,"y":8,"value":0},{"x":5,"y":8,"value":0},{"x":6,"y":8,"value":0},{"x":7,"y":8,"value":0},{"x":8,"y":8,"value":0}]}],"hardGame":[{"values":[{"x":0,"y":0,"value":0},{"x":1,"y":0,"value":0},{"x":2,"y":0,"value":0},{"x":3,"y":0,"value":0},{"x":4,"y":0,"value":0},{"x":5,"y":0,"value":0},{"x":6,"y":0,"value":0},{"x":7,"y":0,"value":0},{"x":8,"y":0,"value":0},{"x":0,"y":1,"value":0},{"x":1,"y":1,"value":0},{"x":2,"y":1,"value":0},{"x":3,"y":1,"value":0},{"x":4,"y":1,"value":0},{"x":5,"y":1,"value":0},{"x":6,"y":1,"value":0},{"x":7,"y":1,"value":0},{"x":8,"y":1,"value":0},{"x":0,"y":2,"value":0},{"x":1,"y":2,"value":0},{"x":2,"y":2,"value":0},{"x":3,"y":2,"value":0},{"x":4,"y":2,"value":0},{"x":5,"y":2,"value":0},{"x":6,"y":2,"value":0},{"x":7,"y":2,"value":0},{"x":8,"y":2,"value":0},{"x":0,"y":3,"value":0},{"x":1,"y":3,"value":0},{"x":2,"y":3,"value":0},{"x":3,"y":3,"value":0},{"x":4,"y":3,"value":0},{"x":5,"y":3,"value":0},{"x":6,"y":3,"value":0},{"x":7,"y":3,"value":0},{"x":8,"y":3,"value":0},{"x":0,"y":4,"value":0},{"x":1,"y":4,"value":0},{"x":2,"y":4,"value":0},{"x":3,"y":4,"value":0},{"x":4,"y":4,"value":0},{"x":5,"y":4,"value":0},{"x":6,"y":4,"value":0},{"x":7,"y":4,"value":0},{"x":8,"y":4,"value":0},{"x":0,"y":5,"value":0},{"x":1,"y":5,"value":0},{"x":2,"y":5,"value":0},{"x":3,"y":5,"value":0},{"x":4,"y":5,"value":0},{"x":5,"y":5,"value":0},{"x":6,"y":5,"value":0},{"x":7,"y":5,"value":0},{"x":8,"y":5,"value":0},{"x":0,"y":6,"value":0},{"x":1,"y":6,"value":0},{"x":2,"y":6,"value":0},{"x":3,"y":6,"value":0},{"x":4,"y":6,"value":0},{"x":5,"y":6,"value":0},{"x":6,"y":6,"value":1},{"x":7,"y":6,"value":1},{"x":8,"y":6,"value":1},{"x":0,"y":7,"value":0},{"x":1,"y":7,"value":0},{"x":2,"y":7,"value":0},{"x":3,"y":7,"value":0},{"x":4,"y":7,"value":0},{"x":5,"y":7,"value":0},{"x":6,"y":7,"value":1},{"x":7,"y":7,"value":1},{"x":8,"y":7,"value":1},{"x":0,"y":8,"value":0},{"x":1,"y":8,"value":0},{"x":2,"y":8,"value":0},{"x":3,"y":8,"value":0},{"x":4,"y":8,"value":0},{"x":5,"y":8,"value":0},{"x":6,"y":8,"value":1},{"x":7,"y":8,"value":1},{"x":8,"y":8,"value":1}]}]} \ No newline at end of file diff --git a/src/test/java/ModelTest.java b/src/test/java/ModelTest.java index c937661..e2f5a5a 100644 --- a/src/test/java/ModelTest.java +++ b/src/test/java/ModelTest.java @@ -1,5 +1,7 @@ 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 src.GameField; import src.MainFrame; @@ -9,6 +11,7 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Iterator; +import java.util.SortedSet; import static org.junit.jupiter.api.Assertions.*; @@ -214,6 +217,7 @@ public class ModelTest extends MainFrame { @Test public void test_loadNewEasy() { + GameField gameField = new GameField(360); } @Test @@ -283,4 +287,61 @@ public class ModelTest extends MainFrame { } } } + + @Test + public void test_initJsonFile() { + + File file = new File("src/main/java/src/sudoku.json"); + + boolean delete = false; + + if (file.exists()) { + delete = file.delete(); + } else { + delete = true; + } + + + assertTrue(delete, "the file still exists, or the path has trouble or stuff like this"); + + Model.initJsonFile(); + + if (!file.exists()) { + assertTrue(false, "create message failed"); + } + + String data = Model.loadAllData(); + + JSONParser parser = new JSONParser(); + JSONObject jsonObject = null; + + try { + jsonObject = (JSONObject) parser.parse(data); + } catch (ParseException e) { + assertTrue(false, "parsing went wrong"); + } + + try { + + JSONArray easyGame = (JSONArray) jsonObject.get("easyGame"); + JSONObject jsonObjectEasyGame = (JSONObject) easyGame.get(0); + + JSONArray mediumGame = (JSONArray) jsonObject.get("mediumGame"); + JSONObject jsonObjectMediumGame = (JSONObject) mediumGame.get(0); + + JSONArray hardGame = (JSONArray) jsonObject.get("hardGame"); + JSONObject jsonObjectHardGame = (JSONObject) hardGame.get(0); + + Model modelEasy = new Model(jsonObjectEasyGame, new GameField(360)); + Model modelMedium = new Model(jsonObjectMediumGame, new GameField(360)); + Model modelHard = new Model(jsonObjectHardGame, new GameField(360)); + +// System.out.println(modelEasy); +// System.out.println(modelMedium); +// System.out.println(modelHard); + + } catch (IndexOutOfBoundsException e) { + assertTrue(false); + } + } } \ No newline at end of file