Browse Source

added some more json and model utils in order wo load data to and from the gamefield

remotes/origin/develop
fdai6499 2 years ago
parent
commit
929abd9951
  1. 39
      src/main/java/src/Model.java
  2. 26
      src/test/java/ModelTest.java

39
src/main/java/src/Model.java

@ -14,6 +14,20 @@ public class Model {
public int difficulty; public int difficulty;
private GameField gameField; private GameField gameField;
public Model(JSONObject mock, GameField gameField) {
this.gameField = gameField;
model = new int[SIZE][];
for (int i = 0; i < SIZE; i++) {
model[i] = new int[SIZE];
}
jsonToModel(mock);
modelToGameField();
}
public int getDifficulty() { public int getDifficulty() {
return difficulty; return difficulty;
} }
@ -141,4 +155,29 @@ public class Model {
return game; return game;
} }
public void jsonToModel(JSONObject jsonObject) {
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");
setField(x, y, value);
}
}
public void modelToGameField() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
gameField.setValue(i, j, getField(i, j));
}
}
}
} }

26
src/test/java/ModelTest.java

@ -232,4 +232,30 @@ public class ModelTest extends MainFrame {
assertEquals(r, value, "x:"+x+", y:"+y+"="+value+"-->"+r+"?"); assertEquals(r, value, "x:"+x+", y:"+y+"="+value+"-->"+r+"?");
} }
} }
@Test
public void test_jsonToModelToGameField() {
//mock
GameField gf = new GameField(360);
for (int i = 0, k = 13; i < 9; i++) {
for (int j = 0, l = 17; j < 9; j++) {
gf.setValue(i, j, (i*l+j*k)%9);
}
}
Model m = new Model(gf);
JSONObject mock = m.modelToJson();
//test
GameField gameField = new GameField(360);
Model model = new Model(mock, gameField);
for (int i = 0, k = 13; i < 9; i++) {
for (int j = 0, l = 17; j < 9; j++) {
int value = gameField.getValue(i, j);
int expected = (i * k + j * l) % 9;
assertEquals(expected, value, "i:"+i+", j:"+j+"="+value+"-->"+expected+"?");
}
}
}
} }
Loading…
Cancel
Save