A typical tic-tac-toe algorithm should look like this:
Blackboard: A vector of nine elements representing a blackboard. We store 2 (indicating empty), 3 (indicating X) or 5 (indicating O). Turn: an integer indicating which course the game is going to play. The 1st move will be marked 1, the last - 9.
Algorithm
The main algorithm uses three functions.
Make2: returns 5 if the central square of the board is empty, i.e. if board[5]=2 . Otherwise, this function returns any non-corner square (2, 4, 6 or 8) .
Posswin(p) : returns 0 if player p wins the next turn; otherwise, the square number is returned, which is the winning move. This feature will allow the program to both win and block the victory of opponents. This function works by checking each row, column and diagonal. Multiplying the values of each square together for the entire row (or column or diagonal), you can check the possibility of winning. If the product is 18 ( 3 x 3 x 2 ), then X can win. If the product is 50 ( 5 x 5 x 2 ), then O can win. If a winning row (column or diagonal) is found, you can define an empty square in it and return the number of this square of this function.
Go (n) : makes a move in square n. this procedure sets the board [n] to 3 if the rotation is odd, or 5 if the rotation is even. It also increases the rotation by one.
The algorithm has a built-in strategy for each move. He makes an odd move if he plays X , an even move if he plays O.
Turn = 1 Go(1) (upper left corner). Turn = 2 If Board[5] is blank, Go(5), else Go(1). Turn = 3 If Board[9] is blank, Go(9), else Go(3). Turn = 4 If Posswin(X) is not 0, then Go(Posswin(X)) ie [ block opponents win], else Go(Make2). Turn = 5 if Posswin(X) is not 0 then Go(Posswin(X)) [ie win], else if Posswin(O) is not 0, then Go(Posswin(O)) [ie block win], else if Board[7] is blank, then Go(7), else Go(3). [to explore other possibility if there be any ]. Turn = 6 If Posswin(O) is not 0 then Go(Posswin(O)), else if Posswin(X) is not 0, then Go(Posswin(X)), else Go(Make2). Turn = 7 If Posswin(X) is not 0 then Go(Posswin(X)), else if Posswin(X) is not 0, then Go(Posswin(O)) else go anywhere that is blank. Turn = 8 if Posswin(O) is not 0 then Go(Posswin(O)), else if Posswin(X) is not 0, then Go(Posswin(X)), else go anywhere that is blank. Turn = 9 Same as Turn=7.
I used this. Let me know how you guys feel.