I am currently working on a tic tac toe auto-mover game. I have come upon a decent algorithm that allows the computer to easily build towards, and claim victory spots.The problem however is that when the opponent is dangerously close to victory, the computer continues to build towards it's own victory instead of blocking the opponents, is there any way the following algorithm can be optimized to counter that?The algorithm works as follows:Initialize a 3x3 integer arrayLoop over all possible combinations on the board (0|0, 1|1, 0|2... e.tc).If combination is a immediate victory, stop and return this position.Else initiate a loop (That loops either 1000, 10000 or 100000 times, depending on difficulty)Continually move from both sides randomly till victory is achieved. If victory is by the computer, increment the score counter.Select the board position with the highest scoreThis algorithm works fine and can build towards victory (example:|x|o    |x|o    |x|o||    x||o    x|x|o||    ||    ||oHowever when confronted with a board like this|x|oo|x|||The optimal move should be to break x's chain, but the computer builds towards raising it's own chain i.e moving at 1|2 (The board is 3x3, indexes start from zero. So that'd be the second row's third coloumn).Is there any way to optimize this algorithm such that this weakness goes away?
How can the following algorithm be optimized?
algorithms
Sure there is a way. Your problem is that your definition of I win is too narrow. As you've pointed out, a row of three is only a win for you if your opponent doesn't already have one - therefore your metric must track potential rows achieved by you and your opponent, and avoid states in which they get a row completed first.