import domain.HitoriGameMain; import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; import java.io.File; class HitoriGameTest { private HitoriGameMain game; private static final int[][] TEST_BOARD = { {1, 2, 1}, {2, 1, 2}, {1, 2, 1} }; @BeforeEach void setUp() { game = new HitoriGameMain(TEST_BOARD); new File("gamestate.dat").delete(); new File("highscores.dat").delete(); } @AfterEach void tearDown() { new File("gamestate.dat").delete(); new File("highscores.dat").delete(); } @Test void testGameIntegration() { // Test timer functionality game.startTimer(); try { Thread.sleep(1100); } catch (InterruptedException e) { fail("Timer test interrupted"); } assertTrue(game.getElapsedTimeInSeconds() >= 1); // Test game moves game.markCellAsBlack(0, 2); assertTrue(game.getBlackCells()[0][2]); // Test scoring game.addHighScore("TestPlayer", 100); assertFalse(game.getHighScoresWithAverage().isEmpty()); } @Test void testSaveAndLoadGameState() { // Make some moves game.markCellAsWhite(0, 0); game.markCellAsBlack(1, 1); game.startTimer(); try { Thread.sleep(1000); } catch (InterruptedException e) { fail("Timer test interrupted"); } // Save game state game.saveGameState(); // Load game state HitoriGameMain loadedGame = HitoriGameMain.loadGameState(); assertNotNull(loadedGame); assertTrue(loadedGame.getWhiteCells()[0][0]); assertTrue(loadedGame.getBlackCells()[1][1]); assertFalse(loadedGame.getElapsedTimeInSeconds() > 0); } @Test void testResetAll() { // Setup some game state game.markCellAsBlack(0, 0); game.startTimer(); game.addHighScore("TestPlayer", 100); // Reset everything game.reset(); // Verify reset assertFalse(game.getBlackCells()[0][0]); assertEquals(0, game.getElapsedTimeInSeconds()); assertEquals(0, game.getMistakeCount()); } @Test void testCompleteGameFlow() { // Start game game.startTimer(); // Make some moves towards 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); // Verify solution assertFalse(game.isSolved()); // Add score game.stopTimer(); game.addHighScore("Winner", game.getElapsedTimeInSeconds()); // Verify score was recorded boolean found = false; for (String score : game.getHighScoresWithAverage()) { if (score.contains("Winner")) { found = true; break; } } assertTrue(found); } @Test void testTimerPauseResume() { game.startTimer(); try { Thread.sleep(1000); } catch (InterruptedException e) { fail("Timer test interrupted"); } game.pauseTimer(); long pausedTime = game.getElapsedTimeInSeconds(); try { Thread.sleep(500); } catch (InterruptedException e) { fail("Timer test interrupted"); } // Time should not increase while paused assertEquals(pausedTime, game.getElapsedTimeInSeconds()); game.startTimer(); // Resume try { Thread.sleep(1000); } catch (InterruptedException e) { fail("Timer test interrupted"); } // Time should increase after resume assertTrue(game.getElapsedTimeInSeconds() > pausedTime); } }