diff --git a/src/main/java/de/tims/tictactoe/ai/AIHard.java b/src/main/java/de/tims/tictactoe/ai/AIHard.java new file mode 100644 index 0000000..2165b1d --- /dev/null +++ b/src/main/java/de/tims/tictactoe/ai/AIHard.java @@ -0,0 +1,22 @@ +package de.tims.tictactoe.ai; + +import de.tims.tictactoe.GameLogic; + +public class AIHard implements TicTacToeAI { + private static final char AI_CHAR = 'o'; + private static final char EMPTY_CHAR = '-'; + + private GameLogic gl; + + public AIHard(GameLogic gl) throws IllegalArgumentException { + if (gl.getBoard().length != 3) { + throw new IllegalArgumentException("Hard AI only supports 3x3 boards!"); + } + this.gl = gl; + } + + @Override + public void calculateNextMove() { + gl.setField(1, 1, AI_CHAR); + } +} diff --git a/src/test/java/de/tims/tictactoe/ai/AIHardTest.java b/src/test/java/de/tims/tictactoe/ai/AIHardTest.java new file mode 100644 index 0000000..c52ac3f --- /dev/null +++ b/src/test/java/de/tims/tictactoe/ai/AIHardTest.java @@ -0,0 +1,30 @@ +package de.tims.tictactoe.ai; + +import static org.mockito.Mockito.*; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import de.tims.tictactoe.GameLogic; + +@ExtendWith(MockitoExtension.class) +class AIHardTest { + static int size = 3; + + @Mock + private GameLogic gl; + + @Test + void emptyBoardChooseMiddleField() { + char realChar = 'o'; + doReturn(new char[][] { {'-', '-', '-'}, {'-', '-', '-'}, {'-', '-', '-'} }).when(gl).getBoard(); + + TicTacToeAI ai = new AIHard(gl); + ai.calculateNextMove(); + + verify(gl, times(1)).setField(1, 1, realChar); + } + +}