Where to begin with Tic Tac Toe?

I’m currently working on the Tic Tac Toe project, and I’m stuck with where to even begin? I understand that the most effective method for writing a decent AI for this would be using some sort of min max algorithm. However, I haven’t been able to find a decent example of a min max algorithm for tic tac toe that explains it from the ground up.
From what I’m understanding, a min max algorithm would essentially look at all possible moves the computer could make, then all the possible moves the other play could make, then could be made from there, and so on and so forth. Then it would figure out if any of those lead to it, within the next turn winning. If so, do that. If not, is there any plays with the other player could make which would result in them winning? If so, block that. Then try and find the next optimal move.

Largely my issue seems to be stemming from the inability to wrap my head around this. I understand the theory behind it, but how do I go about applying it in actual Javascript? So far with all the FCC projects, I’ve at least known where I can start, and then built off from there until I kinda just stumbled across the solution to the problem, but here I don’t even know where to begin with developing the algorithm. How can I tell if the computer is close to winning? How can I tell if the other player can win on their next turn?

Does anyone know any good places to go to go and get some good examples of minmax algorithms for tic tac toe? A lot of the ones I’m finding aren’t in Javascript, and thus have syntax which I don’t understand. The example project also doesn’t make sense to me. Is there any examples which break this sort of thing down into smaller chunks so it can be better understood?
Any help would be greatly appreciated! :slight_smile:

have you done with the look part ??? when you done with it, it really be come easier

I’m sorry but what is the look part?

i mean the visual part. i just finised my tic tac toe project recently.

I went for having an array of an array to record the board state and a number of functions that would do something with the information provided by the game state. My first approach involved counting the openings a particular square had, ie whether or not it was on a row that could still be completed by the computer’s team. But that wasn’t good enough so I wrote functions to determine whether a square would block an opponents move and whether a square would complete a win.

Basically, each function involved counting some property of a square, while a loop iterated and tracked the square with the highest score (using the sorts of functions I just mentioned).

Ah I understand. Yes, I pretty much have.

So did you actually create a min max algorithm or was it kind of something different?

No it was not really min max. Just enough code to make it playable.

tic tac toe has a pretty limited amount of states with only one good move in every of those states, you can simply hardcode them

1 Like