View Full Version : tictactoe AI

07-21-2003, 02:31 PM
hello everyone !

i'm trying to create a flash tictactoe game in which the user fights against the computer.

[ ] [ X ] [ ]
[ ] [ o ] [ o ]
[ X ] [ o ] [ ]

... things like that. the computer handles the O ... the aim is to be the first to have its three signs aligned either horizontally, vertically or in diagonals.

I'm trying to design the AI, but my Natural Intelligence does not foresee the way... :(

how do u program the desire to win in the least number of attempts?

It will always be the computer that will start.

At first, i thought about storing the values in a nine-element array, gradually replacing each eleemnt with either 'X' or 'O'. Each time it would be the computer's turn, it would pick a random number in between the remaining available slots in the array.
But 'random' is not the fastest way to win ( it's more Artificial Dumbness :D )

any idea on how to proceed ?

thanks for your help,


07-21-2003, 11:55 PM
AI is too abstract for this game, in my opinion~ ;)

commonly such games should make deep search for computer's turn, chess game is an instance. but this takes much calculation and time which Flash could hardly handle with (yup Flash may make it but you got less eficient).

and, just fixed to this game, there is a userful way which counting weights for each step, mmm, it seems hard to say it entirely, maybe you could find some acticles on this field, or some days later i wil make one tictactoe and explain the theory with the game, it will be much simple~ :)

07-22-2003, 08:13 AM
a winning row would be something like

[x][][x] or [][x][x]. You will need 8 - 1 for each row and 1 for each column and 1 for each diagonal.

You need to create 8 empty sets to start with and query, as you go along, which has two opponents entries, then pop your 'o' in there...this will win sometimes but a good player can draw with second move or might win if they get first move.

07-22-2003, 08:47 AM
The AI for Tic Tac Toe is not really AI because there are so limited possibilities. The easiest way is to create an array with all the possible board positions and than pick the best option for that board pos.

But I don't think you want that solution.

A simple AI for these kind of problems is to have two two dimensional arrays (3x3) One array has your current board position (the one you already have). The second is a score matrix. Initialize this score matrix with zeros. You create several for loops to loop through your board positions.

when there is one Cross (or Naught depending on which the computer uses) in a row (horizontal, vertical or diagonal) then add 1 point to each open space
when there are two Crosses (or Naughts depending on which the computer uses) in a row (horizontal, vertical or diagonal) then add 2 to the open space left.

After you have done this. You just pick the open space with the highest score in the score matrix.

It is a very simple AI but it works.