From 89a9a3c0a2d02f873545791bbae3cb521ccfb31b Mon Sep 17 00:00:00 2001 From: 3013016 Date: Mon, 6 Jan 2025 23:27:20 +0100 Subject: [PATCH] =?UTF-8?q?Tests=20f=C3=BCr=20die=20Spielz=C3=BCge=20imple?= =?UTF-8?q?mentiert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/vcs.xml | 4 +- src/test/java/HitoriGameMovesTest.java | 142 +++++++++++++++++++++++++ 2 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 src/test/java/HitoriGameMovesTest.java diff --git a/.idea/vcs.xml b/.idea/vcs.xml index d843f34..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,4 +1,6 @@ - + + + \ No newline at end of file diff --git a/src/test/java/HitoriGameMovesTest.java b/src/test/java/HitoriGameMovesTest.java new file mode 100644 index 0000000..3303cd9 --- /dev/null +++ b/src/test/java/HitoriGameMovesTest.java @@ -0,0 +1,142 @@ +import domain.HitoriGameMoves; + +import org.junit.jupiter.api.*; +import static org.junit.jupiter.api.Assertions.*; + +class HitoriGameMovesTest { + private HitoriGameMoves game; + private static final int[][] TEST_BOARD = { + {1, 2, 1, 3}, + {3, 1, 2, 1}, + {2, 3, 3, 2}, + {1, 2, 1, 3} + }; + + @BeforeEach + void setUp() { + game = new HitoriGameMoves(TEST_BOARD); + } + + @Test + void testMarkCellAsBlack() { + // Test valid black marking + game.markCellAsBlack(0, 2); + assertTrue(game.getBlackCells()[0][2]); + assertFalse(game.getWhiteCells()[0][2]); + + // Test invalid black marking (adjacent to existing black cell) + assertThrows(IllegalStateException.class, () -> { + game.markCellAsBlack(0, 1); + }); + } + + @Test + void testMarkCellAsWhite() { + game.markCellAsWhite(0, 0); + assertTrue(game.getWhiteCells()[0][0]); + assertFalse(game.getBlackCells()[0][0]); + } + + @Test + void testUndo() { + // Make some moves + game.markCellAsWhite(0, 0); + game.markCellAsBlack(1, 1); + + // Undo last move + assertTrue(game.undo()); + + // Verify the last move was undone + assertFalse(game.getBlackCells()[1][1]); + + // Verify previous move remains + assertTrue(game.getWhiteCells()[0][0]); + } + + @Test + void testRedo() { + // Make moves and undo + game.markCellAsWhite(0, 0); + game.markCellAsBlack(1, 1); + game.undo(); + + // Redo last move + assertTrue(game.redo()); + + // Verify move was redone + assertTrue(game.getBlackCells()[1][1]); + } + + @Test + void testUndoLimit() { + // Test undo when no moves made + assertFalse(game.undo()); + } + + @Test + void testRedoLimit() { + // Make and undo a move + game.markCellAsWhite(0, 0); + game.undo(); + assertTrue(game.redo()); + + // Try to redo when at latest state + assertFalse(game.redo()); + } + + @Test + void testMoveHistory() { + // Make several moves + game.markCellAsWhite(0, 0); + game.markCellAsBlack(1, 1); + game.markCellAsWhite(2, 2); + + // Undo twice + game.undo(); + game.undo(); + + // Make new move - should clear redo history + game.markCellAsBlack(2, 1); + + // Try to redo - should fail + assertFalse(game.redo()); + } + + @Test + void testReset() { + // Make some moves + game.markCellAsWhite(0, 0); + game.markCellAsBlack(1, 1); + + // Reset game + game.reset(); + + // Verify all cells are reset + boolean[][] blackCells = game.getBlackCells(); + boolean[][] whiteCells = game.getWhiteCells(); + + for (int i = 0; i < TEST_BOARD.length; i++) { + for (int j = 0; j < TEST_BOARD[0].length; j++) { + assertFalse(blackCells[i][j]); + assertFalse(whiteCells[i][j]); + } + } + + // Verify undo/redo history is cleared + assertFalse(game.undo()); + assertFalse(game.redo()); + } + + @Test + void testMistakeCountIncrement() { + // Make valid move + game.markCellAsBlack(0, 0); + assertEquals(0, game.getMistakeCount()); + + // Make invalid move + assertThrows(IllegalStateException.class, () -> { + game.markCellAsBlack(0, 1); + }); + assertEquals(1, game.getMistakeCount()); + } +} -- 2.51.0