Merge remote-tracking branch 'origin/master'

# Conflicts:
#	app/src/main/java/sudoku/structure/MultiDoku.java
#	app/src/main/java/sudoku/structure/Sudoku.java
#	app/src/main/java/sudoku/structure/SudokuFactory.java
#	app/src/test/java/sudoku/solver/SolverTest.java
This commit is contained in:
Melvyn
2025-01-29 18:51:23 +01:00
17 changed files with 504 additions and 222 deletions

View File

@@ -1,39 +1,41 @@
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;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import sudoku.io.SudokuPrinter;
import sudoku.io.SudokuSerializer;
import sudoku.structure.MultiDoku;
import sudoku.structure.SudokuFactory;
import java.util.Random;
public class SudokuSerializerTest {
void testSerializeWithSize(int blockWidth, int blockHeight) {
var sudoku = SudokuFactory.createBasicEmptyRectangleSudoku(blockWidth, blockHeight);
var sudoku = SudokuFactory.createBasicEmptyRectangleDoku(blockWidth, blockHeight,
SudokuFactory.DEFAULT_CONSTRAINTS);
JSONObject data = SudokuSerializer.serializeSudoku(sudoku);
MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
assert(data.toString().equals(SudokuSerializer.serializeSudoku(multiDoku).toString()));
assert (data.toString().equals(SudokuSerializer.serializeSudoku(multiDoku).toString()));
}
void testSaveWithSize(int blockWidth, int blockHeight) {
MultiDoku doku = SudokuFactory.createBasicEmptyRectangleSudoku(blockWidth, blockHeight);
int saveNumber = SudokuSerializer.saveMultiDoku(doku);
MultiDoku doku = SudokuFactory.createBasicEmptyRectangleDoku(blockWidth, blockHeight,
SudokuFactory.DEFAULT_CONSTRAINTS);
String savePath = SudokuSerializer.saveMultiDoku(doku);
MultiDoku otherDoku = null;
try {
otherDoku = SudokuSerializer.getSavedMultiDoku(saveNumber);
otherDoku = SudokuFactory.fromfile(savePath);
assert (otherDoku != null);
// clean file after test
File fileToDelete = new File(savePath);
fileToDelete.delete();
} catch (Exception e) {
e.printStackTrace();
assert false;
}
assert(doku.equals(otherDoku));
assert (doku.equals(otherDoku));
}
@Test
@@ -41,18 +43,15 @@ public class SudokuSerializerTest {
Random r = new Random();
int testCount = 5;
for (int i = 0; i < testCount; i++) {
int blockWidth = r.nextInt(20) + 1;
int blockHeight = r.nextInt(20) + 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(20) + 1;
int blockHeight = r.nextInt(20) + 1;
int blockWidth = r.nextInt(10) + 1;
int blockHeight = r.nextInt(10) + 1;
testSaveWithSize(blockWidth, blockHeight);
}
}
}

View File

@@ -16,51 +16,46 @@ class SolverTest {
void solveTest() {
Random rand = new Random();
MultiDoku dokuToTest = SudokuFactory.createBasicEmptySquareSudoku(3);
MultiDoku dokuResult = SudokuFactory.createBasicEmptySquareSudoku(3);
MultiDoku dokuToTest = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
MultiDoku dokuResult = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
Sudoku sudokuToTest = dokuToTest.getSubGrid(0);
Sudoku sudokuResult = dokuResult.getSubGrid(0);
int ns = Cell.NOSYMBOL;
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,
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);
assert(sudokuToTest.setImmutableCellsSymbol(immutableCells));
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,
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);
assert (sudokuToTest.setImmutableCellsSymbol(immutableCells));
SudokuPrinter.printRectangleSudoku(dokuToTest.getSubGrid(0), 3, 3);
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,
4, 8, 7, 5, 1, 0, 6, 3, 2,
6, 5, 3, 2, 7, 4, 1, 8, 0,
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);
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,
4, 8, 7, 5, 1, 0, 6, 3, 2,
6, 5, 3, 2, 7, 4, 1, 8, 0,
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);
sudokuResult.setCellsSymbol(correctCells);
System.out.println("\n****************************Doku Control\n");
SudokuPrinter.printRectangleSudoku(sudokuResult, 3, 3);
assert(dokuResult.isSolved());
Solver.solveRandom(dokuToTest, rand);
System.out.println("\n****************************\nDoku solved");
SudokuPrinter.printRectangleSudoku(dokuToTest.getSubGrid(0), 3, 3);
@@ -69,25 +64,25 @@ class SolverTest {
assert(dokuToTest.equals(dokuResult));
MultiDoku dokuToTest2 = SudokuFactory.createBasicEmptySquareSudoku(3);
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);
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 = Solver.solveRandom(dokuToTest2, rand);
assert(!isSolved);
assert (!isSolved);
MultiDoku dokuToTest3 = SudokuFactory.createBasicEmptySquareSudoku(3);
MultiDoku dokuToTest3 = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
Solver.solveRandom(dokuToTest3, rand);