This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package sudoku;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Random;
|
||||
@@ -22,7 +21,7 @@ public class SudokuSerializerTest {
|
||||
new RandomSolver().solve(sudoku);
|
||||
JSONObject data = SudokuSerializer.serializeSudoku(sudoku);
|
||||
MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
|
||||
assertTrue(data.toString().equals(SudokuSerializer.serializeSudoku(multiDoku).toString()));
|
||||
assertEquals(data.toString(), SudokuSerializer.serializeSudoku(multiDoku).toString());
|
||||
}
|
||||
|
||||
void testSaveWithSize(int blockWidth, int blockHeight) {
|
||||
@@ -41,7 +40,7 @@ public class SudokuSerializerTest {
|
||||
File fileToDelete = new File(savePath);
|
||||
fileToDelete.delete();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println(e.getMessage());
|
||||
assert false;
|
||||
}
|
||||
}
|
||||
@@ -52,7 +51,7 @@ public class SudokuSerializerTest {
|
||||
JSONObject data = SudokuSerializer.serializeSudoku(sudoku);
|
||||
MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
|
||||
|
||||
assertTrue(data.toString().equals(SudokuSerializer.serializeSudoku(multiDoku).toString()));
|
||||
assertEquals(data.toString(), SudokuSerializer.serializeSudoku(multiDoku).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -60,17 +59,17 @@ public class SudokuSerializerTest {
|
||||
Random r = new Random();
|
||||
int testCount = 20;
|
||||
for (int i = 0; i < testCount; i++) {
|
||||
int blockWidth = r.nextInt(4) + 1;
|
||||
int blockHeight = r.nextInt(4) + 1;
|
||||
int blockWidth = r.nextInt(10) + 1;
|
||||
int blockHeight = r.nextInt(10) + 1;
|
||||
testSerializeWithSize(blockWidth, blockHeight);
|
||||
}
|
||||
for (int i = 0; i < testCount; i++) {
|
||||
int blockWidth = r.nextInt(4) + 1;
|
||||
int blockHeight = r.nextInt(4) + 1;
|
||||
int blockWidth = r.nextInt(10) + 1;
|
||||
int blockHeight = r.nextInt(10) + 1;
|
||||
testSaveWithSize(blockWidth, blockHeight);
|
||||
}
|
||||
for (int i = 0; i < testCount; i++) {
|
||||
int size = r.nextInt(2) + 2;
|
||||
int size = r.nextInt(10) + 1;
|
||||
testSerializeX(size);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,23 +9,50 @@ import org.junit.jupiter.api.Test;
|
||||
import gui.constants.Symbols;
|
||||
import sudoku.io.SudokuPrinter;
|
||||
import sudoku.io.SudokuSerializer;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
import sudoku.structure.SudokuFactory;
|
||||
import sudoku.structure.*;
|
||||
|
||||
class SolverTest {
|
||||
private int ns = Cell.NOSYMBOL;
|
||||
|
||||
@Test
|
||||
void solveTest() {
|
||||
MultiDoku dokuToTest = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
MultiDoku dokuResult = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
void testSize2(String solverToUse){
|
||||
MultiDoku mdTest = SudokuFactory.createBasicEmptySquareDoku(2, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
MultiDoku mdResult = SudokuFactory.createBasicEmptySquareDoku(2, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
Sudoku test = mdTest.getSubGrid(0);
|
||||
Sudoku result = mdResult.getSubGrid(0);
|
||||
List<Integer> immutableCells = List.of(
|
||||
ns, 2, 3, ns,
|
||||
0, ns, ns, ns,
|
||||
ns, ns, ns, 3,
|
||||
ns, 0, 1, ns);
|
||||
assert (test.setImmutableCellsSymbol(immutableCells));
|
||||
List<Integer> correctCells = List.of(
|
||||
1, 2, 3, 0,
|
||||
0, 3, 2, 1,
|
||||
2, 1, 0, 3,
|
||||
3, 0, 1, 2);
|
||||
assert(result.setCellsSymbol(correctCells));
|
||||
assert(result.isSolved());
|
||||
|
||||
Sudoku sudokuToTest = dokuToTest.getSubGrid(0);
|
||||
Sudoku sudokuResult = dokuResult.getSubGrid(0);
|
||||
switch (solverToUse){
|
||||
case "human": new HumanSolver().solve(mdTest); break;
|
||||
case "mixed": new MixedSolver().solve(mdTest); break;
|
||||
default: new RandomSolver().solve(mdTest); break;
|
||||
}
|
||||
assert (mdTest.isSolved());
|
||||
for (Cell cell : test.getCells()) {
|
||||
cell.setImmutable();
|
||||
}
|
||||
assertEquals(SudokuSerializer.serializeSudoku(mdTest).toString(),
|
||||
SudokuSerializer.serializeSudoku(mdResult).toString());
|
||||
}
|
||||
|
||||
int ns = Cell.NOSYMBOL;
|
||||
List<Integer> immutableCells = List.of(ns, ns, 0, ns, ns, 2, 8, ns, 1,
|
||||
void testSize3(String solverToUse){
|
||||
MultiDoku mdTest = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
MultiDoku mdResult = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
Sudoku test = mdTest.getSubGrid(0);
|
||||
Sudoku result = mdResult.getSubGrid(0);
|
||||
List<Integer> immutableCells = List.of(
|
||||
ns, ns, 0, ns, ns, 2, 8, ns, 1,
|
||||
ns, 3, ns, ns, 5, 6, 7, ns, ns,
|
||||
ns, ns, ns, 8, ns, 7, ns, ns, 6,
|
||||
0, ns, 1, ns, ns, ns, ns, ns, ns,
|
||||
@@ -34,13 +61,9 @@ class SolverTest {
|
||||
ns, ns, 6, ns, ns, 8, ns, 7, 5,
|
||||
8, 0, ns, 7, ns, 5, 2, ns, 3,
|
||||
5, ns, ns, ns, 3, 1, 0, ns, ns);
|
||||
|
||||
assert (sudokuToTest.setImmutableCellsSymbol(immutableCells));
|
||||
|
||||
//SudokuPrinter.printRectangleSudoku(dokuToTest.getSubGrid(0), 3, 3);
|
||||
SudokuPrinter.printMultiDoku(dokuToTest, 3, 3, Symbols.Numbers);
|
||||
|
||||
List<Integer> correctCells = List.of(7, 6, 0, 3, 4, 2, 8, 5, 1,
|
||||
assert (test.setImmutableCellsSymbol(immutableCells));
|
||||
List<Integer> correctCells = List.of(
|
||||
7, 6, 0, 3, 4, 2, 8, 5, 1,
|
||||
2, 3, 8, 1, 5, 6, 7, 0, 4,
|
||||
1, 4, 5, 8, 0, 7, 3, 2, 6,
|
||||
0, 2, 1, 6, 8, 3, 5, 4, 7,
|
||||
@@ -49,56 +72,58 @@ class SolverTest {
|
||||
3, 1, 6, 0, 2, 8, 4, 7, 5,
|
||||
8, 0, 4, 7, 6, 5, 2, 1, 3,
|
||||
5, 7, 2, 4, 3, 1, 0, 6, 8);
|
||||
assert(result.setCellsSymbol(correctCells));
|
||||
assert(result.isSolved());
|
||||
|
||||
sudokuResult.setCellsSymbol(correctCells);
|
||||
|
||||
System.out.println("\n****************************Doku Control\n");
|
||||
SudokuPrinter.printRectangleSudoku(sudokuResult, 3, 3, Symbols.Russian);
|
||||
|
||||
assert (dokuResult.isSolved());
|
||||
|
||||
new RandomSolver().solve(dokuToTest);
|
||||
|
||||
System.out.println("\n****************************\nDoku solved");
|
||||
//SudokuPrinter.printRectangleSudoku(dokuToTest.getSubGrid(0), 3, 3);
|
||||
SudokuPrinter.printMultiDoku(dokuToTest, 3, 3, Symbols.Emojis);
|
||||
|
||||
assert (dokuToTest.isSolved());
|
||||
|
||||
for (Cell cell : sudokuToTest.getCells()) {
|
||||
switch (solverToUse){
|
||||
case "human": new HumanSolver().solve(mdTest); break;
|
||||
case "mixed": new MixedSolver().solve(mdTest); break;
|
||||
default: new RandomSolver().solve(mdTest); break;
|
||||
}
|
||||
assert (mdTest.isSolved());
|
||||
for (Cell cell : test.getCells()) {
|
||||
cell.setImmutable();
|
||||
}
|
||||
assertEquals(SudokuSerializer.serializeSudoku(mdTest).toString(),
|
||||
SudokuSerializer.serializeSudoku(mdResult).toString());
|
||||
}
|
||||
|
||||
for (Cell cell : sudokuResult.getCells()) {
|
||||
private void testMDSize3(String solverToUse){
|
||||
MultiDoku mdTest = SudokuFactory.createBasicXShapedMultidoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
try {
|
||||
SudokuFactory.fillDoku(mdTest, Difficulty.Easy);
|
||||
} catch (Exception e) {
|
||||
assert(false);
|
||||
}
|
||||
MultiDoku result = SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(mdTest));
|
||||
assert(result.isSolved());
|
||||
switch (solverToUse){
|
||||
case "human": new HumanSolver().solve(mdTest); break;
|
||||
case "mixed": new MixedSolver().solve(mdTest); break;
|
||||
default: new RandomSolver().solve(mdTest); break;
|
||||
}
|
||||
assert (mdTest.isSolved());
|
||||
for (Cell cell : mdTest.getCells()) {
|
||||
cell.setImmutable();
|
||||
}
|
||||
assertEquals(SudokuSerializer.serializeSudoku(mdTest).toString(),
|
||||
SudokuSerializer.serializeSudoku(result).toString());
|
||||
}
|
||||
|
||||
assertEquals(SudokuSerializer.serializeSudoku(dokuResult).toString(),
|
||||
SudokuSerializer.serializeSudoku(dokuToTest).toString());
|
||||
@Test
|
||||
void solveTest() {
|
||||
String h = "human";
|
||||
String m = "mixed";
|
||||
String r = "random";
|
||||
|
||||
MultiDoku dokuToTest2 = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
Sudoku sudokuToTest2 = dokuToTest2.getSubGrid(0);
|
||||
|
||||
List<Integer> immutableCells2 = List.of(ns, ns, 0, ns, ns, 2, 8, ns, 1,
|
||||
1, 3, ns, ns, 5, 6, 7, ns, ns,
|
||||
ns, ns, ns, 8, ns, 7, ns, ns, 6,
|
||||
0, ns, 1, ns, ns, ns, ns, ns, ns,
|
||||
4, 8, 7, 5, 1, ns, 6, ns, ns,
|
||||
6, ns, 3, 2, ns, ns, ns, 8, 0,
|
||||
ns, ns, 6, ns, ns, 8, ns, 7, 5,
|
||||
8, 0, ns, 7, ns, 5, 2, ns, 3,
|
||||
5, ns, ns, ns, 3, 1, 0, ns, ns);
|
||||
sudokuToTest2.setImmutableCellsSymbol(immutableCells2);
|
||||
|
||||
boolean isSolved = new RandomSolver().solve(dokuToTest2);
|
||||
|
||||
assert (!isSolved);
|
||||
|
||||
MultiDoku dokuToTest3 = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
|
||||
new RandomSolver().solve(dokuToTest3);
|
||||
|
||||
//SudokuPrinter.printRectangleSudoku(dokuToTest3.getSubGrid(0), 3, 3);
|
||||
SudokuPrinter.printMultiDoku(dokuToTest3, 3, 3, Symbols.Letters);
|
||||
testSize2(h);
|
||||
testSize3(h);
|
||||
testMDSize3(h);
|
||||
testSize2(m);
|
||||
testSize3(m);
|
||||
testMDSize3(m);
|
||||
testSize2(r);
|
||||
testSize3(r);
|
||||
testMDSize3(r);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user