137 lines
3.5 KiB
Java
137 lines
3.5 KiB
Java
import domain.GameSolver;
|
|
import org.junit.jupiter.api.*;
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
import java.util.List;
|
|
|
|
class HitoriGameSolverTest {
|
|
private GameSolver game;
|
|
private static final int[][] TEST_BOARD = {
|
|
{1, 2, 1},
|
|
{2, 1, 2},
|
|
{1, 2, 1}
|
|
};
|
|
|
|
@BeforeEach
|
|
void setUp() {
|
|
game = new GameSolver(TEST_BOARD);
|
|
}
|
|
|
|
// Testet den Anfangszustand: Das Puzzle sollte nicht gelöst sein
|
|
@Test
|
|
void testUnsolvedInitialState() {
|
|
assertFalse(game.isSolved());
|
|
}
|
|
|
|
// Testet das Lösen des Puzzles mit einer bekannten Lösung
|
|
@Test
|
|
void testSolvingPuzzle() {
|
|
|
|
game.markCellAsBlack(0, 2);
|
|
game.markCellAsWhite(0, 0);
|
|
game.markCellAsWhite(0, 1);
|
|
game.markCellAsWhite(1, 0);
|
|
game.markCellAsWhite(1, 1);
|
|
game.markCellAsWhite(1, 2);
|
|
game.markCellAsWhite(2, 0);
|
|
game.markCellAsWhite(2, 1);
|
|
game.markCellAsBlack(2, 2);
|
|
|
|
assertFalse(game.isSolved());
|
|
}
|
|
|
|
// Testet eine falsche Lösung, bei der alle Zellen weiß markiert sind
|
|
@Test
|
|
void testIncorrectSolution() {
|
|
|
|
for (int i = 0; i < TEST_BOARD.length; i++) {
|
|
for (int j = 0; j < TEST_BOARD[0].length; j++) {
|
|
game.markCellAsWhite(i, j);
|
|
}
|
|
}
|
|
|
|
|
|
assertFalse(game.isSolved());
|
|
}
|
|
|
|
// Testet, ob das Puzzle nicht gelöst ist, wenn weiße Zellen nicht verbunden sind
|
|
@Test
|
|
void testDisconnectedWhiteCells() {
|
|
// Create a solution with disconnected white cells
|
|
game.markCellAsWhite(0, 0);
|
|
game.markCellAsBlack(0, 1);
|
|
game.markCellAsBlack(1, 0);
|
|
game.markCellAsBlack(1, 1);
|
|
game.markCellAsWhite(2, 2);
|
|
|
|
|
|
assertFalse(game.isSolved());
|
|
}
|
|
|
|
// Testet die Erkennung von ungültigen schwarzen Markierungen (benachbarte schwarze Zellen)
|
|
@Test
|
|
void testFindIncorrectBlackMarks() {
|
|
|
|
game.markCellAsBlack(0, 0);
|
|
game.markCellAsBlack(0, 1);
|
|
|
|
List<int[]> errors = game.findIncorrectBlackMarks();
|
|
assertFalse(errors.isEmpty());
|
|
assertEquals(2, errors.size());
|
|
|
|
|
|
boolean foundFirst = false;
|
|
boolean foundSecond = false;
|
|
for (int[] error : errors) {
|
|
if (error[0] == 0 && error[1] == 0) foundFirst = true;
|
|
if (error[0] == 0 && error[1] == 1) foundSecond = true;
|
|
}
|
|
assertTrue(foundFirst && foundSecond);
|
|
}
|
|
|
|
// Testet den Fall, dass es keine ungültigen schwarzen Markierungen gibt
|
|
@Test
|
|
void testNoIncorrectBlackMarks() {
|
|
|
|
game.markCellAsBlack(0, 0);
|
|
game.markCellAsBlack(2, 2);
|
|
|
|
List<int[]> errors = game.findIncorrectBlackMarks();
|
|
assertTrue(errors.isEmpty());
|
|
}
|
|
|
|
// Testet, ob das Puzzle nicht gelöst ist, wenn einige Zellen unmarkiert bleiben
|
|
@Test
|
|
void testUnmarkedCellsNotSolved() {
|
|
|
|
game.markCellAsWhite(0, 0);
|
|
game.markCellAsBlack(0, 1);
|
|
|
|
|
|
assertFalse(game.isSolved());
|
|
}
|
|
|
|
// Testet den Fall von doppelten Zahlen in einer Zeile
|
|
@Test
|
|
void testDuplicateNumbersInRows() {
|
|
|
|
game.markCellAsWhite(0, 0);
|
|
game.markCellAsWhite(0, 1);
|
|
game.markCellAsWhite(0, 2);
|
|
|
|
|
|
assertFalse(game.isSolved());
|
|
}
|
|
|
|
// Testet den Fall von doppelten Zahlen in einer Spalte
|
|
@Test
|
|
void testDuplicateNumbersInColumns() {
|
|
|
|
game.markCellAsWhite(0, 0);
|
|
game.markCellAsWhite(1, 0);
|
|
game.markCellAsWhite(2, 0);
|
|
|
|
|
|
assertFalse(game.isSolved());
|
|
}
|
|
}
|