Coding Challenge 154: Tic Tac Toe AI with Minimax Algorithm

In this challenge I take the Tic Tac Toe game from coding challenge #149 and add an AI opponent for a human player by implementing the Minimax algorithm.

🎥 Coding Challenge #149: Tic Tac Toe:

Links discussed in this video:
🔗 Minimax:
🔗 Minimax Algorithm in Game Theory:…


  1. cubed tic-tac-toe sounds great, initially. but…how would one pick the middle??

  2. Hey by Minimax Algo the user will never win and eventually throw our game. How can we create the EASY MEDIUM DIFFICULT modes of this ? Please guide a little !

  3. I have written the same code for java with some change in syntax but the AI always plays in the order and never plays as it should do.
    In my version, the player plays first and then the AI. Should I change something with the minimax function or should I let it be the same?

    Any help would be admired

    Edit: I fixed the issue, and also converted the logic to a playable game for Android. Soon it will be available in Android

  4. amazing! just asking a thing. do we really need the isMaximising parameter??? to know ismaximising, we could use depth%2==0?

  5. Quick question please. Hey, this has helped a lot – I've implemented it in C++, well trying – my game can do a larger custom sized grid and the 3 in a row is also custom. i.e. win checks of 2-5 in a row. For some reason it only puts a O in the first box, and everything looks good. I've been here for 5 hours now without looking for help.

  6. Hi there, what is your color theme? I cannot seem to find it but i like it 🙂

  7. Hm, the code seems to have a very strong resemblance to the boilerplate code listing in the book "Artificial Intelligence for Games".

  8. "move = {i, j};
    board[move.i][move.j] =ai;"
    Hello, im new at coding and im trying to adapt this code to python but i cant find a way to adapt the code above. I tried something like: "
    hamle_X = i

    hamle_Y = j

    setO(hamle_X, hamle_Y)
    but this didnt work. My code just put O's to the empty cells respectively. Any help? Thanks.

  9. Please expert. Is there somewhere a tic-tac-toe code that plays against itself. And learns from scratch . It progresses beyong human thought . It would be zero players.

  10. I have been trying tic tac toe minimax for weeks and i still can't figure it out…

  11. This guy thanking vscode for everything it fixes for him and everytime it points him to an error is the only thing keeping the human race from being dominated by AI.
    Loved it, subscribed

  12. I really like how you presented, so fun and lively. But please be more accurate next time, minimize your wrong coding by syntax or logic. I meant, you could write the code before. Memorize it. Then presented it, so the viewer will not get confused/tired. But once again thanks for the great video

  13. This video is a stereotype for all non – AI programmers telling , “AI is all if else statement”

  14. So many possibilities at the beginning of the game? Baloney. They're all variants of three possible moves.
    Having never coded a program like this one, I had assumed that the AI would set a rotation variable and translate the game board back and forth on the fly. A brute force algorithm could then play the game. It wouldn't be difficult to write–the tough part would be the rotation values.

  15. I've wrote a form of minimax function in my own board game. I used depth in a reverse way, instead of starting at 0 and incrementing I started at a set value and decremented, when the minimax function was called with a depth of 0 it would just make a random move. Doing this let me use the depth variable as a Difficulty setting for different AI. Easy had a depth of 0 (so it just played randomly), Novice had a depth of 1, Intermediate was randomly either depth 1 or 2, Hard was depth 2 or 3, and Insane was always depth 4.

Leave a Reply

Your email address will not be published. Required fields are marked *