Merge pull request 'Spiel lösen ausgiebig testen' (#3) from GameSolverTest into main

Reviewed-on: #3
testBranch
Arthur Kovis 2025-01-06 23:34:57 +01:00
commit 185d0255db
1 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,127 @@
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);
}
@Test
void testUnsolvedInitialState() {
assertFalse(game.isSolved());
}
@Test
void testSolvingPuzzle() {
// Apply known solution
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);
assertTrue(game.isSolved());
}
@Test
void testIncorrectSolution() {
// Mark all cells white
for (int i = 0; i < TEST_BOARD.length; i++) {
for (int j = 0; j < TEST_BOARD[0].length; j++) {
game.markCellAsWhite(i, j);
}
}
// Should not be solved due to duplicate numbers
assertFalse(game.isSolved());
}
@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);
// Should not be solved due to disconnected white cells
assertFalse(game.isSolved());
}
@Test
void testFindIncorrectBlackMarks() {
// Create adjacent black cells
game.markCellAsBlack(0, 0);
game.markCellAsBlack(0, 1); // This creates an invalid situation
List<int[]> errors = game.findIncorrectBlackMarks();
assertFalse(errors.isEmpty());
assertEquals(2, errors.size()); // Both cells should be reported
// Verify error coordinates
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);
}
@Test
void testNoIncorrectBlackMarks() {
// Make valid black marks
game.markCellAsBlack(0, 0);
game.markCellAsBlack(2, 2);
List<int[]> errors = game.findIncorrectBlackMarks();
assertTrue(errors.isEmpty());
}
@Test
void testUnmarkedCellsNotSolved() {
// Leave some cells unmarked
game.markCellAsWhite(0, 0);
game.markCellAsBlack(0, 1);
// Don't mark other cells
assertFalse(game.isSolved());
}
@Test
void testDuplicateNumbersInRows() {
// Mark cells to create duplicate numbers in a row
game.markCellAsWhite(0, 0);
game.markCellAsWhite(0, 1);
game.markCellAsWhite(0, 2);
// Row now has duplicate numbers
assertFalse(game.isSolved());
}
@Test
void testDuplicateNumbersInColumns() {
// Mark cells to create duplicate numbers in a column
game.markCellAsWhite(0, 0);
game.markCellAsWhite(1, 0);
game.markCellAsWhite(2, 0);
// Column now has duplicate numbers
assertFalse(game.isSolved());
}
}