diff --git a/src/main/java/de/tims/tictactoe/ai/AIHard.java b/src/main/java/de/tims/tictactoe/ai/AIHard.java index 34e1c26..d3cd174 100644 --- a/src/main/java/de/tims/tictactoe/ai/AIHard.java +++ b/src/main/java/de/tims/tictactoe/ai/AIHard.java @@ -52,12 +52,24 @@ public class AIHard implements TicTacToeAI { if (charsInRow == BOARD_SIZE - 1 || charsInCol == BOARD_SIZE - 1) { for (int k = 0; k < BOARD_SIZE; k++) { - row = (charsInRow == BOARD_SIZE - 1) ? j : k; - col = (charsInRow == BOARD_SIZE - 1) ? k : j; + if (charsInRow == BOARD_SIZE - 1) { + row = j; + col = k; + + if (board[row][col] == EMPTY_CHAR) { + gl.setField(row, col, AI_CHAR); + return; + } + } - if (board[row][col] == EMPTY_CHAR) { - gl.setField(row, col, AI_CHAR); - return; + if (charsInCol == BOARD_SIZE - 1) { + row = k; + col = j; + + if (board[row][col] == EMPTY_CHAR) { + gl.setField(row, col, AI_CHAR); + return; + } } } } diff --git a/src/test/java/de/tims/tictactoe/ai/AIHardTest.java b/src/test/java/de/tims/tictactoe/ai/AIHardTest.java index fc9b48d..57fc4f5 100644 --- a/src/test/java/de/tims/tictactoe/ai/AIHardTest.java +++ b/src/test/java/de/tims/tictactoe/ai/AIHardTest.java @@ -176,6 +176,17 @@ class AIHardTest { verify(gl, times(1)).setField(2, 1, realChar); } + @Test + void ifRowAndColWithSameNumberContainEachContainTwoCharsDontIgnoreCol() { + char realChar = 'o'; + doReturn(new char[][] { {'x', 'o', 'x'}, {'-', 'o', '-'}, {'o', 'x', 'x'} }).when(gl).getBoard(); + + TicTacToeAI ai = new AIHard(gl); + ai.calculateNextMove(); + + verify(gl, times(1)).setField(1, 2, realChar); + } + @ParameterizedTest @MethodSource("testCasesForCountCharsInRow") void countCharsInRowTest(String testName, char[][] board, int rowNum, char charToCount, int expectedResult) {