Spiel lösen ausgiebig testen
parent
2dcf36ff36
commit
5e1e0fada9
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue