Browse Source

little change at pom file, removed jar creation bcs of errors. added json working in Model and test

remotes/origin/develop
fdai6499 2 years ago
parent
commit
ca7e16cdf3
  1. 2
      pom.xml
  2. 31
      src/main/java/src/Model.java
  3. 30
      src/test/java/ModelTest.java

2
pom.xml

@ -8,8 +8,6 @@
<artifactId>sudoku_team_deepthought</artifactId>
<version>0.2-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>

31
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;
}
}

30
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+"?");
}
}
}
Loading…
Cancel
Save