AI TTT merge to ServerManager branche (#24)

* AI made and also a minor change to core board

* Minor changes to the AI, Gamebase changed a little to make some stuff public for usability, TTT changed a few things to public too and making the tests for the AI

* New test on the AI

* fixed some questionable styling in my code

* fixed some questionable styling in omar's code!

* fixed some questionable styling in omar's code! v2

* Update src/main/java/org/toop/game/MinMaxTTT.java

* Wrong value, was 9x9 grid, is now 3x3 grid

---------

Co-authored-by: michiel <m.brands.3@st.hanze.nl>
This commit is contained in:
Bas Antonius de Jong
2025-09-15 20:12:25 +02:00
committed by GitHub
parent 83b4038b2b
commit 34401dd35f
5 changed files with 210 additions and 16 deletions

View File

@@ -0,0 +1,78 @@
import org.junit.jupiter.api.Test;
import org.toop.game.MinMaxTTT;
import org.toop.game.TTT;
import static org.junit.jupiter.api.Assertions.*;
class MinMaxTTTTest {
// makegame makes a board situation so we can test the ai. thats it really, the rest is easy to follow id say
private TTT makeGame(String board, int currentPlayer) {
TTT game = new TTT("AI", "Human");
// Fill the board
for (int i = 0; i < board.length(); i++) {
char c = board.charAt(i);
game.grid[i] = c;
if (c != ' ') game.moveCount++;
}
game.currentPlayer = currentPlayer;
return game;
}
@Test
void testFindBestMove_AIImmediateWin() {
TTT game = makeGame("XX OO ", 0);
MinMaxTTT ai = new MinMaxTTT();
int bestMove = ai.findBestMove(game);
assertEquals(2, bestMove, "AI has to take winning move at 2");
}
@Test
void testFindBestMove_BlockOpponentWin() {
TTT game = makeGame("OO X ", 0); // 0 = AI's turn
MinMaxTTT ai = new MinMaxTTT();
int bestMove = ai.findBestMove(game);
assertEquals(2, bestMove, "AI should block opponent win at 2");
}
@Test
void testFindBestMove_ChooseDrawIfNoWin() {
TTT game = makeGame("XOXOX O ", 0);
MinMaxTTT ai = new MinMaxTTT();
int bestMove = ai.findBestMove(game);
assertTrue(bestMove == 6 || bestMove == 8, "AI should draw");
}
@Test
void testMinimax_ScoreWin() {
TTT game = makeGame("XXX ", 0);
MinMaxTTT ai = new MinMaxTTT();
int score = ai.doMinimax(game, 5, false);
assertTrue(score > 0, "AI win scored positively");
}
@Test
void testMinimax_ScoreLoss() {
TTT game = makeGame("OOO ", 1);
MinMaxTTT ai = new MinMaxTTT();
int score = ai.doMinimax(game, 5, true);
assertTrue(score < 0, "AI loss is negative");
}
@Test
void testMinimax_ScoreDraw() {
TTT game = makeGame("XOXOXOOXO", 0);
MinMaxTTT ai = new MinMaxTTT();
int score = ai.doMinimax(game, 5, true);
assertEquals(0, score, "Draw should be zero!");
}
@Test
void testMiniMax_MultipleMoves() {
TTT game = makeGame(" X OX O ", 0);
MinMaxTTT ai = new MinMaxTTT();
int bestMove = ai.findBestMove(game);
assertTrue(bestMove == 0 || bestMove == 2, "Can look at multiple moves!");
}
}