Search code examples
pythonrecursionartificial-intelligencegreedy

How can I test if my Tic Tac Toe A.I. is perfect?


I made a tic tac toe A.I. Given each board state, my A.I. will return 1 exact place to move. (Even if moves are equally correct, it chooses same one every time, it does not pick a random one)

I also made a function that loops though all possible plays made with the A.I.

So it's a recursive function that lets the A.I. make a move for a given board, then lets the other play make all possible moves and calls the recursive function in it self with a new board for each possible move.

I do this for when the A.I goes first, and when the other one goes first... and add these together. I end up with 418 possible wins and 115 possible ties, and 0 possible loses.

But now my problem is, how do I maximize the amount of wins? I need to compare this statistic to something, but I can't figure out what to compare it to.


Solution

  • Did you read article on wikipedia? link

    Number of terminal positions

    When considering only the state of the board, and after taking into account board symmetries (i.e. rotations and reflections), there are only 138 terminal board positions. Assuming that X makes the first move every time:

    • 91 unique positions are won by (X)
    • 44 unique positions are won by (O)
    • 3 unique positions are drawn

    Number of possible games

    Without taking symmetries into account, the number of possible games can be determined by hand with an exact formula that leads to 255,168 possible games. Assuming that X makes the first move every time:

    • 131,184 finished games are won by (X)
    • 77,904 finished games are won by (O)
    • 46,080 finished games are drawn

    You may generate 138 terminal board positions from first paragraph

    or

    Yo may run enough tests on random fields and compare your results with statistics from here link

    Win in 5 moves    1440     0.6%
    Win in 6 moves    5328     2.1%
    Win in 7 moves    47952   18.8%
    Win in 8 moves    72576   28.4%
    Win in 9 moves    81792   32.1%
    Draw              46080   18.1%    
    Total             255168 100.0%