From 88d559c6c9a46f36f7c5d3f4a05ef91ab6c1bb7a Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Mon, 31 Jan 2022 12:21:54 +0100 Subject: [PATCH] tictactoe: added hard AI --- .../java/de/tims/tictactoe/ai/AIHard.java | 22 ++++++++++++++ .../java/de/tims/tictactoe/ai/AIHardTest.java | 30 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/main/java/de/tims/tictactoe/ai/AIHard.java create mode 100644 src/test/java/de/tims/tictactoe/ai/AIHardTest.java 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); + } + +}