I made an unbeatable Tic Tac Toe AI (Minimax algorithm)

Sdílet
Vložit
  • čas přidán 24. 07. 2024
  • This video shows how I made my tic tac toe algorithm in Python. I used the minimax algorithm with alpha beta pruning to search through all possible game states and find the best move for the computer to make. The game can still end in a draw, but it should be impossible to win against it.
    Minimax is a recursive algorithm -- it calls itself thousands of times for each turn the computer takes. This is a similar process to how basic chess AI's are created.
    I keep calling it an AI (artificial intelligence) but I think technically it's just an algorithm.
  • Zábava

Komentáře • 282

  • @nextProgram
    @nextProgram  Před 4 lety +204

    Update: the chess program is finished and I think it's working. I might put the video on hold for a bit, though. I'll probably still make it if there's a demand for it, so let me know!

    • @joshmohamed
      @joshmohamed Před 4 lety +17

      nextProgram I’d love to see the chess AI

    • @patsen29
      @patsen29 Před 4 lety +11

      Chess AIs are very interesting, and it's a deep rabbit hole. Definitely like to see your take on it.

    • @nextProgram
      @nextProgram  Před 4 lety +8

      Glitch Jomo noted!

    • @iminni3459
      @iminni3459 Před 4 lety +4

      I demand a video on this!

    • @Channel-dp3wc
      @Channel-dp3wc Před 4 lety +1

      I would like to see this

  • @StrangeIndeed
    @StrangeIndeed Před 4 lety +474

    smh just hard code every single possible outcome

    • @KingJellyfishII
      @KingJellyfishII Před 4 lety +27

      I once did that, it was horrific.
      Well not really I just told it every possibility of two Xs or Os in a row and told it to either win or block.

    • @stickfigure42
      @stickfigure42 Před 4 lety +7

      XKCD 832

    • @philfrisbie4170
      @philfrisbie4170 Před 4 lety +13

      I did just that 40 years ago using BASIC, I was in high school. It is really easy once you realize that the 3x3 square board only has three starting locations: center, corner, side, and the corners and sides are just rotations 😎

    • @thesabre8458
      @thesabre8458 Před 4 lety +1

      @@philfrisbie4170 tbh most humans prob know every position

    • @jailee6438
      @jailee6438 Před 4 lety +1

      Sabre *positions?*

  • @Firigion
    @Firigion Před 4 lety +228

    "if not winner == False:" is some strong coding XD

    • @samuelzlota2306
      @samuelzlota2306 Před 4 lety +13

      if winner:

    • @Firigion
      @Firigion Před 4 lety

      @@samuelzlota2306 ye

    • @nextProgram
      @nextProgram  Před 4 lety +29

      Yeah I did that because I designed the function that produces the value for winner kind of weirdly. You'd need to see the code but that line made it slightly more readable in my opinion, haha

    • @Firigion
      @Firigion Před 4 lety +9

      @@nextProgram I'm sure it did. Htat's like the only reason to do something like that. It's funny nevertheless.

    • @paulstelian97
      @paulstelian97 Před 4 lety

      @@nextProgram Also if the winner could be 0 that line wouldn't even work as "if winner:".

  • @asherhovell
    @asherhovell Před 4 lety +113

    Get two, put em against each other.

    • @englishmotherfucker1058
      @englishmotherfucker1058 Před 4 lety +8

      automatic chess draws

    • @aydenzinter2849
      @aydenzinter2849 Před 4 lety +10

      @@themblue8236 r/iamverysmart

    • @LazoGT
      @LazoGT Před 4 lety +8

      Ayden Zinter if you think that's a smart thing to say, then im scared for your future

    • @luminatron
      @luminatron Před 4 lety +3

      @@LazoGT bruh, it's meant *sarcastically.* Every entry in that subreddit is pompous arrogant pseudo-intellectuals like that guy above.

    • @LazoGT
      @LazoGT Před 4 lety +2

      Trinitrotoluene Monomania i know, but he's saying that he's trying to sound smart but is actually dumb, when it's not something smart to say at all.

  • @kyler7473
    @kyler7473 Před 4 lety +81

    Tic Tac Toe is such an unbalanced and unfair game, the developers obviously had no idea what they were doing.

    • @nextProgram
      @nextProgram  Před 4 lety +5

      Haha true

    • @andarerYT
      @andarerYT Před 4 lety +4

      How is a game that's always a tie unfair? Or am I missing some new meme?

    • @woxpop8792
      @woxpop8792 Před 4 lety +3

      @@andarerYT In my family we always allowed moving the pieces after the fourth one has been placed.

    • @Opasidity
      @Opasidity Před 3 lety +5

      There is to much lootboxes

    • @Mez0ry1337
      @Mez0ry1337 Před rokem

      well every game is a tie ( almost xD ) if both play perfect

  • @Creeperman1524
    @Creeperman1524 Před 4 lety +137

    “The only winning move is not to play”

    • @user-le8ul4nr5t
      @user-le8ul4nr5t Před 4 lety +5

      too bad we don't see this algorithm play against itself...

    • @gus9351
      @gus9351 Před 4 lety +8

      @@user-le8ul4nr5t tic tac toe is very simple, once you get the hang of it then it's always a draw

    • @tricky778
      @tricky778 Před 4 lety

      in tic tac toe the winning move is to play first, in a corner.

    • @gus9351
      @gus9351 Před 4 lety +2

      @@tricky778 actually no, there is no winning move, you can place right about anywhere and you'll win, if you're second move, the safest move is middle because there's this thing that we call "Absolute Control" in which if I play for example in any corners, and if then the enemy does not play middle then there is at LEAST one square that would make the enemy block that move(hence controlling them) but you have to make sure that square does not connect to their first move because they would also gain absolute control and the game is tied so Absolute control a square that does not connect to the enemie's first move. Now you can win middle but there's no absolute control, it's only rng on where your enemy plays so if the enemy knows everything (which takes about 5 minutes to be learned) then game becomes boring, I only find it fun when in parties and playing and betting with random people, I'm always first move to secure victory and it's a great time always to win 10 bucks because of a tic tac toe game

    • @gus9351
      @gus9351 Před 4 lety

      Also if you're second and your enemy is naive, you can probably absolute control them if they somehow doesn't absolute control in moves 2 and 3

  • @erikaordog8288
    @erikaordog8288 Před 4 lety +215

    Do you know about modular arithmetic? That’s the idea you use for identifying the numbers 1 through 9 with a spot in the grid 👍

    • @nextProgram
      @nextProgram  Před 4 lety +59

      Oh I didn't know that was a thing. Well I know how to use the % operator, so I guess so?

    • @Lance0
      @Lance0 Před 4 lety +15

      S a m e t h i n g
      %=mod

    • @jordananderson2728
      @jordananderson2728 Před 4 lety +3

      @@nextProgram % is modulo
      So 3%2 == 3mod2 = 1

    • @Starwort
      @Starwort Před 4 lety +4

      @@nextProgram you could simplify lines 52 and 53 into:
      row, col = divmod(index - 1, 3)
      Also the operator // is integer division, which you could have used on line 52 to remove the int cast

  • @beri4138
    @beri4138 Před 4 lety +58

    "Searching thorugh 255k positions is ridiculous".
    Stockfish 11 running at 20M nodes per second "amateur".

  • @terminallychill8029
    @terminallychill8029 Před 4 lety +4

    I really like that you take the time to discuss the mistakes that you made. Even though they're sometimes elementary, they're usually informative to people who are starting out. This channel is great.

  • @somdudewillson
    @somdudewillson Před 4 lety +32

    Tic-tac-toe is a solved game tho. There's just a simple 8-step plan that will provide perfect play.

    • @Cup-of-Joe
      @Cup-of-Joe Před 4 lety +2

      I was wondering about this. The person who goes first always wins...

    • @somdudewillson
      @somdudewillson Před 4 lety +34

      @@Cup-of-Joe Well, no. If played optimally, it will always end in a draw.

    • @Cup-of-Joe
      @Cup-of-Joe Před 4 lety +1

      Ah, I wasn’t aware of that haha. I’ll have to look that up.

    • @somdudewillson
      @somdudewillson Před 4 lety +5

      @@Cup-of-Joe en.wikipedia.org/wiki/Tic-tac-toe#Strategy
      I still have a very old command-line player vs AI tic-tac-toe game that implements it on my hard drive.

    • @nextProgram
      @nextProgram  Před 4 lety +18

      This project was more about trying to implement the minimax algorithm haha. If you've seen my chess AI video, I used it for that

  • @CubeverseTutorials
    @CubeverseTutorials Před 4 lety +10

    I saw this video in my recommended and instead of watching it, I attempted to make this myself in java. It took hours upon hours, but I finally made it unbeatable and even gave the whole thing a simple GUI. This is a great video, and it was interesting to see your approach in tackling this!

    • @nextProgram
      @nextProgram  Před 4 lety +1

      Nice job!

    • @lilecaz9547
      @lilecaz9547 Před 2 lety

      Hello it might be a little late but could you send me your program? Thats exactly what im looking for

  • @tuzcu680
    @tuzcu680 Před 4 lety +2

    I made a minimax dots-and-boxes AI a couple of years ago and this video was surreal to watch because i basically had the exact same console interface where I would print the entire grid at each turn and had numbers representing places on the grid. It all came back to me when I saw your function names because I think I also had a "check diagonals". Didn't have time to implement pruning though. Great video!

  • @ImAlexander_YT
    @ImAlexander_YT Před 4 lety +6

    I just have to say, your channel is AMAZING. I found you when I was watching the DEVLOG of KARLSON. I still want to see more on keeper! keeper' up....

  • @erikaordog8288
    @erikaordog8288 Před 4 lety +5

    Impressive! Can’t wait for the chess video!

  • @arsam3292
    @arsam3292 Před 4 lety +113

    An unbeatable chess AI would probably take forever, Cant wait to see how long it takes you, also was your exam in person? cause if it was i feel bad for you considering the virus going on

    • @nextProgram
      @nextProgram  Před 4 lety +32

      Yeah it's been a lot of work so far. I'm posting a Unity devlog next week for a new game I'm making, so it should be ready 2 weeks from now. And no, the exam was online haha

    • @reversev9778
      @reversev9778 Před 4 lety +1

      nextProgram It would’ve taken me longer than a month

    • @Spacebar
      @Spacebar Před 4 lety +6

      An unbeatable chess engine would need to run on a supercomputer for a long time, as this algorithm needs to calculate all the possible moves branching off of each next move to calculate the perfect move.

    • @Fireball248
      @Fireball248 Před 4 lety +2

      @@Spacebar you can get pretty close to unbeatable engines with deep learning, as seen in Deepmind's AlphaZero etc., but obviously it's not a perfect solution. However, it is extremely close and can beat the vast majority, if not all, human and traditional AI players and runs in a fraction of the time of traditional engines, as it considers only a few thousand moves as opposed to millions per turn.

    • @NameisU
      @NameisU Před 4 lety

      Unbeatable chess AI (so far) has already been made. Check ALPHA 0 or Google deepmind.

  • @Hyrtsi
    @Hyrtsi Před 3 lety

    Looking forward to your chess AI :)

  • @TheInvestmentThesis
    @TheInvestmentThesis Před 3 lety

    Very informative and entertaining! Your channel will be big, bro ;)

  • @stevencvisuals
    @stevencvisuals Před 4 lety +8

    can you link the github/ or show us the full source code? That would be very helpful if you do!

  • @TFWPLSSUB
    @TFWPLSSUB Před 4 lety +1

    The reason using “is” didn’t give you an error is because it checks if the thing is same or not, like using == to compare numbers and strings only gives False

  • @JakeFace0
    @JakeFace0 Před 4 lety +1

    Hey just so you know: In python you can replace int(a/b) with a//b. The // operator is an integer divide operator. It returns the floor of a/b and (I think) is more computationally efficient as it uses something similar to repeated subtraction rather than doing a computationally expensive floating point division.

    • @stickfigure42
      @stickfigure42 Před 4 lety

      There's no particularly notable efficiency difference, it's just nice to be able to write it that way. The more important consideration for some people is that if you're working with *really* big numbers, a // b will give you the correct answer, and int(a / b) will give you the wrong answer.

    • @stickfigure42
      @stickfigure42 Před 4 lety +1

      For example:
      >>> 10 ** 1000 // 2
      (5 with 999 zeros)
      >>> int(10 ** 1000 / 2)
      Traceback (most recent call last):
      File "", line 1, in
      OverflowError: integer division result too large for a float

  • @Philyshark7
    @Philyshark7 Před 4 lety +4

    Can't wait for the chess one!!!

  • @HeilmwaterStudios
    @HeilmwaterStudios Před 4 lety +4

    CZcams algorithm likes your algorithm

  • @monochromeart7311
    @monochromeart7311 Před 4 lety

    I guess you're new to Python, welcome!
    A language that did 90% of the heavy lifting for you and I love to use for servers

  • @anonymous-ds3mc
    @anonymous-ds3mc Před 4 lety +3

    I remember as my school project i made a person vs person tic tac toe in c++, never decided to do the ai tho

    • @vibaj16
      @vibaj16 Před 4 lety

      @C Lopez Hard coding that perfect game takes more code (although easier to do) than using minimax

  • @kairu_b
    @kairu_b Před 2 lety

    This is awesome!

  • @anonymous-ds3mc
    @anonymous-ds3mc Před 4 lety +1

    "Early challenges, taking the number of the space wanted and convert to 2d array" I just hard coded 1 to 0,0; 2 to 0,1 etc and made a function that returns it.

  • @dikshantraj6005
    @dikshantraj6005 Před 4 lety

    Hey, there's an amazing course on game ai on kaggle using the game connect, I'm sure you'll love it

  • @Knights_of_the_Nine
    @Knights_of_the_Nine Před 2 lety +1

    I know in C that "=" basically sets a value. So while(x = 0) will always move to the next instruction because it will MAKE x 0 and then because it's false it'll move on instead of loop. While(x == 0) will loop though as long as x is 0.
    I don't know how it works in python yet =p.

  • @TextGuy
    @TextGuy Před 4 lety +17

    hey, i just made a Chess AI and a video about it few days ago, it is my first time and its quite a fun project to do, but its quite complicated for me , because chess has so many possibilities and i am quite stuck in the finishing state ,will love to see yours :)

    • @nextProgram
      @nextProgram  Před 4 lety +6

      Awesome!

    • @beri4138
      @beri4138 Před 4 lety +1

      You just need a search algorithm and an evaluation function.
      Use Alpha Beta search and a simple evaluation that counts the material.
      You can then add various bonuses and penalties to the evaluation.
      For example: doubled pawns bad -0.5, backwards pawns bad -0.5, passed pawns good +0.7, etc.
      Also look into "move ordering" so your program searches the most promising variations first and only then the weird moves.
      You can also add tablebases and an opening book to make your program play stronger.

  • @developerx962
    @developerx962 Před 2 lety

    This is very cool, thank you very much

  • @widecat1404
    @widecat1404 Před 4 lety +2

    Can we get a download link for this? It seems fairly interesting

  • @filipe_paixao
    @filipe_paixao Před 4 lety +2

    tic tac toe + rock paper scissors. = kinda a best game
    Chose your position in tic tac toe, then play rock paper scissors, if you win you maintain your position, if you lose your can't chose that position and play rock paper scissors again after choosing another one, in case no position is left your adversary choses your position for you. In case of ties just play again

  • @Neto_69
    @Neto_69 Před 4 lety +3

    I am new to coding and I have a question. After the alfa-beta pruning and some branches are "cut off", what happens if the player doesnt make an optimal move and chooses a branch that the AI didnt consider?

    • @pokemonrampagemake
      @pokemonrampagemake Před 4 lety

      This has happened before for chess AI, usually they have a database to record the moves, so in the future they’d be able to identify it in the future, also, once the move is played, the AI goes back to check it to figure out its next move.

    • @angel-ig
      @angel-ig Před 3 lety +1

      Well, just recalculate the branches

  • @ccuuttww
    @ccuuttww Před 2 lety

    Hey at 3:30 the minimax function
    is it necessary to do winner = checkGameIsOver(board) in every recursion?
    If we can skip in some recursion we can speed up the caculation
    I have an old I3 CPU PC it takes 3 seconds to finish

  • @matejnovosad9152
    @matejnovosad9152 Před 4 lety

    I made this with Q learning and 2 ais playing against each other. Worked really well.

  • @teamdoodz
    @teamdoodz Před 4 lety +3

    You can make the board look nicer by using ASCII pipe characters instead of | and _.

  • @Jack-rn3rm
    @Jack-rn3rm Před 4 lety +13

    You should definitely try an AI for ultimate tic tac toe ;)

    • @nextProgram
      @nextProgram  Před 4 lety +3

      That would be interesting haha

    • @That_Guy977
      @That_Guy977 Před 4 lety

      Is that the one with a big grid with 9 small ones?

    • @gagidoo4757
      @gagidoo4757 Před 4 lety

      Tic tac toe toc

    • @Jack-rn3rm
      @Jack-rn3rm Před 4 lety

      @@That_Guy977 yeah, big 3x3 grid full of 3x3 grids
      it's good fun

    • @That_Guy977
      @That_Guy977 Před 4 lety

      @@Jack-rn3rm I've played it on coolmathgames as strategic tic tac toe i think, it's great

  • @anidea8012
    @anidea8012 Před 4 lety +2

    Instead of , You can use this , new index = Column_index + row_index * Number of column ; by this way you convert 2d array into single array

    • @mohammedjawahri5726
      @mohammedjawahri5726 Před 4 lety +1

      For anyone reading this in the future it's good to think about what it represents so you'll never have to memorize it
      You are essentially checking how far down the 2d grid you are (how many rows in) and multiplying that by the width. Because each row you go down represents crossing all of the width cells right above you. Hence row*width skips all the rows above you, then you add the current col to represent how far along you are in the current column

  • @-_lIl_-
    @-_lIl_- Před rokem

    now make that AI go against a clone of itself

  • @teddieo7647
    @teddieo7647 Před 4 lety +1

    That’s a little spoopy

  • @arnoldsianturi2181
    @arnoldsianturi2181 Před 3 lety +3

    I wanna ask, I kinda confuse. How can we determine the score (in the leaves of the tree) of TicTacToe? Isn't the score/value should be +1 (for winning) or 0 (for draw) or -1 (for losing)?

    • @abbasuccess3155
      @abbasuccess3155 Před rokem

      Yeah I know this is late, but you could just give a value, like the number of empty squares, multiplied by 1 for winning, 0 for tie and -1 for losing.
      That way the ai can make moves that help it win the fastest, cause if it can win when there are 4 squares left, that move would have a score of 4 as compared to a later winning move when there are 2 squares left with a score of 2.

    • @arnoldsianturi2181
      @arnoldsianturi2181 Před rokem

      @@abbasuccess3155 Lol thanks a lot for this answer, i really appreciate it 😇 but thank god, i've been graduated my study on computer science 😂🤍

    • @abbasuccess3155
      @abbasuccess3155 Před rokem

      @@arnoldsianturi2181 good for you👍

  • @SrikarMaddula
    @SrikarMaddula Před 4 lety

    Now computer plays against computer, always draws

  • @jumhig
    @jumhig Před 4 lety +3

    I did minimax "noughts and crosses" in GWBASIC on a 386 PC in 1993, for a varsity project.

  • @AaronWGaming
    @AaronWGaming Před 4 lety

    Umm it is easy to do since there is only 3 moves players can start with center corner side, starting side gives opponent the chance to win center gives NO CHANCE as it leaves opponent with 2 moves (Corner/ side) one leads to a tie and the other (side) causes X to win always. Corner and side give opponents 5 different moves each

  • @MSFTSTRIO
    @MSFTSTRIO Před 4 lety

    You can generate each endstate using itertools

  • @acumen8566
    @acumen8566 Před 3 lety

    Could you make upload a copy of your tic tak toe code?

  • @mudasarahmad4419
    @mudasarahmad4419 Před 4 lety

    Hi, Can you explain a bit more why you used: return -10 + depth and return 10 - depth
    What is its logic? i need this, will really appreciate your help!!!
    Thank You!

    • @nextProgram
      @nextProgram  Před 4 lety

      Hey! If I remember correctly, that will allow the algorithm to prioritize winning the game in fewer moves. The depth will always be less than 10 since there are only 9 possible slots. Basically, the algorithm won't choose a move that wins in 5 moves if it can win in 3 moves, because the score will be higher for the 3 move case!

  • @user-qr7zm9wo6z
    @user-qr7zm9wo6z Před 4 lety +1

    Make it play against itself

  • @legendaryspud3462
    @legendaryspud3462 Před 4 lety

    Make a tic tac toe ai have a tic tac toe gladiator fight against him self!

    • @oyungogdfrust4136
      @oyungogdfrust4136 Před 4 lety

      Legendary spud it would end with a tie every time since they both always do the best possible outcome to counter the best possible outcome.

  • @lau4996
    @lau4996 Před 4 lety +1

    Hey man, can you explain to me why you made a 2d array instead of a list?
    you could just do
    list = []
    x = 0
    while x < 9:
    list.append(x)
    x += 1
    for arg in list:
    print("""
    {0} | {1} | {2}
    -----|------|-----
    {3} | {4} | {5}
    -----|------|------
    {6} | {7} | {8}""").format(list[0], list[1], list[2], list[3], list[4], list[5], list[6], list[7], list[8])
    if not x == 8:
    IntToChange = input("which booth do you want?: ")
    list.replace(IntToChange, "o")

  • @ttshs
    @ttshs Před 4 lety +1

    I have a strategy that wins in every circumstance except for one, which you tie in.

    • @Aaronmajowskavitz
      @Aaronmajowskavitz Před 4 lety +1

      yes because if both players know each optimal strategy it always ends in a draw

  • @ToriKo_
    @ToriKo_ Před 4 lety

    1:20
    Could you help me understand why you couldn’t do something similar to
    user_input = input(“choose from 1-9 ”)
    position = user_input -1
    Edit: oh I think I just realized it’s because your board is a bit more complicated
    Edit 2:
    Well actually you could keep a 0-8 list and just make a display_board with the board, eg:
    positions = [1, 2, 3 etc]
    display_board = positions(1) + “||” + positions(2) etc

  • @okboing
    @okboing Před 4 lety +3

    I got this stuff built into my brain tho
    Ngl I've only really lost when I was young
    Now I only tie and win

  • @Krishan_Verma
    @Krishan_Verma Před 4 lety

    what is the initial value of alpha beta?

  • @wassim5928
    @wassim5928 Před 2 lety

    I can beat easly because in tic tac toe there are two tricks to wins start from any corner or from the middel

  • @30svich
    @30svich Před 4 lety

    how did it search through 255k positions, if there are only less than 3^9=19k positions?

  • @yit6
    @yit6 Před 4 lety +2

    I made a tic tac toe algorithm one time. It was unbeatable, what it did was win if it could, then it would block, then go in a corner.

    • @angel-ig
      @angel-ig Před 4 lety +1

      Yeah, this "rule-setted" AIs were the ones in the 80s. Now there is minimax as well as machine learning like neural networks ;)

    • @tricky778
      @tricky778 Před 4 lety +1

      You can beat it if you go first, if it goes first it can win but you can force a draw, but the strategy you gave can lose even if it goes first. The best first mover is: corner, opposite corner, block or else 3rd corner, win or else block until a draw

  • @corniferjr3300
    @corniferjr3300 Před 3 lety

    I hope your chem exam went well after that 2 AM programming session

  • @lubu682
    @lubu682 Před 3 lety

    awsome!

  • @saveerjain8168
    @saveerjain8168 Před 4 lety

    Hey I use Java but want to start some python. What software are you using to code?

    • @Schnorzel1337
      @Schnorzel1337 Před 4 lety

      Im not the creator of the video but eclipse with the pyDev Extensions works as you expect.

  • @thyden2k
    @thyden2k Před 4 lety

    finally, a worthy opponent

  • @vigintinek3718
    @vigintinek3718 Před 4 lety

    Hi. I'm making similar alogorithm as yours. But I've got a problem with MemoryError: Stack overflow. Have you get it? If yes how did you solve it?

    • @nextProgram
      @nextProgram  Před 4 lety

      My guess is that that’s an issue with an infinite loop or infinite recursion!

  • @ericj4094
    @ericj4094 Před 4 lety

    he uses sublime text omg liked and subbed

  • @mukeshtandale2325
    @mukeshtandale2325 Před 4 lety

    you could use ANSI Escape codes for a much better representation

  • @hanafifadzillah1621
    @hanafifadzillah1621 Před 4 lety

    But can you make a program where CPU always win against human (Draw isn't included)? Whenever I play tic tac toe, the game usually end in draw.

    • @nextProgram
      @nextProgram  Před 4 lety

      Hanafi Fadzillah Nope, the only thing you can guarantee is that the CPU never loses. The algorithm is unbeatable, but it doesn’t necessarily always win. Like you said, it’ll end in a draw if both players are skilled

  • @Mastervitro
    @Mastervitro Před 4 lety +1

    And here I thought there were only 512 possible games of Tic Tac Toe

    • @ferencgazdag1406
      @ferencgazdag1406 Před 4 lety

      There are 512 possible end states of tic-tac-toe, but there are 3⁹=19683 possible states and even more possible games.

    • @beri4138
      @beri4138 Před 4 lety +1

      @@ferencgazdag1406 How did you get that number? There are 9 possible first moves, 8 possible second moves, 7 possible third moves, etc.
      So the number of positions is obviously 9x8x7x6x5x4x3x2 = 362880.
      Do you even math bro?

    • @ferencgazdag1406
      @ferencgazdag1406 Před 4 lety

      @@beri4138 But each position on the board can be achieved indifferent ways, so the number of possible positions is less. It is also possible, that a sequence of moves can not be carried out, because the game is won halfway thru, so the number of possible moves is also inaccurate. 512 is assuming, that at the end of the game each tile is filled, so it is 2⁹, but it doesn't have to be this way, and there are 3⁹ possible states, but not really, because
      OOO
      XXX
      is not a possible state what can arise during a normal game, or as an end result.
      It is difficult.

    • @ferencgazdag1406
      @ferencgazdag1406 Před 4 lety

      What i have found, is that at the fifth move, where a game can be won, there are 630 board states and 15120 possible games, and got stuck there, gone to sleep.

    • @Schnorzel1337
      @Schnorzel1337 Před 4 lety

      You should consider symmetry that way there are 3 moves at the start: In the mid, a corner or between two corners. For those you can get: Mid 2, Corner 4 , between 5. So implementing a simple rotation and mirroring routine would reduce the number of possible games drasticly.

  • @puppergump4117
    @puppergump4117 Před 2 lety

    I thought the scroll bar was mine and got really mad at youtube for it being in fullscreen.

  • @MACHINEBUILDER
    @MACHINEBUILDER Před 4 lety +3

    I wrote a neural network which played against itself so that it'd learn to never lose a tic tac toe game :)
    (In python - the superior language - of course)

    • @beri4138
      @beri4138 Před 4 lety +1

      Did it take long to train? How many blocks and nodes?

    • @MACHINEBUILDER
      @MACHINEBUILDER Před 4 lety

      @@beri4138 actually not that bad. It's quite simple, 9 input nodes, 2 hidden layers of 9 nodes, and one output layer of 9 nodes. I trained it against a random bot for about an hour (a few thousand games)

  • @danifalkjensen
    @danifalkjensen Před 4 lety +1

    lol my attempt at minmax worked in reverse the bot always tried to lose and never to win

    • @matthewweitzner8956
      @matthewweitzner8956 Před 3 lety

      If the bot is trying to lose, make sure you have the minimizing player vs. maximizing player right, and that minimizing vs. maximizing the score works, and if what you mean is that it just picks the next spot in order, not that it actively moves away from a line of 2 when that is the next spot, then I don't know how to help you, because that's exactly the problem mine has.

  • @davidkedra3001
    @davidkedra3001 Před 3 lety

    Can someone please help me I have an error in my code I cant find @t

  • @darexleon
    @darexleon Před 4 lety

    Great TicTacToe program! Github Code?

  • @juliankaiser1323
    @juliankaiser1323 Před 4 lety

    Is the ai always starting? Or does the player start against the ai?

  • @gregistopal
    @gregistopal Před 4 lety

    Now have the AI play the AI

  • @usamasom59
    @usamasom59 Před 3 lety

    Can i get this code?

  • @snalgae
    @snalgae Před 4 lety +1

    What will happen, if 2 bots will play with each other?

  • @Mark-wy7oq
    @Mark-wy7oq Před 3 lety

    Can you send me the code please?

  • @VerrouSuo
    @VerrouSuo Před 4 lety

    shall we play a game?

  • @aniketsahu9316
    @aniketsahu9316 Před 4 lety

    Can we get the code please?

  • @teddieo7647
    @teddieo7647 Před 4 lety +8

    How long did this take?

    • @nextProgram
      @nextProgram  Před 4 lety +10

      It took a few days to make the program. The video took about 10 hours to make

    • @teddieo7647
      @teddieo7647 Před 4 lety +2

      Wow nice

  • @MudakTheMultiplier
    @MudakTheMultiplier Před 4 lety

    Hypothetically would it be possible to beat a minimax AI with alpha beta pruning by taking paths that it had already pruned?

    • @nextProgram
      @nextProgram  Před 4 lety +1

      Mudak The Multiplier Nope, it only prunes moves that have to be worse than ones it’s already found. It assumes the opponent is playing perfectly, so if the opponent plays worse than perfectly then it still works

    • @MudakTheMultiplier
      @MudakTheMultiplier Před 4 lety

      @@nextProgram presumably this only works with games that can be "solved" then?

    • @nextProgram
      @nextProgram  Před 4 lety

      Mudak The Multiplier Kind of. You can also use it for chess, for example

  • @Rahul-lg1nw
    @Rahul-lg1nw Před 4 lety +1

    Source code?

  • @hamzaabbaszaidi8788
    @hamzaabbaszaidi8788 Před 4 lety

    Which editor did you use?

  • @sadhlife
    @sadhlife Před 4 lety

    if you wanna check out a really clean Python tic tac toe (no AI tho) i just made one a few days ago, it's on github.com/tusharsadhwani/tic.py

  • @tricky778
    @tricky778 Před 4 lety +2

    Did this on a 48K sinclair spectrum when I was 10. Just made an array with every game state in it and pruned every path from a losing state back to the initial state leaving all the winning moves only.

  • @ExamSolutions.e
    @ExamSolutions.e Před rokem

    cool

  • @matejnovosad9152
    @matejnovosad9152 Před 4 lety

    There is way less states I made a tic tac toe ai a month ago and there os maximum of 3^9 states. Which is acctually less because not all are valid.

    • @nextProgram
      @nextProgram  Před 4 lety

      www.jesperjuul.net/ludologist/2003/12/28/255168-ways-of-playing-tic-tac-toe/#:~:text=Here's%20a%20document%20with%20every,player%2C%20and%2046%2C080%20are%20drawn.

    • @matejnovosad9152
      @matejnovosad9152 Před 4 lety

      @@nextProgram ok I commented and then coded this from scratch and yeah there is that many games possible, although not as many *unique* end states.

  • @pe3akpe3et99
    @pe3akpe3et99 Před 4 lety

    now make 3d tic tac toe AI

  • @greengriffon7179
    @greengriffon7179 Před 4 lety +3

    Bruder i have all the tic tac toe strategies memorized its easy

  • @mytypeofjordan
    @mytypeofjordan Před 3 lety

    i did my IB math IA on torus tic tac toe and learned way too much about the game, so i greatly enjoyed this. (spoiler alert: player 1 wins every time without fail in torus tic tac toe)

  • @nnaaaaaa
    @nnaaaaaa Před 4 lety

    why use a 2D array instead of partitioning a 1D array into 3 sections?

    • @danifalkjensen
      @danifalkjensen Před 4 lety

      i don't know his reason
      but printing the board will change and there are likely other reasons

  • @allennelson1987
    @allennelson1987 Před 3 lety

    Nimbers?

  • @JustinSletcherMusic
    @JustinSletcherMusic Před 4 lety

    "is" is usually for type checking, if I'm not mistaken

    • @nythepegasus
      @nythepegasus Před 4 lety

      is is actually for finding if two variables are the exact same. So setting a variable to True, and then checking if variable is True will result in True, because they both reference the same memory location. Setting variable c = [1,2,3] then a = c and then running a is c will result in true, whereas setting a = list(c) will result in false (because you made a new list object in memory, instead of just referencing the first object). Running id() on variables helps to find which two would return True with is.

    • @JustinSletcherMusic
      @JustinSletcherMusic Před 4 lety

      @@nythepegasus Ya, I haven't used Python yet, but been programming in swift and it's obviously a bit different.

  • @DylansLappalterCopium
    @DylansLappalterCopium Před 4 lety

    I too, am unbeatable in Tic Tac Toe. 😎

  • @thevictor180
    @thevictor180 Před 4 lety

    I’m perfect at tic tac toe too, it’s pretty easy

  • @pyrodynamic4144
    @pyrodynamic4144 Před 4 lety +2

    Am I the only one who thinks he sounds like 3blue1brown?

  • @Vescena
    @Vescena Před 3 lety

    The thing I don't get is if they can beat you every time, if you do the same thing the AI does, can't you beat anyone every time too?

    • @nextProgram
      @nextProgram  Před 3 lety

      Not quite. It can’t beat you every time, it just never loses. So the game could end in a tie if both players are playing perfectly

  • @BlunderMunchkin
    @BlunderMunchkin Před 3 lety

    30 years ago a bunch of people at MIT built an unbeatable Tic-Tac-Toe computer. They built it out of Tinker Toys. Your algorithm might be more complicated than it needs to be.

    • @nextProgram
      @nextProgram  Před 3 lety

      This code isn’t really that complicated, it’s just a basic minimax algorithm

  • @craiglobo2165
    @craiglobo2165 Před 4 lety +9

    Wow I just made this in python some time ago😂

  • @bensmart2829
    @bensmart2829 Před 4 lety

    are we gonna talk about
    if not winner == False

  • @suryakiran3085
    @suryakiran3085 Před 4 lety

    But , it is easy to stay unbeatable in tic tac toe ..