#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# minimax algorithm for tictactoe AI

Member

i've made a tictactoe game and then i made a AI for the game.but its not working correctly.

expected result :- pick a place to win the game or not let the opponent win the game.

given result :- pick the first available spot from the board array.

here is the script that controls everything.

any help will be appreciated...

• Member

Hey there!

It'd take some time for me to figure out the problem from looking at the code. I can do that, if you really need it, but I'd like to help you find it yourself first, though.

Have you tried filling your methods with Debug statements / breakpoints to see what happens in the decision process?

Have you tried isolating the AI decision? You could make a console project where you just have the AI making decisions based on a board state, so that you isolate any potential exterior influence.

You could take it one step further and test that each method does what it is supposed to with each kind of input.

If you want me to look properly, I'd like to ask you to make some comments about what does what and why. Also your naming is a bit inconsistent, so it's a bit difficult to read ^^;

• Member
edited September 17

@Sl0th i tried like you said about debug.log thing and i found that everything is working score is also changing but at the end it returns -1 so nothing happens.it pick the first available spot.

Any idea about what can it be

• Member

What exactly returns -1?

• Member

@Sl0th the only way to get of the recursive minimax loop is to return a value by checking that if in the current recursive depth ai wins loses or tie .the values are diffrent for this conditions so if the ai wins the it return +1 lose will give -1 and tie will give 0 and it happens several times and calculate the best score for that depth and return a value and also checks for the best between all possible available spaces to abstract the best place to move. And i think there is a problem with the state checking but when i check for if it is the bug in 2 player tictactoe mode it turned out to be 100% working.so the problem must be in the checking for state in the minimax algorithm.