Coding an UNBEATABLE Tic Tac Toe AI (Game Theory Minimax Algorithm EXPLAINED)

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ส.ค. 2024
  • Use the Minimax Algorithm to code a tic tac toe AI that is literally unbeatable. It is undefeated. You can tie, but only if you play optimally. Find out what happens when you pit AI against AI.
    Want to learn Python if you are an absolute beginner? Check out my in-depth beginner series:
    • Python for the Complet...
    In this video, I explain how the minimax algorithm can be used to find the most optimal move in a game of tic tac toe. The algorithm never loses. Then, I delve into a technical explanation of my code and what each line represents.
    Feel free to leave any questions.
    Please consider subscribing if you liked this video: th-cam.com/users/ycubed?sub_...
    Thanks for watching everyone!
    ~~~~~~~~~~~~~~~~~~~~~~~~
    Source code: github.com/kying18/tic-tac-toe
    game.py - tic tac toe board implementation/playing a game
    player.py - player implementations
    ~~~~~~~~~~~~~~~~~~~~~~~~
    Tags: Minimax Algorithm, Game Theory, Tic Tac Toe, Artificial Intelligence, Machine Learning, Python, Software Engineering, Coding
    ~~~~~~~~~~~~~~~~~~~~~~~~
    Follow me on Instagram: / kylieyying
    Follow me on Twitter: / kylieyying
    Check out my website: www.kylieying.com
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 103

  • @charlie6618
    @charlie6618 3 ปีที่แล้ว +24

    please do more projects related to AI...
    Studying AI from an MIT graduate would be very cool and beneficial.
    Thank you for your effort!

  • @amriiheb
    @amriiheb 3 ปีที่แล้ว +4

    amazing explanation and great code .
    i am glad i found the algorithm and found this channel , you found yourself a new sub

  • @arm9180
    @arm9180 3 ปีที่แล้ว +17

    Thank you so much! Iv'e been stuck on the implementation of this algorithm for a while now, and I can't thank you enough for showing me a method that actually works. I subbed btw ❤

    • @KylieYYing
      @KylieYYing  3 ปีที่แล้ว +2

      🥰🥰

    • @javhaasuhochir8126
      @javhaasuhochir8126 2 ปีที่แล้ว +1

      I loved both you and your algorithm. your are beautiful

  • @tonyleung-to3oj
    @tonyleung-to3oj ปีที่แล้ว +3

    I am currently learning how to code a Tic Tac Toe by using python. Your video giving me such a great inspiration. Can you talk more about how to code the optimal solution by using python?

  • @rustyelectron
    @rustyelectron 2 ปีที่แล้ว +1

    Thanks, I was working on the vanilla minimax algorithm and the AI wasn't able to prioritize the quicker victory, your tip about the utility function helped.

  • @paulaugis
    @paulaugis 3 ปีที่แล้ว

    Great video ! You are helping a lot for my AI class haha ! Keep up the good work Kylie :)

  • @fernandocl4788
    @fernandocl4788 2 ปีที่แล้ว +1

    Really great video! Thanks so much for uploading this! Would've been amazing if you could have explained a bit more the whole minimax function :/ I subscribed to your channel now. You have great content

  • @jdeb0016
    @jdeb0016 2 ปีที่แล้ว

    Thank you for having kindly shared this fantastic implementation and explanation :)

  • @DJdopeMike
    @DJdopeMike 9 หลายเดือนก่อน

    Wow thank you for this wonderful tutorial, this is the first implementation of the minimax algorithm that actually worked for me!!!

  • @neerajmanjhi9376
    @neerajmanjhi9376 3 ปีที่แล้ว +2

    Thanks alot for inspiring me to learning more and more coding.

  • @enzi_r9810
    @enzi_r9810 3 ปีที่แล้ว +3

    HI, i tried , and it works really good with 3x3, but when it comes to 5x5 its not that efficient.. i tried using alpha-beta pruning, but somehow it doesn't work, the AI letting me win, any suggestion?

  • @oscareriksson9414
    @oscareriksson9414 2 หลายเดือนก่อน

    Very good tutorial. I was also wondering how to make a value from the states while going down the tree! I am trying it next!

  • @itssyaagiirl
    @itssyaagiirl 2 ปีที่แล้ว

    This video is soooo helpful thank you

  • @willmassey2679
    @willmassey2679 2 ปีที่แล้ว +9

    Great video. However, for user_input the allowable moves should be (0-8), and not (0-9) because there is no 9th position on the board.

  • @squad7953
    @squad7953 4 ปีที่แล้ว +1

    Very Very Amazingly well explained and concept with code implementation is superb !!!

    • @KylieYYing
      @KylieYYing  4 ปีที่แล้ว +1

      Thank you for your support! :)

  • @diegovelejador
    @diegovelejador 3 ปีที่แล้ว

    Liked a lot, thanks for the video.

  • @caitlinkeegan7822
    @caitlinkeegan7822 4 ปีที่แล้ว +5

    Thanks for this -- ready to win all of my tic-tac-toe games now!!!

    • @KylieYYing
      @KylieYYing  4 ปีที่แล้ว +3

      Amazing send me the results of your next tournament

  • @mateoslab
    @mateoslab 3 ปีที่แล้ว

    This video is awesome. I'm a c++ beginner and I want to try to implement this on a sudoku solver

  • @Tanklice
    @Tanklice ปีที่แล้ว

    Hi,do you have any article or something that can help me do the same thing but with quantum tic tac toe ????

  • @anidea8012
    @anidea8012 4 ปีที่แล้ว +4

    Congratulations you earned my subscription 😌❤️

  • @md.sabbirhossen6525
    @md.sabbirhossen6525 3 ปีที่แล้ว +1

    can anyone explain me how the code works inside the for loop and the recursion ??? please help me

  • @kushalappabe1975
    @kushalappabe1975 2 ปีที่แล้ว

    hello tried running your code but found an error module not found "player" tried resolving by pip install, re ran the code found another error pyramid.compat not found even the cmd couldnt find it. by the way tried in jupyter notebook. I am searching for a random player vs smart/unbeatable ai for over 1000 iterations to get the number of draws.

  • @theentirecircus6623
    @theentirecircus6623 ปีที่แล้ว

    Great explanation

  • @joaopedrooliveira9328
    @joaopedrooliveira9328 4 ปีที่แล้ว

    Hi! Very good explanation! :)
    Is there a way to use the same algorithm for more than two players? For example: in turn based RPG when the main character is against two enemies. Is it possible to consider that, for an enemy, the algorithm will have cycles of max-max-min ?

    • @KylieYYing
      @KylieYYing  4 ปีที่แล้ว +3

      Interesting question. I hadn’t thought about that. I suppose in this game, instead of two enemies, it would be more like two consecutive turns for the opponent. If the board were larger, I imagine you can use the same logic with the tree, but max max min as you mentioned.. I encourage you to do some additional reading about this.. I’m not totally sure!

    • @johnvonhorn2942
      @johnvonhorn2942 3 ปีที่แล้ว +2

      Hi Joao, wouldn't be just be max for the current player and min, min for the other players? Every player is trying to max their advantage and minimise all the other players. So I think you could extrapolate this to any number of players.

  • @fay-qp7cn
    @fay-qp7cn 4 ปีที่แล้ว +20

    Hi I really like the way you explain things in your videos! Would you be able to do more coding tutorials pls?

    • @KylieYYing
      @KylieYYing  4 ปีที่แล้ว +3

      Yeah sure! Is there anything specific that you’d like to see? I’m thinking of doing one about coding a website from scratch

    • @afewerkifkadu6070
      @afewerkifkadu6070 4 ปีที่แล้ว +1

      @@KylieYYing I really need that tutorial. please

    • @KylieYYing
      @KylieYYing  4 ปีที่แล้ว +4

      Afewerki Fkadu check out my latest video! It’s a website tutorial for Flask using Python. Next week I’ll be doing another website tutorial using VueJS

    • @KylieYYing
      @KylieYYing  4 ปีที่แล้ว +3

      Afewerki Fkadu th-cam.com/video/8q3qje9K5uU/w-d-xo.html

    • @KylieYYing
      @KylieYYing  4 ปีที่แล้ว +14

      Hi! Just letting you know that I posted a video on how I created my new personal website using Vue and Netlify!

  • @ahmedsaadtanim
    @ahmedsaadtanim 4 ปีที่แล้ว +2

    Hey...in minimax algorithm ..if we actually do the player vs computer game ..is the maximizer the player and the minimizer the computer? or is it the other way around or its a different thing?

    • @ahmedsaadtanim
      @ahmedsaadtanim 4 ปีที่แล้ว +2

      nevermind i found it by myself while tinkering with it...it seems maximizer minimizer can be either the player or the AI...the key factor is deciding find best move for whom

    • @KylieYYing
      @KylieYYing  4 ปีที่แล้ว +9

      Great question! You are the maximizer because you want to win in the end. This means BOTH the computer and the player are maximizers, trying to minimize their opponent’s total score. If we are the player playing against the computer, then we are the maximizer and to us, the computer is the minimizer. But if we look from the perspective of the computer, it would be the other way around, with the computer the max. Does that make sense?

    • @mohamedsahnoune8493
      @mohamedsahnoune8493 4 ปีที่แล้ว +1

      @@KylieYYing it totally make sence thank you

  • @keylanoslokj1806
    @keylanoslokj1806 2 ปีที่แล้ว

    What languages are you using on this project?

  • @thisweirdo2123
    @thisweirdo2123 3 ปีที่แล้ว

    Can i add gui in this code? Help

  • @dd1e952
    @dd1e952 3 ปีที่แล้ว

    Thank you very much!!!! >

  • @jawadaslam8537
    @jawadaslam8537 2 ปีที่แล้ว

    Thanks dear

  • @raheelamughal1242
    @raheelamughal1242 2 ปีที่แล้ว

    Hello ma'am! I'm trying to run your code files on Kaggle IDE. It is prompting error message("no module named player"). How can I fix this error?Thank you :)

    • @peterfarrell66
      @peterfarrell66 2 ปีที่แล้ว

      I had the same error. If you're pasting it all into one file or notebook, you don't need to import player.

  • @akashbhakta4397
    @akashbhakta4397 3 ปีที่แล้ว +1

    What’s the origin of the evil laugh at 12 seconds?

  • @shikhargupta105
    @shikhargupta105 2 ปีที่แล้ว

    Can we use minimax to find a move that would lead to a tie?

  • @whistletoe
    @whistletoe 4 ปีที่แล้ว +1

    I’m still a bit unclear on why you added 1 to the number of remaining blank squares before multiplying the sum by 1 or -1. Could you explain that again?

    • @KylieYYing
      @KylieYYing  4 ปีที่แล้ว

      I added the 1 basically as an extra incentive to achieve the “win” state earlier. So we choose the path in the tree that gets us the win in the least steps (since each step occupies a square)

  • @milene9403
    @milene9403 2 ปีที่แล้ว

    What other algorithm can be used?

  • @krasimirt6277
    @krasimirt6277 3 ปีที่แล้ว

    Can this be adapted for an 8x8 gomoku game

    • @KylieYYing
      @KylieYYing  3 ปีที่แล้ว +2

      Just minimax may be too slow but you could probably incorporate some alpha beta pruning and it should be adaptable

  • @kashish4136
    @kashish4136 2 ปีที่แล้ว

    Please provide me it's code link bcz i think it's not working now

  • @Larry21924
    @Larry21924 7 หลายเดือนก่อน

    This is eye-opening content. I read a book with a similar theme that was just as revolutionary. "Game Theory and the Pursuit of Algorithmic Fairness" by Jack Frostwell

  • @anlenguyenchi6716
    @anlenguyenchi6716 4 ปีที่แล้ว

    Oh my god! The best video ever!

  • @jvps1036
    @jvps1036 3 ปีที่แล้ว +1

    ❤️❤️❤️ sooo good❤️

  • @mohamedsahnoune8493
    @mohamedsahnoune8493 4 ปีที่แล้ว +2

    i have read that tic tac toe is a zero-sum game and each participant’s gain is equal to the other participants’ losses then shouldn't the minimiser and maximizer 's sum be zero ?

    • @KylieYYing
      @KylieYYing  4 ปีที่แล้ว +2

      It depends what you mean by sum. If you mean at the end of the game, the sum of. the opponent's "score" and your "score" is zero, then yes. However, this does not imply that the sum of all the scores in the tree should equal zero. Check out this article, it may help: towardsdatascience.com/tic-tac-toe-creating-unbeatable-ai-with-minimax-algorithm-8af9e52c1e7d

    • @programmingtestsandproje
      @programmingtestsandproje 4 ปีที่แล้ว +1

      @@KylieYYing thank you i have read the article and it took me a couple of days to completely understand how the algorithm works , this is my second channel i would love to hear your opinion on my videos i make tests and mini projects for beginners.

  • @ritakwan5679
    @ritakwan5679 2 ปีที่แล้ว

    Would you be so kind as to create a tic tac toe tutorial for just reg. folks who are just learning Python? Please & Thank you! 🙂

  • @amyapostol511
    @amyapostol511 4 ปีที่แล้ว +1

    NICE!!!

  • @second1799
    @second1799 6 หลายเดือนก่อน

    Oh what a top G! Thank you

    • @eluscara
      @eluscara หลายเดือนก่อน

      💀

  • @PhuongPham-vb6fn
    @PhuongPham-vb6fn 3 ปีที่แล้ว

    I see it still running without 'print_game=True' in 'def play'.
    Anyone can explain me. I'm beginner.
    Thanks a lot.

  • @codenull3755
    @codenull3755 3 ปีที่แล้ว +3

    You're like a funnier version of mayuko. Subbed

  • @BitBard302
    @BitBard302 10 หลายเดือนก่อน

    Having savored this, you'll find this book a gourmet of knowledge. "Game Theory and the Pursuit of Algorithmic Fairness" by Jack Frostwell

  • @duttulurihitheshkumar4800
    @duttulurihitheshkumar4800 3 ปีที่แล้ว

    can you make a video on how to apply pygame on this tictactoe game please

    • @jaredrosales8758
      @jaredrosales8758 3 ปีที่แล้ว +1

      could you do it? im having the same trouble and have no idea how to do it

  • @adnanprenuershorts
    @adnanprenuershorts 2 ปีที่แล้ว

    I am still facing errors in this help
    If name == 'main':
    NameError name 'name' is not defined

    • @peterfarrell66
      @peterfarrell66 2 ปีที่แล้ว +1

      Hi, don't forget the double underscores, they're kind of important. It's if ___name___ is '__main__':

  • @son_of_hiskingdom5092
    @son_of_hiskingdom5092 2 ปีที่แล้ว

    I'm currently pursuing python and wanting to learn to create and build and even get great pay for it. I'm trying to understand python right now.

  • @shanekillen7175
    @shanekillen7175 3 ปีที่แล้ว +1

    TypeError: object.__init__() takes exactly one argument (the instance to initialize), getting this error from the HumanPlayer class

    • @KylieYYing
      @KylieYYing  3 ปีที่แล้ว +2

      yeah you need to add "self" within the parenthesis

  • @KCUFyoufordoxingme
    @KCUFyoufordoxingme 3 ปีที่แล้ว

    When I was in 7th grade back in the 90's many kids did this with their graphing calculators.

  • @upper_moon1727
    @upper_moon1727 ปีที่แล้ว

    This is funny cuz i just finnished making my 356 line Tic tao game with my own bot that never loses either it's a tie or you make a mistake and it wins

  • @itssara8199
    @itssara8199 3 ปีที่แล้ว

    How to make an ai???

  • @whatnow1194
    @whatnow1194 3 ปีที่แล้ว

    what if both X and O moves decided by computer
    bot vs bot?
    who will win?

    • @KylieYYing
      @KylieYYing  3 ปีที่แล้ว +4

      No winner, it’s a tie

  • @darkden123
    @darkden123 ปีที่แล้ว

    not the tic tac toe esports finals

  • @ozguraydn8407
    @ozguraydn8407 2 ปีที่แล้ว

    Im in love

  • @uxoy
    @uxoy 3 หลายเดือนก่อน

    5:19

  • @KevinBozarth
    @KevinBozarth 3 หลายเดือนก่อน

    8:32

  • @delfish41
    @delfish41 3 ปีที่แล้ว

    omg why is it so hard for me to understand the code

  • @Kaazmaz3447
    @Kaazmaz3447 ปีที่แล้ว

    #

  • @nephilim18
    @nephilim18 หลายเดือนก่อน

    You didn't code $hit. It's a a lecture from cs50 ai.

  • @realneosi
    @realneosi 4 ปีที่แล้ว

    oooooooo

  • @puppergump4117
    @puppergump4117 2 ปีที่แล้ว +2

    I believe that no programming tutorial should be done in python, especially with complicated functions. There's so much implicit behavior it's hard to follow.

  • @KylieYYing
    @KylieYYing  4 ปีที่แล้ว +4

    Source code: www.​github.com/kying18/tic-tac-toe
    Want to learn Python if you are an absolute beginner? Check out my in-depth beginner series: th-cam.com/play/PLkWv3oO4kHnuKi032yRRYgyQ4kgaNp6gs.html
    ~~~~~~~~~~~~~~~~~~~~~~~~
    For more coding ideas, projects, and tutorials, be sure to subscribe! --> th-cam.com/users/ycubed
    Follow me on Instagram: @kylieyying
    Follow me on Twitter: @kylieyying

  • @javhaasuhochir8126
    @javhaasuhochir8126 2 ปีที่แล้ว

    thanks pretty girl