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)...
The computer also needs to check for game over conditions...
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.
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:
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
Randomising 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 |