Renamed TTT to TicTacToe, fixed grammer and spelling

This commit is contained in:
lieght
2025-09-15 20:29:37 +02:00
parent aac96cc57c
commit 27423d3337
2 changed files with 14 additions and 18 deletions

View File

@@ -4,15 +4,11 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.toop.Main; import org.toop.Main;
/* public class MinMaxTicTacToe {
* TTT = TIC TAC TOE FOR THE LESS EDUCATED POPULATION ON THIS CODE
*/
public class MinMaxTTT {
private static final Logger logger = LogManager.getLogger(Main.class); private static final Logger logger = LogManager.getLogger(Main.class);
public int findBestMove(TTT game) { public int findBestMove(TicTacToe game) {
/** /**
* This method tries to find the best move by seeing if it can set a winning move, if not, it will do a minimax. * This method tries to find the best move by seeing if it can set a winning move, if not, it will do a minimax.
*/ */
@@ -22,7 +18,7 @@ public class MinMaxTTT {
// simulate all possible moves on the field // simulate all possible moves on the field
for (int i = 0; i < game.grid.length; i++) { for (int i = 0; i < game.grid.length; i++) {
if (game.validateMove(i)) { // check if the move is legal here if (game.validateMove(i)) { // check if the move is legal here
TTT copyGame = game.copyBoard(); // make a copy of the game TicTacToe copyGame = game.copyBoard(); // make a copy of the game
State result = copyGame.playMove(i); // play a move on the copy board State result = copyGame.playMove(i); // play a move on the copy board
int thisMoveValue; int thisMoveValue;
@@ -42,7 +38,7 @@ public class MinMaxTTT {
return bestMove; // return the best move when we've done everything return bestMove; // return the best move when we've done everything
} }
public int doMinimax(TTT game, int depth, boolean maximizing) { public int doMinimax(TicTacToe game, int depth, boolean maximizing) {
/** /**
* This method simulates all the possible future moves in the game through a copy in search of the best move. * This method simulates all the possible future moves in the game through a copy in search of the best move.
*/ */
@@ -50,17 +46,17 @@ public class MinMaxTTT {
if (state) { if (state) {
if (maximizing) { if (maximizing) {
// its the maximizing players turn and someone has won. this is not good, so return a negative value // it's the maximizing players turn and someone has won. this is not good, so return a negative value
return -10 + depth; return -10 + depth;
} else { } else {
// it is the turn of the ai and it has won! this is good for us, so return a positive value above 0 // it is the turn of the AI and it has won! this is good for us, so return a positive value above 0
return 10 - depth; return 10 - depth;
} }
} }
else { else {
boolean empty = false; boolean empty = false;
for (char cell : game.grid) { // else, look at draw conditions. we check per cell if its empty or not for (char cell : game.grid) { // else, look at draw conditions. we check per cell if it's empty or not
if (cell == ' ') { if (cell == ' ') {
empty = true; // if a thing is empty, set to true empty = true; // if a thing is empty, set to true
break; // break the loop break; // break the loop
@@ -71,11 +67,11 @@ public class MinMaxTTT {
} }
} }
if (maximizing) { // its 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 int bestVal = -100; // set the value to lowest as possible
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)) {
TTT copyGame = game.copyBoard(); TicTacToe copyGame = game.copyBoard();
copyGame.playMove(i); // play the move on a copy board copyGame.playMove(i); // play the move on a copy board
int value = doMinimax(copyGame, depth - 1, false); // keep going with the minimax int value = doMinimax(copyGame, depth - 1, false); // keep going with the minimax
bestVal = Math.max(bestVal, value); // select the best value for the maximizing player (the AI) bestVal = Math.max(bestVal, value); // select the best value for the maximizing player (the AI)
@@ -88,7 +84,7 @@ public class MinMaxTTT {
int bestVal = 100; // set the value to the highest possible int bestVal = 100; // set the value to the highest possible
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)) {
TTT copyGame = game.copyBoard(); TicTacToe copyGame = game.copyBoard();
copyGame.playMove(i); // play the move on a copy board copyGame.playMove(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 = Math.min(bestVal, value); // select the lowest score for the minimizing player, they want to make it hard for us bestVal = Math.min(bestVal, value); // select the lowest score for the minimizing player, they want to make it hard for us

View File

@@ -4,11 +4,11 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.toop.Main; import org.toop.Main;
public class TTT extends GameBase { public class TicTacToe extends GameBase {
public int moveCount; public int moveCount;
private static final Logger logger = LogManager.getLogger(Main.class); private static final Logger logger = LogManager.getLogger(Main.class);
public TTT(String player1, String player2) { public TicTacToe(String player1, String player2) {
super(3); // 3x3 Grid super(3); // 3x3 Grid
players = new Player[2]; players = new Player[2];
players[0] = new Player(player1, 'X'); players[0] = new Player(player1, 'X');
@@ -79,11 +79,11 @@ public class TTT extends GameBase {
return false; return false;
} }
public TTT copyBoard() { public TicTacToe copyBoard() {
/** /**
* This method copies the board, mainly for AI use. * This method copies the board, mainly for AI use.
*/ */
TTT clone = new TTT(players[0].Name(), players[1].Name()); TicTacToe clone = new TicTacToe(players[0].Name(), players[1].Name());
System.arraycopy(this.grid, 0, clone.grid, 0, this.grid.length); System.arraycopy(this.grid, 0, clone.grid, 0, this.grid.length);
clone.moveCount = this.moveCount; clone.moveCount = this.moveCount;
clone.currentPlayer = this.currentPlayer; clone.currentPlayer = this.currentPlayer;