diff --git a/pom.xml b/pom.xml index 7165069..d459e53 100644 --- a/pom.xml +++ b/pom.xml @@ -8,8 +8,6 @@ sudoku_team_deepthought 0.2-SNAPSHOT - jar - 8 8 diff --git a/src/main/java/src/Model.java b/src/main/java/src/Model.java index e87779c..8dc95ea 100644 --- a/src/main/java/src/Model.java +++ b/src/main/java/src/Model.java @@ -1,6 +1,9 @@ package src; -import javax.swing.*; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +import java.util.Iterator; import java.util.List; public class Model { @@ -9,6 +12,7 @@ public class Model { public int[][] model; public int difficulty; + private GameField gameField; public int getDifficulty() { return difficulty; @@ -19,6 +23,7 @@ public class Model { } public Model(GameField gameField) { + this.gameField = gameField; model = new int[SIZE][]; @@ -33,6 +38,8 @@ public class Model { } } + + public void setField(int x, int y, int value) { model[y][x] = value; } @@ -112,4 +119,26 @@ public class Model { return sb.toString(); } + + public JSONObject modelToJson() { + + JSONArray values = new JSONArray(); + + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("x", j); + jsonObject.put("y", i); + jsonObject.put("value", getField(i, j)); + values.add(jsonObject); + + } + } + + JSONObject game = new JSONObject(); + game.put("values", values); + + return game; + } } diff --git a/src/test/java/ModelTest.java b/src/test/java/ModelTest.java index 1068b40..87df513 100644 --- a/src/test/java/ModelTest.java +++ b/src/test/java/ModelTest.java @@ -1,8 +1,12 @@ +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; import org.junit.jupiter.api.Test; import src.GameField; import src.MainFrame; import src.Model; +import java.util.Iterator; + import static org.junit.jupiter.api.Assertions.*; public class ModelTest extends MainFrame { @@ -202,4 +206,30 @@ public class ModelTest extends MainFrame { @Test public void test_showAllGames() { } + + @Test + public void test_modelToJson() { + + //mock + GameField gameField = new GameField(360); + for (int i = 0, k = 13; i < 9; i++) { + for (int j = 0, l = 17; j < 9; j++) { + gameField.setValue(i, j, (i*l+j*k)%9); + } + } + Model model = new Model(gameField); + + //test + JSONObject jsonObject = model.modelToJson(); + JSONArray values = (JSONArray) jsonObject.get("values"); + Iterator iterator = values.iterator(); + 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"); + int r = (x*13+y*17)%9; + assertEquals(r, value, "x:"+x+", y:"+y+"="+value+"-->"+r+"?"); + } + } } \ No newline at end of file