This game is definitely small enough for brute force.
You can simply list all the states. There are 16 squares with 3 possible values ββfor each square (X, O, or empty).
3 ^ 16 = 43046721, about 43 million.
This means that a table with one byte to describe the gain of each state will be equal to only 43 megabytes.
I would create a function that matches each state with an index between one and 43 million (you only need states, not possible playback orders), basically representing it as a number in base-3 and letting you create a state from the index.
Choose 4 win values, each state can take - win for O, win for X, not winning and unknown.
Highlight a buffer 43046721 in length to save the winnings in each game state.
Iterate over all index numbers, indicating won states. Then go through and iteratively fill the winnings of each of the other states, if they are known (check all successor states based on who does it). It will take no more than 16 iterations on a set of indices, so I see no reason why brute force does not work here.
There is an optimization, for example, the use of symmetry, using the fact that all states with n parts are reduced with n + 1 pieces, etc., but I donβt think you need it at first.
source share