AI to tictactoe

I'm currently working on a tictactoe as a school project, but I want to be sure to get max score from the project, so I want to spice up my current project a little bit. I have pretty decent looking basic tictactoe game that is player vs "computer". The computer itself can't be called AI, because it's simply random values from random generator looking to be upgraded into an actual AI)

But the real question is: How can I approach creating AI for the game ?(yes I have heard about minimax, but couldn't really find a way to "insert" this into my project with entirely understanding every bit of it.)

My current "matrix" looks like this, but I've implemented values 1-9 (as char's)
00 01 02 1 2 3
10 11 12 4 5 6
20 21 22 7 8 9

Is it too much work to just consider every possible move there is and write them all up to make the AI unbeatable?

If there are useful books on the subject that are publicly available I would love to hear about them aswell.

Am mainly hoping for different approaches/perspectives on the situation, not complete solutions for me to copypaste - so I can actually learn a thing or two from this.
Thanks alot!
The code itself is very long (at least 600+ lines) so didn't post it. And the interface is made in finnish. Can post though incase someone wants/needs to see it
I have done AI for tic tac toe a couple of times and always follow the same logic.

1. check for win.
create a function that checks for two in a row and have the AI place the third space to win.

If a win is not possible...

2. check for block.

create a function that checks for 2 in a row of opponents. if so have the AI go for the block.

if a block is not needed yet...

3. check for double.

this function is a bit tricky but basically you have the AI place a move that creates a two way possible win situation.

if trapping your opponent in a two way is not yet possible..

4. prevent double.

this function checks to see if the opponent has a way to create a two way win situation and spoils it for them.

if all of the previous functions are not valid then...

5. make random move.

Following this logic will create a challenging AI, but not an unbeatable AI as usually the computer's first move is random and can allow the human a win.

As for the option of considering every possible move... it is not really too much work, but it does make the computer unbeatable. Not fun for human players.

Thank you very much!
Was exactly reply I was looking for and that really did help me alot!
TTT is the classic example of 'transposition tables'. The idea here is that (first move)


is the same position as


is the same as

and again at

so you can apply the exact same decision to each of those if you look at the board as if rotated, mirrored, or flipped, etc.

I forget the exact number of positions but it is actually quite small so you can code them all in followed by the next best move. This leads to a boring computer player that never makes a mistake, draws or wins all games.
Last edited on
Registered users can post here. Sign in or register to post.