diff --git a/src/main/java/de/tims/tictactoe/ai/AIHard.java b/src/main/java/de/tims/tictactoe/ai/AIHard.java index 2165b1d..ae1e7cb 100644 --- a/src/main/java/de/tims/tictactoe/ai/AIHard.java +++ b/src/main/java/de/tims/tictactoe/ai/AIHard.java @@ -17,6 +17,17 @@ public class AIHard implements TicTacToeAI { @Override public void calculateNextMove() { - gl.setField(1, 1, AI_CHAR); + boolean emptyBoard = true; + char[][] board = gl.getBoard(); + for (char[] row : board) { + for (char field : row) { + emptyBoard &= field == '-'; + } + } + if (emptyBoard) { + gl.setField(1, 1, AI_CHAR); + } else { + gl.setField(0, 0, AI_CHAR); + } } } diff --git a/src/test/java/de/tims/tictactoe/ai/AIHardTest.java b/src/test/java/de/tims/tictactoe/ai/AIHardTest.java index c52ac3f..dddb7d2 100644 --- a/src/test/java/de/tims/tictactoe/ai/AIHardTest.java +++ b/src/test/java/de/tims/tictactoe/ai/AIHardTest.java @@ -26,5 +26,16 @@ class AIHardTest { verify(gl, times(1)).setField(1, 1, realChar); } + + @Test + void middleFieldAlreadySetChooseEdgeField() { + char realChar = 'o'; + doReturn(new char[][] { {'-', '-', '-'}, {'-', 'x', '-'}, {'-', '-', '-'} }).when(gl).getBoard(); + + TicTacToeAI ai = new AIHard(gl); + ai.calculateNextMove(); + + verify(gl, times(1)).setField(0, 0, realChar); + } }