 Should have done this video like a week after school got out Yeah, so I made a so I made a an algorithm for so I made an AI for my game that Made about a year ago, I think I made the video here cuz it came I made it came up with it when I was in eighth grade I don't know what it is about my computer, but it does not like to play nice with the With left click on relive other other laptops have a laptop that works perfectly fine With this like the exact same program and it has no problem with it, but I don't know. I don't know what it is this with my PC Just doesn't like it But anyways, you know one day wreck in a rec recommended file and recommended video I got A video about the min max algorithm and I was like hey, I should implement that for my game I always wanted to have a NA I play against it and I'm pretty sure I implemented it wrong like I Implanted a lot aren't wrong because it makes because it makes blatantly incorrect moves Sometimes and It's just not good some I a friend played against it and they couldn't win so So I guess it's good enough, but since the min max algorithm is Deterministic you can always find situations where you will always win. So Yeah Let me go back. So What the min max algorithm Basically it's it's a way for a computer to choose a turn like a move and a two-player game That's like tic-tac-toe or chess Basically, it has to be Turn-based there's only two players and each player has perfect information. So like there's no chance or anything like that and the way and the way the computer plays is No, the way we play the way the way humans to big we choose a move is we try to get to a point That's more advantageous to us until we we win so So that's so the and we try to anticipate what the other player is gonna do when we when we consider What the what the game? when when we consider how advantageous our Our position is gonna be I'm rambling way too much, but what the computer does is it creates a tree of Every single of every single game state So think of every edge as the move it takes to get to a game state and every node as a game state I should have said at the other way around, but we get the idea So yeah, let's say we have player a And player B and player a wants to get the highest score and player B wants to get the lowest score So they have their own scoreboard but they But when we're using the mini max algorithm We consider player B score to be negative because you know, we want to minimize the score. That's where it gets his name It's right. It wants to minimize and it wants to maximize min max All right so so we play the game and This is the finals. These are the final scores. So the computer does is it simulates every single note So we go down we go down here All right, we just we don't have a value still so we want to give this a value and Once you reach once we reach the end we give it a we it compares we have this alright This is negative three. Let's go back up and check Go down here. All right, this is two. Let's compare the total amount and This is the minimizing player. So we give this a value of negative three We do the same we go back up go down Check these two. This is the value of one Do it again. This is negative one and now we and now as the maximizing player It compares all these sees that this is the greatest one. So we will go down here Now notice how despite 12 being the greatest Score you could have had we didn't we didn't go to the move that goes to 12 because we knew the other player was gonna choose negative one So so that's the I think that's the beauty of this algorithm. Just that uh That it stops bad. Whoops Yeah, I that's the whole point, but yeah, I Think I think it's really cool. Now Most most games are gonna are aren't three turns or They're they're they're way long even tic-tac-toe So this so this thing gets exponentially bigger. So we need to we need to have a way to Make it so that it doesn't have to do as many calculations. So What we so typically the most the most common and most efficient not efficient, but You know, it says the most resources is having a depth So imagine this was actually Way longer than it than it really was. Oh, no, it has like Keeps going um We we just set a depth of three now my My game it has a depth. It's written in C It's uh, it's inefficient In a lot of in a lot of places like in the undue turn part In the undue turn it just has a list Of all the previous turns it goes through it except for the last turn. So That's something that could be fixed But uh, I'm not gonna so right now as five And it did take like two seconds to find the move. Let's see what happens if we set it to seven All right, what's from the game? Good computer. All right And we're gonna be stuck here for a while if chess if if If chess didn't have a cap it would it would take a I don't know to the heat death of the universe to make for the computer to make its first move so It takes way longer if it's seven Let's hit it back to five again. I don't like my stuff being mistake And I showed I showed earlier Show this here, but it does it relatively quickly Again, I could optimize I could optimize undue turn Uh, but I'm not gonna all right So the problem the problem with setting a depth. Oh, yeah, and the way you set a depth is Once you since uh minimax the recursive algorithm Once you when you run it again, you want to you want to give it a depth minus one And then when it reaches a depth of zero you evaluate it get a position evaluated or state evaluators Uh, just something that gives it a score. Uh, that's not based on the end, but just on how the How the board looks like basically All right, so we set our cap But what if we want to what what if we want to optimize more than that because again Most games are not gonna they're not gonna have just these two. They're gonna have like Or three they're gonna have like, uh, I think tic-tac-toe. It's gonna be like nine eight seven After every single one. So another way to Optimize the algorithm and this time it doesn't sacrifice how smart the ai is Is something called alpha beta pruning? So let's undo let's undo all this Process and I'm gonna go through the layman's terms and And just Just run the algorithm, but this time just go through the algorithm, but this time it's just alpha beta pruning So we do the same thing again. We go down here All right, we look at the we look at the value here. All right negative three We go back up. Let's compare it to this other one two One more turns value is negative three go back up to the same thing One okay It does it goes to the next one. It's five, you know compared to this is one All right, let's go back up. Let's go down here negative one now negative one Is less than one This is the minimizing player. So we know that at most this is going to be negative one So we could we can even have something here like eight We know that at most it's going to be negative negative one. So we don't even bother checking anything else We're just going to say that this is negative one And that's that and we we're not going to bother. We're not going to bother with this branch at all All right, so now that we've pruned it We checked with these two That's and that's pretty much it we're just we're just finding We're just finding a way to not check anything Anything else And this can save a lot of resources because again, this is an exponential unexponentially growing tree So it keeps track. So the way it does this is it keeps track of the Alpha and beta so When we first initialize it by the way We have to we have to do alpha is Uh You know alpha and beta we got we got to do them as negative infinity and positive infinity Obviously, you don't have infinity and I don't have infinity and I'll see Unless I'm using like a library. So I just did an arbitrarily large number And it has to be greater than the winner tie state by the way winning winning Winning and losing and time. So when you win you have you have to have a that's the largest Uh or the smallest depending on which player one when you tie that's always gonna have to be zero All right, but there but everything every single evaluation has to be less than Than whatever the uh computer said it was or I mean the than the original alpha and binda So Wait a second Hmm That's weird. Let me let me pass something Oh, I know Alpha the maximizing one has to be negative infinity and beta the minimizing one has to be positive infinity Because you gotta because whatever you get it has to change We got to change it so, uh Yeah, all right, so we're keeping so we're keeping track of this so Once we run once we run min max and once we get to the bottom So I'm just gonna draw my tree right here. We get here to the bottom. All right We set alpha to this let's say alpha is three All right, go back up Yeah, so alpha alpha is three And if we see you right here, this is That is that if it's greater than beta so beta We're checking for the smallest number so So we go around here. We see that beta is negative one We quit we quit execution and we break out of this for loop And we go back up This is now three Again, this is a recursive algorithm. So it does this for every single note in the tree You keep going We have some prunes prunes um I don't really know what else to say maybe Because because it's been a long time. I've forgotten exactly how it is, but that's the but that's basically the gist Uh, let me see if I can remember but let's keep the alpha and beta values over here So right now We're looking for beta beta is negative three right here and So that's alpha. What is it? Oh, yeah, I have to grab it on Yeah, it won't be a problem. All right. So right now alpha is negative three. Let's go down here one So the new alpha is now one Go back up go down We see that beta is negative one now Alpha is greater than beta. So we don't continue execution Uh, basically just repeating myself, but Uh, yeah And it gives you the exact same Things that as if you as if you hadn't done alpha beta pruning now Mainly this video is just to show off code um Was just it was just to show off what what the code would look like again. This is poorly written It's been a long time since I looked at this. It's hard to read Even for me But uh, and I'm the one who wrote it But like all the all the videos I've seen about it didn't really have code Uh, it was just some guy with a white board like like me with the the pencil and stuff, but uh Yeah, just just copy this code if you want It's gpl. So don't forget to make it open source Uh, that's it