better human/ai selector with bot selection and depth on TicTacToeAIR

This commit is contained in:
michiel
2025-12-04 14:31:04 +01:00
parent a00d25f24a
commit 134c9a2fd8
18 changed files with 113 additions and 50 deletions

View File

@@ -25,10 +25,10 @@ public final class ReversiR extends AbstractGame<ReversiR> {
// TODO: Don't hardcore for two players :)
public record Score(int player1Score, int player2Score) {}
public ReversiR(Player<ReversiR>[] players) {
super(8, 8, 2, players);
public ReversiR(Player<ReversiR>[] players) {
super(8, 8, 2, players);
addStartPieces();
}
}
public ReversiR(ReversiR other) {
super(other);
@@ -53,8 +53,8 @@ public final class ReversiR extends AbstractGame<ReversiR> {
}
}
@Override
public int[] getLegalMoves() {
@Override
public int[] getLegalMoves() {
final ArrayList<Integer> legalMoves = new ArrayList<>();
int[][] boardGrid = makeBoardAGrid();
int currentPlayer = this.getCurrentTurn();
@@ -67,7 +67,7 @@ public final class ReversiR extends AbstractGame<ReversiR> {
}
}
return legalMoves.stream().mapToInt(Integer::intValue).toArray();
}
}
private Set<Point> getAdjacentCells(int[][] boardGrid) {
Set<Point> possibleCells = new HashSet<>();
@@ -76,7 +76,7 @@ public final class ReversiR extends AbstractGame<ReversiR> {
for (int deltaRow = -1; deltaRow <= 1; deltaRow++){ //orthogonally and diagonally
int newX = point.x + deltaColumn, newY = point.y + deltaRow;
if (deltaColumn == 0 && deltaRow == 0 //continue if out of bounds
|| !isOnBoard(newX, newY)) {
|| !isOnBoard(newX, newY)) {
continue;
}
if (boardGrid[newY][newX] == EMPTY) { //check if the cell is empty

View File

@@ -26,9 +26,15 @@ public final class TicTacToeAIR extends AbstractAI<TicTacToeR> {
* @param game the current Tic-Tac-Toe game state
* @param depth the depth of lookahead for evaluating moves (non-negative)
* @return the index of the best move, or -1 if no moves are available
*
*/
private int depth;
public TicTacToeAIR(int depth) {
this.depth = depth;
}
public int getMove(TicTacToeR game) {
int depth = 9;
assert game != null;
final int[] legalMoves = game.getLegalMoves();

View File

@@ -10,7 +10,7 @@ import static org.junit.jupiter.api.Assertions.*;
final class TicTacToeAIRTest {
private final TicTacToeAIR ai = new TicTacToeAIR();
private final TicTacToeAIR ai = new TicTacToeAIR(9);
// Helper: play multiple moves in sequence on a fresh board
private TicTacToeR playSequence(int... moves) {