Browse Source

implemented initJsonFile and test

remotes/origin/develop
fdai6499 2 years ago
parent
commit
e27d4dad7e
  1. 142
      src/main/java/src/Model.java
  2. 1
      src/main/java/src/sudoku.json
  3. 61
      src/test/java/ModelTest.java

142
src/main/java/src/Model.java

@ -2,9 +2,13 @@ package src;
import org.json.simple.JSONArray; import org.json.simple.JSONArray;
import org.json.simple.JSONObject; 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.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -56,8 +60,6 @@ public class Model {
} }
} }
public void setField(int x, int y, int value) { public void setField(int x, int y, int value) {
model[y][x] = value; model[y][x] = value;
} }
@ -86,12 +88,17 @@ public class Model {
public void safeFieldAsCurrentGame(List<Model> history) { public void safeFieldAsCurrentGame(List<Model> 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()) { if (file.exists()) {
try { try {
@ -115,6 +122,13 @@ public class Model {
} }
public void loadNewEasy() { 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 @Override
public String toString() { public String toString() {
@ -191,9 +201,18 @@ public class Model {
while (iterator.hasNext()) { while (iterator.hasNext()) {
JSONObject next = (JSONObject) iterator.next(); 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); 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);
}
}
} }

1
src/main/java/src/sudoku.json
File diff suppressed because it is too large
View File

61
src/test/java/ModelTest.java

@ -1,5 +1,7 @@
import org.json.simple.JSONArray; import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import src.GameField; import src.GameField;
import src.MainFrame; import src.MainFrame;
@ -9,6 +11,7 @@ import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator; import java.util.Iterator;
import java.util.SortedSet;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@ -214,6 +217,7 @@ public class ModelTest extends MainFrame {
@Test @Test
public void test_loadNewEasy() { public void test_loadNewEasy() {
GameField gameField = new GameField(360);
} }
@Test @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);
}
}
} }
Loading…
Cancel
Save