I have this program for minimax tic tac toe
import java.io.*;
import java.util.*;
public class TicTacToe implements Serializable {
String state; //State of the board such as 1, 2, 3, 4, 12, 13, etc.
Integer score; //Score for that state
static ArrayList <TicTacToe> States; //AllCombinations
static final long serialVersionUID = -5036364614491205547l;
static String board = "41532"; //Represents the state of the board, in this case, first player put his piece in fourth spot, then second at first, etc.
public static void minimax (int start, int end, int start1, int end1, int operation) { //Start and end is where a specific size state can be found, I have comments in the bottom. For example 0-8 is only states with size 1, 81-584 is size 3.
ArrayList <Integer> scores = new ArrayList <> ();
for (int i = start; i <= end; i++) { //Start looking in array list where only a specific size state exists
if(States.get(i).state.substring(0, board.length()).equals(board) && States.get(i).score == null) { //Check if that state has the board state in its current state and if it has score
for (int j = start1; j <= end1; j++) { //Check one size higher state
if(States.get(j).state.substring(0, States.get(j).state.length() - 1).equals(States.get(i).state)) { //If it also has the board state, add the score to a list
scores.add(States.get(j).score);
}
}
}
if(!scores.isEmpty()) {
if(operation == 0) {
States.get(i).score = Collections.max(scores); //Get max
} else {
States.get(i).score = Collections.min(scores); //Get min
}
scores.clear();
}
}
}
public static void main (String [] args) throws IOException {
try {
FileInputStream readData = new FileInputStream("States.txt");
ObjectInputStream readStream = new ObjectInputStream(readData);
States = (ArrayList<TicTacToe>) readStream.readObject(); //Get states and size from file
readStream.close();
} catch (Exception e) {
e.printStackTrace();
}
minimax(221625, 421191, 421192, 549063, 0); //So for example, I am going through all states of size 8 and using the minimax method, I am assigning scores based on scores of a state that's size 9, a terminal state
minimax(73449, 221624, 221625, 421191, 1); //Then size 7 scores from size 8
}
}
//0-8 Size one states
//9-80 Size two states
//81-584
//585-3608
//3609-18728
//18729-73448
//73449-221624
//221625-421191
//421192-549063 Size nine states
So basically this is my implementation of a minimax method, it starts from terminal states that are size nine and assigns it to states that are size eight if they don't have a score already. Then from score eight to seven. I had a different method but they are both slow when the board is empty. I need it to be far quicker, any suggestions or better data structures. The file it reads it from has states in ascending order so like 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 13, etc.
States.txt
or add the code that generated it? I'd like to try this out. \$\endgroup\$