Browse Source

tictactoe: added hard AI

tictactoe
Tobias Krause 3 years ago
committed by Lorenz Hohmann
parent
commit
88d559c6c9
  1. 22
      src/main/java/de/tims/tictactoe/ai/AIHard.java
  2. 30
      src/test/java/de/tims/tictactoe/ai/AIHardTest.java

22
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);
}
}

30
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);
}
}
Loading…
Cancel
Save