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

Поделиться
HTML-код
  • Опубликовано: 17 ноя 2024

Комментарии • 284

  • @nextProgram
    @nextProgram  4 года назад +208

    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 4 года назад +18

      nextProgram I’d love to see the chess AI

    • @patsen29
      @patsen29 4 года назад +11

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

    • @nextProgram
      @nextProgram  4 года назад +8

      Glitch Jomo noted!

    • @iminni3459
      @iminni3459 4 года назад +4

      I demand a video on this!

    • @Channel-dp3wc
      @Channel-dp3wc 4 года назад +1

      I would like to see this

  • @StrangeIndeed
    @StrangeIndeed 4 года назад +476

    smh just hard code every single possible outcome

    • @KingJellyfishII
      @KingJellyfishII 4 года назад +28

      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 4 года назад +7

      XKCD 832

    • @philfrisbie4170
      @philfrisbie4170 4 года назад +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 4 года назад +1

      @@philfrisbie4170 tbh most humans prob know every position

    • @jailee6438
      @jailee6438 4 года назад +1

      Sabre *positions?*

  • @Firigion
    @Firigion 4 года назад +233

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

    • @samuelzlota2306
      @samuelzlota2306 4 года назад +14

      if winner:

    • @Firigion
      @Firigion 4 года назад +1

      @@samuelzlota2306 ye

    • @nextProgram
      @nextProgram  4 года назад +31

      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 4 года назад +9

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

    • @paulstelian97
      @paulstelian97 4 года назад

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

  • @asherhovell
    @asherhovell 4 года назад +113

    Get two, put em against each other.

    • @englishmotherfucker1058
      @englishmotherfucker1058 4 года назад +8

      automatic chess draws

    • @aydenzinter2849
      @aydenzinter2849 4 года назад +10

      @@themblue8236 r/iamverysmart

    • @LazoGT
      @LazoGT 4 года назад +8

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

    • @luminatron
      @luminatron 4 года назад +3

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

    • @LazoGT
      @LazoGT 4 года назад +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.

  • @Creeperman1524
    @Creeperman1524 4 года назад +137

    “The only winning move is not to play”

    • @user-le8ul4nr5t
      @user-le8ul4nr5t 4 года назад +5

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

    • @gus9351
      @gus9351 4 года назад +8

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

    • @tricky778
      @tricky778 4 года назад

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

    • @gus9351
      @gus9351 4 года назад +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 4 года назад

      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

  • @kyler7473
    @kyler7473 4 года назад +82

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

    • @nextProgram
      @nextProgram  4 года назад +5

      Haha true

    • @andarerYT
      @andarerYT 4 года назад +4

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

    • @woxpop8792
      @woxpop8792 4 года назад +3

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

    • @eisendrag
      @eisendrag 3 года назад +5

      There is to much lootboxes

    • @Mez0ry1337
      @Mez0ry1337 Год назад

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

  • @erikaordog8288
    @erikaordog8288 4 года назад +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  4 года назад +59

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

    • @Lance0
      @Lance0 4 года назад +15

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

    • @jordananderson2728
      @jordananderson2728 4 года назад +3

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

    • @Starwort
      @Starwort 4 года назад +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

  • @somdudewillson
    @somdudewillson 4 года назад +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 4 года назад +2

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

    • @somdudewillson
      @somdudewillson 4 года назад +34

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

    • @Cup-of-Joe
      @Cup-of-Joe 4 года назад +1

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

    • @somdudewillson
      @somdudewillson 4 года назад +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  4 года назад +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

  • @beri4138
    @beri4138 4 года назад +58

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

  • @CubeverseTutorials
    @CubeverseTutorials 4 года назад +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  4 года назад +1

      Nice job!

    • @lilecaz9547
      @lilecaz9547 2 года назад

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

  • @terminallychill8029
    @terminallychill8029 4 года назад +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.

  • @tuzcu680
    @tuzcu680 4 года назад +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!

    • @nextProgram
      @nextProgram  4 года назад +1

      Maybe I stole the idea from you ;)

  • @MarxyBasement
    @MarxyBasement 4 года назад +4

    RUclips algorithm likes your algorithm

  • @ImAlexander_YT
    @ImAlexander_YT 4 года назад +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....

  • @-_lIl_-
    @-_lIl_- Год назад

    now make that AI go against a clone of itself

  • @erikaordog8288
    @erikaordog8288 4 года назад +5

    Impressive! Can’t wait for the chess video!

  • @anonymous-ds3mc
    @anonymous-ds3mc 4 года назад +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 4 года назад

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

  • @arsam3292
    @arsam3292 4 года назад +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  4 года назад +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 4 года назад +1

      nextProgram It would’ve taken me longer than a month

    • @Spacebar
      @Spacebar 4 года назад +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 4 года назад +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 4 года назад

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

  • @filipe_paixao
    @filipe_paixao 4 года назад +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

  • @SrikarMaddula
    @SrikarMaddula 4 года назад

    Now computer plays against computer, always draws

  • @Jack-rn3rm
    @Jack-rn3rm 4 года назад +13

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

    • @nextProgram
      @nextProgram  4 года назад +3

      That would be interesting haha

    • @That_Guy977
      @That_Guy977 4 года назад

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

    • @gagidoo4757
      @gagidoo4757 4 года назад

      Tic tac toe toc

    • @Jack-rn3rm
      @Jack-rn3rm 4 года назад

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

    • @That_Guy977
      @That_Guy977 4 года назад

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

  • @ДимитърТасев-р9п
    @ДимитърТасев-р9п 4 года назад +1

    Make it play against itself

  • @anonymous-ds3mc
    @anonymous-ds3mc 4 года назад +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.

  • @Philyshark7
    @Philyshark7 4 года назад +4

    Can't wait for the chess one!!!

    • @nextProgram
      @nextProgram  4 года назад +2

      Philip Elbert coming soon!

  • @TheFantasticWarrior
    @TheFantasticWarrior 4 года назад +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

  • @puppergump4117
    @puppergump4117 3 года назад

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

  • @stevencvisuals
    @stevencvisuals 4 года назад +8

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

  • @monochromeart7311
    @monochromeart7311 4 года назад

    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

  • @ttshs
    @ttshs 4 года назад +1

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

    • @Aaronmajowskavitz
      @Aaronmajowskavitz 4 года назад +1

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

  • @Mastervitro
    @Mastervitro 4 года назад +1

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

    • @ferencgazdag1406
      @ferencgazdag1406 4 года назад

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

    • @beri4138
      @beri4138 4 года назад +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 4 года назад

      @@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 4 года назад

      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 4 года назад

      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.

  • @anidea8012
    @anidea8012 4 года назад +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 4 года назад +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

  • @yit6
    @yit6 4 года назад +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 4 года назад +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 4 года назад +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

  • @matejnovosad9152
    @matejnovosad9152 4 года назад

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

  • @gregistopal
    @gregistopal 4 года назад

    Now have the AI play the AI

  • @MACHINEBUILDER
    @MACHINEBUILDER 4 года назад +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 4 года назад +1

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

    • @MACHINEBUILDER
      @MACHINEBUILDER 4 года назад

      @@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)

  • @corniferjr3300
    @corniferjr3300 3 года назад

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

  • @Hyrtsi
    @Hyrtsi 4 года назад

    Looking forward to your chess AI :)

  • @MSFTSTRIO
    @MSFTSTRIO 4 года назад

    You can generate each endstate using itertools

  • @Knights_of_the_Nine
    @Knights_of_the_Nine 2 года назад +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.

  • @legendaryspud3462
    @legendaryspud3462 4 года назад

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

    • @oyungogdfrust4136
      @oyungogdfrust4136 4 года назад

      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.

  • @JakeFace0
    @JakeFace0 4 года назад +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 4 года назад

      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 4 года назад +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

  • @VerrouSuo
    @VerrouSuo 4 года назад

    shall we play a game?

  • @okboing
    @okboing 4 года назад +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

  • @TextGuy
    @TextGuy 4 года назад +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  4 года назад +6

      Awesome!

    • @beri4138
      @beri4138 4 года назад +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.

  • @teamdoodz
    @teamdoodz 4 года назад +3

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

  • @suryakiran3085
    @suryakiran3085 4 года назад

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

  • @wassim5928
    @wassim5928 3 года назад

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

  • @ThunderboltPath
    @ThunderboltPath 4 года назад

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

  • @snalgae
    @snalgae 4 года назад +1

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

  • @teddieo7647
    @teddieo7647 4 года назад +1

    That’s a little spoopy

  • @BlunderMunchkin
    @BlunderMunchkin 3 года назад

    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  3 года назад

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

  • @mytypeofjordan
    @mytypeofjordan 3 года назад

    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)

  • @mukeshtandale2325
    @mukeshtandale2325 4 года назад

    you could use ANSI Escape codes for a much better representation

  • @pe3akpe3et99
    @pe3akpe3et99 4 года назад

    now make 3d tic tac toe AI

  • @allennelson1987
    @allennelson1987 4 года назад

    Nimbers?

  • @bensmart2829
    @bensmart2829 4 года назад

    are we gonna talk about
    if not winner == False

  • @jumhig
    @jumhig 4 года назад +3

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

  • @danifalkjensen
    @danifalkjensen 4 года назад +1

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

    • @matthewweitzner8956
      @matthewweitzner8956 4 года назад

      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.

  • @dikshantraj6005
    @dikshantraj6005 4 года назад

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

  • @widecat1404
    @widecat1404 4 года назад +2

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

  • @mateuszabramek7015
    @mateuszabramek7015 4 года назад

    At 3x3 everyone can only draw.

  • @AaronWGaming
    @AaronWGaming 4 года назад

    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

  • @archerestarcher
    @archerestarcher 4 года назад +1

    You could've done
    if winner != False:

    • @archerestarcher
      @archerestarcher 4 года назад

      actually yeah that could've worked too since winner is already a boolean in and of itself.

  • @greengriffon7179
    @greengriffon7179 4 года назад +3

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

  • @pyrodynamic4144
    @pyrodynamic4144 4 года назад +2

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

  • @arnoldsianturi2181
    @arnoldsianturi2181 4 года назад +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 Год назад

      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 Год назад

      @@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 Год назад

      @@arnoldsianturi2181 good for you👍

  • @JustinSletcherMusic
    @JustinSletcherMusic 4 года назад

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

    • @nythepegasus
      @nythepegasus 4 года назад

      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 4 года назад

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

  • @garyordog2861
    @garyordog2861 4 года назад +1

    Dr O. Incredible!! You did it. They say if you spend 25,000 hours on something that makes you an expert. So in the last 40 years, it is much more than that, like 16 hours a day! So, me, bring a computer expert, you still blow me away!!

  • @matschbirne5363
    @matschbirne5363 3 года назад

    1:25 i just used a 1 dimensional array when i did it.

  • @perfectionbox
    @perfectionbox 4 года назад

    if u put an X at 6 you would've won

  • @lau4996
    @lau4996 4 года назад +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")

  • @Neto_69
    @Neto_69 4 года назад +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 4 года назад

      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 3 года назад +1

      Well, just recalculate the branches

  • @ericj4094
    @ericj4094 4 года назад

    he uses sublime text omg liked and subbed

  • @teddieo7647
    @teddieo7647 4 года назад +8

    How long did this take?

    • @nextProgram
      @nextProgram  4 года назад +10

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

    • @teddieo7647
      @teddieo7647 4 года назад +2

      Wow nice

  • @tricky778
    @tricky778 4 года назад +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.

  • @ccuuttww
    @ccuuttww 3 года назад

    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

  • @thyden2k
    @thyden2k 4 года назад

    finally, a worthy opponent

  • @ToriKo_
    @ToriKo_ 4 года назад

    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

  • @petfama4211
    @petfama4211 4 года назад +2

    Why did your AI, even after searching all those games, still play a first move that is guaranteed to result in a draw? Would make sense for it to at least play something that can result in a win

    • @DevWithMartinus
      @DevWithMartinus 4 года назад +1

      Probably because the first moves that can result in a win requires move from the opponent that it will never do (assuming the opponent will always choose the best move) and so it could lead the AI to loose the game.

    • @petfama4211
      @petfama4211 4 года назад

      Dev with Martinus ah that makes sense... seeing as the game of tic tac toe can only be won through exploiting your opponents mistakes, and the database it searches through assumes no such mistakes; doing anything but moves that guarantee the best score (that being a neutral 0 seeing as positive scores aren’t even in the database), even if it’s technically still impossible for a perfect AI to ever lose and may even give it the option to win, would still only take into account it’s own mistakes and not the opponents, and therefore yield an average score of less than 0?

    • @DevWithMartinus
      @DevWithMartinus 4 года назад

      ​@@petfama4211 Sorry but I don't really understand what you mean. English is not my first language.
      Just keep in mind that minimax is a very basic algorithm, it can really "take into account its own mistake" or search through a database. There isn't a database actually, each turn the algorithm compute the possible states after X turn and choose the one that advantage it the most and disadvantage the opponent.
      This video explain it in details : ruclips.net/video/l-hh51ncgDI/видео.html

    • @petfama4211
      @petfama4211 4 года назад

      Dev with Martinus what i meant with database was simply the 200,000 or so games it searched through in order to set up the minimax algorithm to begin with, so really it’s just the algorithm. The main problem i think isn’t actually that it disregards the opponents mistakes; meaning you’d always wanna settle for a guaranteed score of 0 (a tie) as it’s the highest score possible (positive scores being impossible); but actually that the algorithm on an unequal basis factors in your own mistakes by leaving branches of the algorithm unoptimized in a way before deciding (don’t know how to explain it). If it treated you equally and ignored your possible mistakes as well (which makes sense for an AI) then other good moves would not yield an average of below zero, but actually 0 as well, and the AI could thus choose good moves at random, some of which would not 100% guarantee a draw.

    • @DevWithMartinus
      @DevWithMartinus 4 года назад

      @@petfama4211 Yup that's the biggest cons of minimax is that it assumes that the opponent will choose the most rationale choice each time.

  • @crimeenthusiast7776
    @crimeenthusiast7776 4 года назад

    Just put the google tic tac toe bot on impossible

  • @developerx962
    @developerx962 3 года назад

    This is very cool, thank you very much

  • @acumen8566
    @acumen8566 3 года назад

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

  • @thevictor180
    @thevictor180 4 года назад

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

  • @kairu_b
    @kairu_b 2 года назад

    This is awesome!

  • @darexleon
    @darexleon 4 года назад

    Great TicTacToe program! Github Code?

  • @30svich
    @30svich 4 года назад

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

  • @matejnovosad9152
    @matejnovosad9152 4 года назад

    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  4 года назад

      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 4 года назад

      @@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.

  • @craiglobo2165
    @craiglobo2165 4 года назад +9

    Wow I just made this in python some time ago😂

  • @DylansLapplandSimping
    @DylansLapplandSimping 4 года назад

    I too, am unbeatable in Tic Tac Toe. 😎

  • @rodrigoqteixeira
    @rodrigoqteixeira 5 месяцев назад

    please do not use "not a is b". Unstead use "a is not b". Same for "not a == b". Unstead use "a != b". Hope it was helpful

  • @saveerjain8168
    @saveerjain8168 4 года назад

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

    • @Schnorzel1337
      @Schnorzel1337 4 года назад

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

  • @Krishan_Verma
    @Krishan_Verma 4 года назад

    what is the initial value of alpha beta?

  • @Mark-wy7oq
    @Mark-wy7oq 4 года назад

    Can you send me the code please?

  • @mudasarahmad4419
    @mudasarahmad4419 4 года назад

    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  4 года назад

      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!

  • @vescena
    @vescena 3 года назад

    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  3 года назад

      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

  • @aniketsahu9316
    @aniketsahu9316 4 года назад

    Can we get the code please?

  • @soundshq2556
    @soundshq2556 2 года назад

    What about chess game

  • @davidmeinname
    @davidmeinname 3 года назад

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

  • @gwentarinokripperinolkjdsf683
    @gwentarinokripperinolkjdsf683 4 года назад

    this is over kill though, you can do it just by searching the current board. wikipedia explains the strategy. then again you probably already knew that

  • @hamzaabbaszaidi8788
    @hamzaabbaszaidi8788 4 года назад

    Which editor did you use?