Hello, in this video we're gonna cover implementing the AI system into our Tic Tac Toe game. Because if we launch our game, what we have our splash screen or a main menu, the main menu takes you to the game screen. And on the game screen, we have a grid system, which we can place pieces on, we've got an automatic sign into Game Center, and you'll have the equivalent for Google Play services. If you've implemented that play grid, I can start placing pieces cool animations, and there is a wind condition and I can no longer place the pieces. But the moment we don't want to play against each other, we want AI in there. That's what we're going to be implementing in this video.
The first of all, go to your definitions file, and we need to add a few extra definitions. So the first two, we need to add right here and it's gonna be defined for the play a piece so play out underscore PCE underscore x, I mean, sorry, the ply piece, and this is going to be x underscore piece. You can change your round if you want it to be a circle. And we need one for the AI piece. And this is going to be circle underscore peace. P, like so.
And there's two more states that we need to add. We need a state for when the AI is placed in a piece, you might be thinking, Oh, the AI realistically should be instantaneous. One there may be a delay and to because of animation as well, he won't be straightaway there will be a bit of a delay, hence why we need a state to prevent you or or prevent something else occurring while the AI is actually playing. Then we need a state for draw cars If you draw in the game at the moment, nothing happens. There's nothing to handle that situation. So once you've got these state and pieces sorted, what we're going to do is create a couple of new files.
So far right click, go to the find out, I always like just carrying an existing file, duplicating it and renaming it accordingly. This is gonna be called AI dot h. As you can tell, this is gonna be auto artificial intelligence files and scroll back, we just need to include it into our project. Obviously, you include them, however you need to depending on what Id you're using. Don't need to copy a finish and we got AI and what I'm going to do is literally just get rid of all of the content. And we'll be writing all of this out from scratch. So for the header file, we are going to put a hash pragma Once they're going to do include costs low vos co costs to the dot h gonna include the c++ vector.
Class, we won't be using vectors. And we're going to do hash can include array as well. So the class of you can get what's commonly called a I. In the public with me two methods. First one is going to be the constructor. And this is going to be an interplay piece.
So this will be paste. Note that when initialized the play, a piece will be passed in. So the AO knows essentially what the player base is and then essentially what this piece is going to do void Place P. So this is the only public method that allows the AI to place a piece. So first of all, we need to pass in some parameters, we need to pass in the actual grid array. So we're gonna put this as an end. We are passing it in, in such a way that allows us to modify the original values.
Remember, it's a free by free array. We're also going to pass in a code costs to the kernel code on sprite and it's gonna be grid pieces free by free and we need an int game state law. So the IRS knows exactly what state the game is in private. And there are two methods here. First one is void check section. So this is going to allow us to check a particular section and handle it accordingly.
So, for this we need to put int x one, int y one and x two and y two them in x and in y. We need an into check piece to check. Obviously, what people check in and now we need to pass in our grid as well just exactly how we've done up here so we can copy and paste this Once you've done that, we need to pass in the grid pieces sprite array as well. Because obviously we're placed in a pace, we need to be able to update the grid accordingly. Let me just an empty nine second easy scroll up and down. I'm gonna put a void and it's gonna check if a piece is empty.
Obviously, we don't want to place a piece if it's not empty. This is gonna be int x, int, y. Did you set up a case x and int y. And we are passing in the grid array. Same way we did before our pattern in the grid pieces array as well. So we can copy and paste this from here, like so we're good and now we need a few Variables need a piece.
And play a piece which we're passing in, will calculate the AI piece based on the player piece. And we need a vector. That's going to be a vector array. And the array there's going to be an int, six on terms. And this is a mean, check, match vector. Some of this may sound a bit confusing at the moment, don't worry as we code it, very, very soon, we'll explain what each one of these functions variables are all used for.
Put a semicolon there. We go to our AI dot cpp file. We can now start coding this bad boy. So all you want to do is do hash include H hash include definitions dot h when you do a hash include iostream. When they use the namespace std pros make it easier you could do that with in the header file as well. Gonna have a constructor so a colon colon a Oh, remember, this takes in a player piece which is an integer.
So int play piece okay and now we need to do this player info the player piece that we passed in What we're going to do is check a circle on the school piece equals play piece. If it does, then a piece equals x on the score piece. And otherwise, is these are puppies. Puppies, very simple code at the moment. And now what we're going to do is called the check match vector. So we're going to do check, matches vector dot pushback.
And you know what, what I'm actually going to do is open up my web browser, and go to GitHub. And the reason I'm going to go to GitHub is Just to copy and paste the check match vector code, because quite a few lines are very, very similar, and we'll explain what each one of those does. So if we go here, so I just want all of this. So we're just doing a bunch of checks when we are constructing our player. And we'll be implementing this method very, very soon. So we'll actually explain when we implement the method.
So for now, let's just move on to place piece. So what we're going to do is do void AI plays piece. So void, probably therefore just copy and paste it from here. Copy and paste that If I do a URL colon colon, what we're going to do is do a try. Catch era to be on this upper line. What we're going to do in here, we're going to first check if AI can actually win, because if it can then that is the piece that we should be placing.
By definition that is obviously the goal it is winning for the ion. So for interCall equals zero while i is less than check, match vector dots So what we've actually done here is passed on the order of all of our checks. So we would be checking, essentially, each individual different combination that the AI can take. And this is based on 13 Tic Tac Toe rules. So instead of just placing a p for the AI in the first place there, there is an empty space or in any empty space, they're all you know, certain rules, certain combination, which are best. And as a result, we'll be checking these.
First of all, we obviously want to check if we can actually win. If we can, then we would obviously use whichever particular combination works for that. If not, then we'll be using the first combination that we can actually play a piece within. So we need to size for we're going to iterate over it. Now we're going to do i plus, plus Now we're gonna do check section for the check section is going to be, check match vector, check match vector I, zero. We need to copy and paste this.
Change this to one changes to it to change changes to a free 45101234 and five now we need to specify the player peace we're going to be specifying the grid Right. Now also the grid pieces. So like so this check section, we obviously haven't implemented it yet, but we will be very, very soon at my bad. We're not putting in the player peaceful path in the AI piece because obviously that's the piece we want to place. So now we need to do something similar to check if the player can win. So if we could actually just copy and paste this and check if player can win because obviously the highest priorities even if the player can win, but if the AI can win by placing one piece, then you will place a piece there.
If the player can win, then you need to block the player. So that is the sort of the second precedents in the game. logic. Now, if none of these conditions are met, so if we call win on the player call win, then we can just place a piece in another space but there's still some logic here. The first thing we would want to do is check if piece is well empty. So we want to check if the centerpiece in empty so check if piece is empty for the piece that would check in is one one which is the centerpiece need to pass in the grid array.
We need to pass in the grid pieces sprite array. Now if the center is not empty, what we need to do is check if the corners are empty and have you well aware there are four corners. corners are too So the corners are 022200 and two zero and if one of the corners on empty then basically check for any other space. So if I were to copy and paste this check for any of space. So this is gonna be a 1201. If you want to change the order of these checks WC whichever first one is met, whether it's the center corner or any one of these, you will place the piece in there so you can easily change your game logic very very easily.
And now what we need to do is change the game state for doing Asterix game state equals date on the score plain. So, the user can play again because we have well placed our piece and now what we need to do is check section. So we will go here actually copy out my bad that should be STD not str, copy and paste these two methods you need to have a colon colon a colon colon. So for check in a particular section, we need to check if two pieces match. So we need to do if Asterix grid array x one y one One is equal to the piece to check. And then we essentially want to do the same condition before y to x to copy and paste this.
So changes to boy to boy, I mean x two and now in here they seem like we're missing some sort of Rocky Toby. No, don't look right Eva. Ah, I see what I've done here. This should not this should be a closing bracket here. Should be a closing bracket here. Still good.
My brackets mixed up to be one closed. One closer one there. So, now should not have any arrows in here we need to check if the there is an empty piece where we are trying to place our PD so people grid array for this is x so this is essentially the matching one, this is the one we would want to complete the set with whether that if the players one or not one Okay, did I not put not uppercase H, upper lowercase h for this one sided lowercase X on y. And now, we need to obviously place the PCR so I'm literally going to copy and paste this messed up the brackets. Make sure you want to copy and paste this here. And this will be equal to the AI piece.
We're gonna put grid pieces x, y, and we need to set the texture and the texture is going to be circle on the school file path we need to set it to be invisible so great pieces x, y set visible equals true Now we need to run an action to fade it in. So we're going to do grid pieces. x, y, run action, on the action we're going to run is Coco costs to the fade in. And this nearly gonna take the parameter of time, which is piece on the score fading times it does the great thing about having definition or some sort of settings file that allows us to keep the animations universal, we're going to do fro negative one. And the reason we're throwing anything that we haven't made an error is just our way of emitting the rest of the code. So once we've actually done check section Okay, so assuming that check section has essentially failed, and they no place to put the piece to either win or block the player, we need to check if a piece is empty and place a piece there.
So what we need to do is, we can actually copy and paste the code, copy and paste this new data. So when you check if mtps is equal to x and y, or put this one, uppercase x and uppercase y, we need to put an AI piece there need to set the text or as well. We need to set it to be visible. We need to run the action and they fit so the code is very similar. I'm going to throw negative two and this just allows us to Handle each different fro differently if we want to, we don't want it. But if you wanted to do something, maybe you wanted to play some sort of separate animation or a separate sound effect, or record something separately on on achievement.
Maybe you can easily do in do that now with in the catch, but that is it for the AI class. Not too difficult, not too difficult to get your head around. So now we've got a few changes to make in the game scene. Let's go to the game scene. First of all, you should do hash include AR H, we need to scroll down and create an AI object, which is going to be a pointer. This is going to be AI.
If we go to our game scene dot cpp. We need to initialize it. And we'll just initialize it after we set peace but instead of setting it to XPS, we're going to update this so this has play a piece Now remember, we did do few changes in the event, I mean in the definition file, so we're just gonna put after this AI equals new icon on colon yo and we are merely passing in the turn. Like so, we are almost done. So if we keep scrolling down the few more changes that we need to make initiating the grid rectangles, not a problem initiating the grid pieces, not a problem checking and placing a piece that looks a okay to me so we can just you know, leave that as it is if we keep scrolling down. So in the check when section after we've checked for us being able to eat so if the player can win, we need to check if Well, the IoT can win.
So if state one doesn't equal game state, that means the player hasn't won. And we're going to do game state equals state underscore AI plank, the AI is now playing, you can do a boy place piece. So the AI is going to try and place a piece we're passing in a grid array reference, we're passing in the grid pieces passing in the reference to the game state. So obviously, we want to be able to modify the original values. And what we need to do is copy and paste all of this because we're going to be checking if I only had one, the only difference is We are actually going to be passing in the AI piece and the player piece as well, because at the moment, when we check for a particular when we just check in for the user, so we need to update this so it does int peace to check, you need to update this in the header file.
So if I go to check free pieces for March, paste that here and we'll update this code when we need to in a moment. So up here, what we need to do is pass in the player piece every single time. So player piece oopsy Daisy, went all the way down. Okay, now For this, we need to pass in the AI underscore piece. So now that we've passed in the AI piece, that's all good for the i o 's turn, we need to now check if it's a draw. So what we're going to do is actually let's remove all of this code, so we're going to be reworking quite a bit of this.
Let's do a variable called empty num. This is going to be equal to nine. And we are going to nuke over our grid array. So for int x equals zero, while x is less than three x plus plus we're going to do the same for Y equals All simple stuff that we've already covered allows us to iterate over our 2d array. So we're going to check if there isn't an MTP in a particular section, the grid array x, y. If that isn't, that means there's already a piece there.
And we need to do empty num, minus minus. So if this is actually zero, after we've looped through it, then the game is a draw. You go the other way, and set this to zero. And if there is an empty piece there, then increment it, whichever way it is fine. I've just chosen this method. I'm going to put if zero equals equals empty num, then game state equals date on the school draw one Just for the purpose of testing, I'm going to see out the game state so we can see what state is occurring after every single move.
And now, if we go to sleep, what does that not like? Okay, this doesn't like a few things. Expected expression. Okay, game state equals state to draw. Don't see what's wrong with that. If I build it back to expression.
So game state here with setting it here, unless let's go to the definition of state draw up. I did not put a particular state number, hence why it gave us an error. So I'm going to put 92 go to game dot cpp and no member name. See out in namespace std I need to include Oh yo stream Okay, so the gank state is now going to be consoled out. And if we scroll down so we need to modify the check free pieces and match method. So instead of doing if turn equals x one, y one and if it equals x two and y two, or we are actually going to do is if it equals the piece to check so we can update this accordingly.
Everything else should be okay. Apart from a couple of things we need to do In this two piece to check, we just keep scrolling down. Obviously, we're just placing the winning pleat pieces. Now, that looks very good. The only other thing we need to do, we're not going to set the game state equal to one, because it will depend on Well, what piece we would check in for player where is the player or the AI going to do? Play a piece equals two piece to check.
If it's the fly piece, then the game state equals state underscore one. Else gamestate equals state underscore news. So now you're actually ready to run this code. Hopefully we don't get any errors. It looks like we don't. And now we should be able to test our AI variable Very soon.
So it's just building it into our simulator is your raw case running now, we can just close out of Game Center. So cancel click Run plays a piece here. Okay, place one in the middle. I'm gonna place one here is blocked me. So, okay. What should I do?
If I want to place a piece if I if I place a PC, I will block the AI. And then by definition, they'll just go to a corner with I placed a piece here, for example, they could block me, but it will be stupid to block me because they could win. So this would be a good test. And as you can see, it has worked. We can't place anything. And now we have a fully functioning a AI at the moment.
Yes, we have an AI system that actually works, but there's no prompt to the user. Yes, we've got this printing out what particular state the game is in. But we, as the user don't have a way of actually going back to the game and replaying it. We don't want to have to shut down the game every single time. We want to play it down the next video, we're going to be handling Game Over, and we'll be allowing the user to be able to replay their game. So that's it for this video, we covered a lot.
If you have any questions, feel free to post them on my education platform. So no learning curve, okay? Or just communicate however you feel comfortable on the link for the source code will be available. Feel free to check that out. And as usual, thanks for watching and I hope you have a great day.