Understanding the minmax algorithm of tic-tac-toe (without recursion)

243 views Asked by At

I'm trying to write a minimax algorithm for a tic-tac-toe at Python.
I don't need help with the code, just with the algorithm... :-)
I'm trying to do it without recursion, and to understand it better I look at this answer

So, my algorithm is this:
Lets say that em is a list of all the empty squares at the board.
'x' - is the human (or the computer) that palying, and 'o' - is the tree (of the minimax algorithm).
So, the input of the algorithm is a tic-tac-toe board, and the output as the square of the next move...

I also use dictionary for every empty square, to put there the score for deciding the next move. We will call it map...

The algorithm

  1. Takes all the permutations of em at size len(em)
  2. For every permutation do this:
  3. from the first element at the permutation put 'x' or 'o', e.g. if the permutation is (2,4,6,7) so the algorithm will put 'o' at 2 (because it's the tree turn) and then 'x' at 4 and so on...

  4. If 'x' wins at the permutation (it can be at the middle, we don't need to fill it until the end) map[the_first_element_at_the_permutation]-=10

  5. If 'o' wins at the permutation: map[the_first_element_at_the_permutation]+=10

After all this - we are looking for the element at map with the highest score and we return it as the square for the next move....

Unfortunately - It doesn't work... It's works for many cases, but there are few cases that it's doesn't working....
For example:
x.. .o. ..x
Now it the turn of the tree, and it will put 'o' here:
x.. .o. o.x
And it's not good because I can put 'x' at the top-right corner and I'll win....


What you suggest me to do? Why my algorithm doesn't work? (I work on it for a few days and I don't know hat to do....)

0

There are 0 answers