Compare commits
1 Commits
af0ac0ff77
...
tests
| Author | SHA1 | Date | |
|---|---|---|---|
| abf6f6c7c3 |
@@ -1,13 +1,13 @@
|
|||||||
package sudoku.io;
|
package sudoku.io;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import gui.RenderableMultidoku;
|
import gui.RenderableMultidoku;
|
||||||
import gui.constants.Symbols;
|
import gui.constants.Symbols;
|
||||||
import sudoku.structure.Cell;
|
import sudoku.structure.Cell;
|
||||||
import sudoku.structure.MultiDoku;
|
import sudoku.structure.MultiDoku;
|
||||||
import sudoku.structure.Sudoku;
|
import sudoku.structure.Sudoku;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class SudokuPrinter {
|
public class SudokuPrinter {
|
||||||
public static final String ANSI_RESET = "\u001B[0m";
|
public static final String ANSI_RESET = "\u001B[0m";
|
||||||
public static final String ANSI_RED = "\u001B[31m";
|
public static final String ANSI_RED = "\u001B[31m";
|
||||||
@@ -47,7 +47,7 @@ public class SudokuPrinter {
|
|||||||
List<String> listSymbols) {
|
List<String> listSymbols) {
|
||||||
StringBuilder header = new StringBuilder("");
|
StringBuilder header = new StringBuilder("");
|
||||||
header.append(" ");
|
header.append(" ");
|
||||||
for (int x = 0; x < blockWidth*blockHeight; x++) {
|
for (int x = 0; x < blockWidth * blockHeight; x++) {
|
||||||
header.append(x + 1).append(" ");
|
header.append(x + 1).append(" ");
|
||||||
if (x % blockWidth == blockWidth - 1 && x != blockWidth * blockHeight - 1) {
|
if (x % blockWidth == blockWidth - 1 && x != blockWidth * blockHeight - 1) {
|
||||||
header.append(" ");
|
header.append(" ");
|
||||||
@@ -77,11 +77,12 @@ public class SudokuPrinter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void printMultiDoku(final RenderableMultidoku rm, Symbols symbols, int blockWidth, int blockHeight) {
|
public static String printMultiDoku(final RenderableMultidoku rm, Symbols symbols, int blockWidth,
|
||||||
printMultiDoku(rm, symbols.getSymbols(), blockWidth, blockHeight);
|
int blockHeight) {
|
||||||
|
return printMultiDoku(rm, symbols.getSymbols(), blockWidth, blockHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void printMultiDoku(final RenderableMultidoku rm, List<String> listSymbols, int blockWidth,
|
public static String printMultiDoku(final RenderableMultidoku rm, List<String> listSymbols, int blockWidth,
|
||||||
int blockHeight) {
|
int blockHeight) {
|
||||||
StringBuilder line = new StringBuilder("\n");
|
StringBuilder line = new StringBuilder("\n");
|
||||||
int nBlockInWidth = rm.getWidth() / blockWidth;
|
int nBlockInWidth = rm.getWidth() / blockWidth;
|
||||||
@@ -108,7 +109,8 @@ public class SudokuPrinter {
|
|||||||
line.append("]\n");
|
line.append("]\n");
|
||||||
}
|
}
|
||||||
line.append("__".repeat(Math.max(0, rm.getWidth() + nBlockInWidth))).append("_\n");
|
line.append("__".repeat(Math.max(0, rm.getWidth() + nBlockInWidth))).append("_\n");
|
||||||
System.out.println(line);
|
// System.out.println(line);
|
||||||
|
return line.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void printMultiDokuWithIndex(final RenderableMultidoku rm, List<String> listSymbols, int blockWidth,
|
public static void printMultiDokuWithIndex(final RenderableMultidoku rm, List<String> listSymbols, int blockWidth,
|
||||||
@@ -127,7 +129,7 @@ public class SudokuPrinter {
|
|||||||
if (y % blockHeight == 0) {
|
if (y % blockHeight == 0) {
|
||||||
line.append(" ").append("__".repeat(Math.max(0, rm.getWidth() + nBlockInWidth))).append("_\n");
|
line.append(" ").append("__".repeat(Math.max(0, rm.getWidth() + nBlockInWidth))).append("_\n");
|
||||||
}
|
}
|
||||||
line.append(y+1).append(" [ ");
|
line.append(y + 1).append(" [ ");
|
||||||
for (int x = 0; x < rm.getWidth(); x++) {
|
for (int x = 0; x < rm.getWidth(); x++) {
|
||||||
if (x % blockWidth == 0 && x > 0) {
|
if (x % blockWidth == 0 && x > 0) {
|
||||||
line.append("| ");
|
line.append("| ");
|
||||||
@@ -182,12 +184,16 @@ public class SudokuPrinter {
|
|||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void printMultiDoku(final MultiDoku doku, int blockWidth, int blockHeight, Symbols symbols) {
|
public static String printMultiDoku(final MultiDoku doku) {
|
||||||
if (doku.getNbSubGrids() == 1) {
|
int blockWidth = doku.getSubGrid(0).getBlockWidth();
|
||||||
printRectangleSudoku(doku.getSubGrid(0), blockWidth, blockHeight, symbols);
|
if (blockWidth == 0)
|
||||||
} else {
|
return printMultiDoku(doku, 0, 0, Symbols.Numbers);
|
||||||
printMultiDoku(RenderableMultidoku.fromMultidoku(doku), symbols, blockWidth, blockHeight);
|
else
|
||||||
}
|
return printMultiDoku(doku, blockWidth, doku.getSubGrid(0).getSize() / blockWidth, Symbols.Letters);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String printMultiDoku(final MultiDoku doku, int blockWidth, int blockHeight, Symbols symbols) {
|
||||||
|
return printMultiDoku(RenderableMultidoku.fromMultidoku(doku), symbols, blockWidth, blockHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void printMultiDokuWithIndex(final MultiDoku doku, int blockWidth, int blockHeight, Symbols symbols) {
|
public static void printMultiDokuWithIndex(final MultiDoku doku, int blockWidth, int blockHeight, Symbols symbols) {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package sudoku.structure;
|
package sudoku.structure;
|
||||||
|
|
||||||
|
import sudoku.io.SudokuPrinter;
|
||||||
import sudoku.io.SudokuSerializer;
|
import sudoku.io.SudokuSerializer;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -182,7 +183,7 @@ public class MultiDoku {
|
|||||||
if (sudoku.getSize() != otherSudoku.getSize())
|
if (sudoku.getSize() != otherSudoku.getSize())
|
||||||
return false;
|
return false;
|
||||||
for (int j = 0; j < sudoku.getSize() * sudoku.getSize(); j++) {
|
for (int j = 0; j < sudoku.getSize() * sudoku.getSize(); j++) {
|
||||||
if (sudoku.getCell(i).getSymbolIndex() != otherSudoku.getCell(i).getSymbolIndex())
|
if (sudoku.getCell(j).getSymbolIndex() != otherSudoku.getCell(j).getSymbolIndex())
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -191,13 +192,7 @@ public class MultiDoku {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
return SudokuPrinter.printMultiDoku(this);
|
||||||
sb.append("Multidoku {");
|
|
||||||
for (Sudoku sudoku : subGrids) {
|
|
||||||
sb.append("\n\t").append(sudoku.toString());
|
|
||||||
}
|
|
||||||
sb.append("\n}");
|
|
||||||
return sb.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public MultiDoku clone() {
|
public MultiDoku clone() {
|
||||||
|
|||||||
@@ -328,7 +328,7 @@ public class SudokuFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rempli un MultiDoku donné par rapport à un difficulté.
|
* Remplit un MultiDoku donné par rapport à une difficulté.
|
||||||
*
|
*
|
||||||
* @param doku MultiDoku, vide.
|
* @param doku MultiDoku, vide.
|
||||||
* @param difficulty Difficulty, qui correspond au pourcentage de cases à enlever.
|
* @param difficulty Difficulty, qui correspond au pourcentage de cases à enlever.
|
||||||
|
|||||||
@@ -42,13 +42,13 @@ class SolverTest {
|
|||||||
ns, 0, 1, ns);
|
ns, 0, 1, ns);
|
||||||
assertTrue(test.setImmutableCellsSymbol(immutableCells));
|
assertTrue(test.setImmutableCellsSymbol(immutableCells));
|
||||||
List<Integer> correctCells = List.of(
|
List<Integer> correctCells = List.of(
|
||||||
1, 2, 3, 0,
|
1, 2, 3, 0,
|
||||||
0, 3, 2, 1,
|
0, 3, 2, 1,
|
||||||
2, 1, 0, 3,
|
2, 1, 0, 3,
|
||||||
3, 0, 1, 2);
|
3, 0, 1, 2);
|
||||||
assertTrue(result.setCellsSymbol(correctCells));
|
assertTrue(result.setCellsSymbol(correctCells));
|
||||||
assertTrue(result.isSolved());
|
assertTrue(result.isSolved());
|
||||||
|
|
||||||
assertNotEquals(mdResult, mdTest);
|
assertNotEquals(mdResult, mdTest);
|
||||||
solver.solve(mdTest);
|
solver.solve(mdTest);
|
||||||
assertTrue(mdTest.isSolved());
|
assertTrue(mdTest.isSolved());
|
||||||
@@ -101,24 +101,32 @@ class SolverTest {
|
|||||||
MultiDoku mdResult = SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(mdTest));
|
MultiDoku mdResult = SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(mdTest));
|
||||||
assertFalse(mdTest.isSolved());
|
assertFalse(mdTest.isSolved());
|
||||||
assertFalse(mdResult.isSolved());
|
assertFalse(mdResult.isSolved());
|
||||||
|
assertEquals(mdTest, mdResult);
|
||||||
|
|
||||||
assertTrue(solver.solve(mdTest));
|
assertTrue(solver.solve(mdTest));
|
||||||
|
|
||||||
assertTrue(mdTest.isSolved());
|
assertTrue(mdTest.isSolved());
|
||||||
assertFalse(mdResult.isSolved());
|
assertFalse(mdResult.isSolved());
|
||||||
|
System.out.println(mdTest);
|
||||||
|
System.out.println(mdResult);
|
||||||
assertNotEquals(mdTest, mdResult);
|
assertNotEquals(mdTest, mdResult);
|
||||||
solver.solve(mdResult);
|
|
||||||
|
assertTrue(solver.solve(mdResult));
|
||||||
|
|
||||||
assertEquals(mdTest, mdResult);
|
assertEquals(mdTest, mdResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void solveTest() {
|
void solveTest() {
|
||||||
initializeSolvers();
|
for (int i = 0; i < 100; i++) {
|
||||||
testSize2(h);
|
testSize2(h);
|
||||||
testSize3(h);
|
testSize3(h);
|
||||||
testSize2(m);
|
testSize2(m);
|
||||||
testSize3(m);
|
testSize3(m);
|
||||||
testMDSize3(m);
|
testMDSize3(m);
|
||||||
testSize2(r);
|
testSize2(r);
|
||||||
testSize3(r);
|
testSize3(r);
|
||||||
testMDSize3(r);
|
testMDSize3(r);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user