 Allow me to explain...

The game allows the user, then the computer to choose squares on a tic-tac-toe board. The difficulty in the scripting lies in the need to analyse the positions on the board. The computer must choose a move based on the following possibilities (in order of preference)...

• can the computer get three in a row (WIN)
• does the computer need to block the player from winning
• can the computer create a split (two opportunities to win on the next move)
• can the computer create an opportunity to win on the next move

The computer also needs to check for game over conditions...

• has the player got three in a row after his move
• has the computer got three in a row
• are all the positions filled without a winner (draw)

There are a few other things to be wary of — some positions (corners & centre) are more advantageous than others, so the computer needs to 'prefer' these positions. If two different moves of equal value are possible, the computer needs to randomly choose between them. If you were playing the game against another person, you would take turns going first. It would be nice if the Flash version alternated between player-first, computer-first games.

How the scripting works.
The status of each position can be found by checking the frame number of the movie clip in each position. Frame 1 is an empty position, 2 is a nought (player), 3 is a cross (the computer). These movie clips are examined and a value is calculated for each row, each column and each diagonal. These values are set as follows:
the initial value is zero
a value of 1 is added for each nought in the row (player)
a value of 1 is subtracted for each cross in the row (computer)
...therefor it follows that the possible values for any row, column or diagonal are:

 --- 0 --O 1 --X -1 -OO 2 -XX -2 -OX 0 -XO 0 OOO 3 XXX -3 OOX 1 OXX -1

When the computer needs to choose its next move, it examines all the squares on the board. For each square, it chooses a ranking value (the higher the rank - the better the move). If a square is already occupied, it is given a ranking of -999 (very low).

If the square is not occupied, the computer gives an initial value of 0, then examines the row, column and if appropriate, diagonal(s) that intersect the position and adds the following values to the rank:

• 20 if the row, column or diagonal has a value of -2 (one more X on the row wins)
• 5 if the row, column or diagonal has a value of 2 (one more O on the row loses)
• 1 if the row, column or diagonal has a value of -1 (two more X's on the row wins)

These values are added for each row, column or diagonal that is considered, therefore a position that gives two chances to win would be ranked 1+1 = 2.

Preferred Positions
An offset value is added to the rank according to the following table. This allows that in a situation where two positions have equal rank, the corner or centre positions would be preferred:

 0.5 0 0.5 0 0.25 0 0.5 0 0.5

Randomising
A very small (0 ... 0.1) random value is added to the ranking. This ensures that if two positions have the same value even after adding the preferred position offset, the computer won't always choose the same position.

There is also a special case implemented when the computer has the second move in a game. The second move will always be completely random, in order to remove some predictability in what the computer will do.

Hopefully this makes the whole thing clearer! - CleverPig