Renaming and formatting

This commit is contained in:
lieght
2025-09-21 16:49:15 +02:00
parent 6e35993dc2
commit 174c24d935

View File

@@ -1,23 +1,19 @@
package org.toop.game.tictactoe.ai; package org.toop.game.tictactoe.ai;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.game.tictactoe.GameBase; import org.toop.game.tictactoe.GameBase;
import org.toop.game.tictactoe.TicTacToe; import org.toop.game.tictactoe.TicTacToe;
public class MinMaxTicTacToe { public class MinMaxTicTacToe {
private static final Logger logger = LogManager.getLogger(MinMaxTicTacToe.class);
/** /**
* This method tries to find the best move by seeing if it can set a winning move, if not, it * This method tries to find the best move by seeing if it can set a winning move, if not, it
* will do a minimax. * will do a minimax.
*/ */
public int findBestMove(TicTacToe game) { public int findBestMove(TicTacToe game) {
int bestVal = -100; // set bestval to something impossible int bestVal = -100; // set bestVal to something impossible
int bestMove = 10; // set bestmove to something impossible int bestMove = 10; // set bestMove to something impossible
int winningmove = -5; int winningMove = -5;
boolean empty = true; boolean empty = true;
for (char cell : game.grid) { for (char cell : game.grid) {
@@ -29,7 +25,6 @@ public class MinMaxTicTacToe {
if (empty) { // start in a random corner if (empty) { // start in a random corner
return switch ((int) (Math.random() * 4)) { return switch ((int) (Math.random() * 4)) {
case 0 -> 0;
case 1 -> 2; case 1 -> 2;
case 2 -> 6; case 2 -> 6;
case 3 -> 8; case 3 -> 8;
@@ -55,7 +50,7 @@ public class MinMaxTicTacToe {
TicTacToe opponentCopy = copyGame.copyBoard(); TicTacToe opponentCopy = copyGame.copyBoard();
GameBase.State opponentResult = opponentCopy.play(index); GameBase.State opponentResult = opponentCopy.play(index);
if (opponentResult == GameBase.State.WIN) { if (opponentResult == GameBase.State.WIN) {
winningmove = index; winningMove = index;
} }
} }
} }
@@ -69,8 +64,8 @@ public class MinMaxTicTacToe {
} }
} }
} }
if (winningmove > -5) { if (winningMove > -5) {
return winningmove; return winningMove;
} }
return bestMove; // return the best move when we've done everything return bestMove; // return the best move when we've done everything
} }
@@ -109,8 +104,9 @@ public class MinMaxTicTacToe {
} }
} }
int bestVal;// set the value to the highest possible
if (maximizing) { // it's the maximizing players turn, the AI if (maximizing) { // it's the maximizing players turn, the AI
int bestVal = -100; // set the value to lowest as possible bestVal = -100;
for (int i = 0; i < game.grid.length; i++) { // loop through the grid for (int i = 0; i < game.grid.length; i++) { // loop through the grid
if (game.validateMove(i)) { if (game.validateMove(i)) {
TicTacToe copyGame = game.copyBoard(); TicTacToe copyGame = game.copyBoard();
@@ -124,14 +120,13 @@ public class MinMaxTicTacToe {
// AI) // AI)
} }
} }
return bestVal;
} else { // it's the minimizing players turn, the player } else { // it's the minimizing players turn, the player
int bestVal = 100; // set the value to the highest possible bestVal = 100;
for (int i = 0; i < game.grid.length; i++) { // loop through the grid for (int i = 0; i < game.grid.length; i++) { // loop through the grid
if (game.validateMove(i)) { if (game.validateMove(i)) {
TicTacToe copyGame = game.copyBoard(); TicTacToe copyGame = game.copyBoard();
copyGame.play(i); // play the move on a copy board copyGame.play(i); // play the move on a copy board
int value = doMinimax(copyGame, depth - 1, true); // keep minimaxing int value = doMinimax(copyGame, depth - 1, true); // keep miniMaxing
bestVal = bestVal =
Math.min( Math.min(
bestVal, bestVal,
@@ -139,7 +134,7 @@ public class MinMaxTicTacToe {
// they want to make it hard for us // they want to make it hard for us
} }
} }
return bestVal;
} }
return bestVal;
} }
} }