From 1ec161933c60dabe1f221a288faf6d65e14c9c88 Mon Sep 17 00:00:00 2001 From: fdai6499 Date: Sat, 21 Jan 2023 06:41:23 +0100 Subject: [PATCH] it is possible now to load an easy game from file to the model and testing this --- src/main/java/src/Model.java | 34 ++++++++++++++++++++++++++++------ src/test/java/ModelTest.java | 6 +++++- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/main/java/src/Model.java b/src/main/java/src/Model.java index bc21831..57ebad5 100644 --- a/src/main/java/src/Model.java +++ b/src/main/java/src/Model.java @@ -14,6 +14,8 @@ import java.util.Iterator; import java.util.List; import java.util.Scanner; +import static org.junit.jupiter.api.Assertions.assertTrue; + public class Model { private static int SIZE = 9; @@ -40,6 +42,7 @@ public class Model { return difficulty; } + //1=easy, 2=medium, 3=hard public void setDifficulty(int difficulty) { this.difficulty = difficulty; } @@ -121,15 +124,35 @@ public class Model { } - public void loadNewEasy() { - String s = loadAllData(); - JSONParser jsonParser = new JSONParser(); + public void loadNewEasy(int p) { + + String data = Model.loadAllData(); + + setDifficulty(1); + + JSONParser parser = new JSONParser(); + JSONObject jsonObject = null; + try { - JSONObject parse = (JSONObject) jsonParser.parse(s); + jsonObject = (JSONObject) parser.parse(data); } catch (ParseException e) { - throw new RuntimeException(e); + assertTrue(false, "parsing went wrong"); } + try { + + JSONArray easyGame = (JSONArray) jsonObject.get("easyGame"); + + JSONObject jsonObjectEasyGame = (JSONObject) easyGame.get(p); + +// Model modelEasy = new Model(jsonObjectEasyGame, new GameField(360)); +// System.out.println(modelEasy); + + jsonToModel(jsonObjectEasyGame); + + } catch (IndexOutOfBoundsException e) { + throw e; + } } public void loadNewMedium() { @@ -301,7 +324,6 @@ public class Model { JSONArray jsonArrayHard = new JSONArray(); jsonArrayHard.add(jsonObjectHard); - jsonObjectRoot.put("easyGame", jsonArrayEasy); jsonObjectRoot.put("mediumGame", jsonArrayMedium); jsonObjectRoot.put("hardGame", jsonArrayHard); diff --git a/src/test/java/ModelTest.java b/src/test/java/ModelTest.java index e2f5a5a..5e33f56 100644 --- a/src/test/java/ModelTest.java +++ b/src/test/java/ModelTest.java @@ -217,7 +217,11 @@ public class ModelTest extends MainFrame { @Test public void test_loadNewEasy() { - GameField gameField = new GameField(360); + Model model = new Model(new GameField(360)); + model.loadNewEasy(0); + int field = model.getField(0, 0); + assertEquals(1, field, "the first game has a 1 at (0,0)"); + System.out.println(model); } @Test