 Hey, what's up YouTube? This is John Hammond coming back after another video for the Tic-Tac-Toe series and C++ programming So I haven't touched this in a little bit of time I wanted to wipe the dust off because I did intend on creating this next video, but honestly just flip through the cracks in time So so far we've got a working game of Tic-Tac-Toe where we can let the player play and figure out if the player has won But we have not yet created the computer or the enemy like AI that we're working with So in this video, I want to create a pretty stupid AI that will just at random pick a spot in the grid And then later on we can make a much more smart AI or more intelligent computer enemy or player to Okay, look ahead to see what the player has played and then go ahead and block moves or do something more Advantages to let that computer player win But for now, let's just do the simple basic one of a random Computer player turn so let's create a new function for that. I'm gonna call this computer player turn It's not gonna return anything so the return time will be void I'm still gonna be inside this game class and that should just be fine for us We can keep that in our main loop when we're working with it, but we do want to have a computer player turn so if we're doing this at random we're gonna want to use the Crand function and Let's go ahead and get that because that's in a specific module or a library We need to include and that should be from what I know the standard live a dot H I guess we can print out What that will return for us to make sure that actually happens Let's go ahead and get a computer choice Variable and that will be the number on the grid that they're actually going to Choose as their turn so that's going to have to be in between Numbers one and nine right because we have one through nine available places on the grid to to choose from so if we're gonna use This random function it'll return a completely random number, but we want to cap it at nine and make sure the start is at One so the way we can do this is by using the modulus operator and Modding it to nine so that way no matter what it seems to be it'll be wrapped around over and over and over again until it's only nine or less if Did if there is a remainder of nine? Or it's divisible by nine How the modulus operator works that'll become a zero so we'll actually get numbers zero through eight zero one two three five six seven eight But we want one through nine so we'll just add on a one at the very end here So now we'll have numbers one through nine. That's how we can get our max and min set up just like that And let's go ahead and actually just just play that out on the screen It's just make sure where our random function works more getting it from the right library because I want to double-check on that so Peter Chose Percent D Has their turn Whatever and then let's just put the computer choice in there make sure this actually executes in Computer player turn don't need any arguments there, but let's go ahead and make this make clean to remove that Make to compile and run and now we can main computer player chooses to is return. Okay, so that works just fine Let's play something whatever so the computer is choosing all these places, but we're not actually putting something there so let's go ahead and make sure they actually do update the grid it with their choice and And We're doing this with a random function, right and we don't know for sure whether or not we've actually Chosen a spot that has already been used before either the computer's turn or the player's turn So we actually have to keep trying to figure out a place to play our move And determining whether or not we actually can place our move there So we'll do that inside of a while loop We'll just use a while true because we'll keep looping over and over and over again to keep trying to figure out a new Random place to play our move if another spot is taken So since we have this computer choice we can use that trick We've been doing to get the row and column in the game or on the grid That was computer choice minus one because that's zero based divisible by three for the row and column minus one a mod three for the column perfect Okay, so now we can use that to get what the actual Character is at that grid position with that row and column determining that in that Character matrix we've been using in the two-dimensional array of just grid and now we can determine if this grid position Variable it's a character is going to be there an x or an o and if it is then we know Okay, we can't actually move into this spot. So if group position is equal to an x or To pipe symbols there or group position is equal to an o We will continue. We'll keep iterating. We'll try another location because we can't sit there. We can't place our move Great position is already taken. So let's try another Random number and if it's not then we can go ahead and place so with that means we can say grid row and column We'll be set to the enemy Character so in this case the o because the computer player is playing his o's and then we'll break out of our loop and then we can tell Let's print out to the screen computer will play at Percentee new line and then let's just put the computer choice right there. Cool. Okay That should be everything we need for that function, but let's clean up this generate grid and our kind of main loop here now because Let's show grid and check for wins just very very beginning Which I guess we don't really need to do but since we have two players playing now We want to ask for our turn and then show the grid and check for wins and Then after the player a computer enemy to the enemy player Has made their move we'll show the grid and then we'll check if they won so Kind of a little duplicate stuff here, but after each player has their turn You want to see how it changed the game and whether or not they won So there's nothing wrong with us calling those functions again Let's see how this works make clean make. Nope. Hopefully there are no errors. Looks like it compiles Just fine. Let's run main. Where would you like to play? I'll play at five. Cool the computer will play it to and So I've placed my move. He's placed his at two. Where would you like to play? Let's move it to four Computer will play at eight. So he's down there now and I'll move into six and they're right that way. I've won. Okay. Maybe we don't need to show the grid after I Have played or because that way since the computer player will move after We don't want to have to duplicate that portrayal of the grid. I guess we don't have to do that Let's move it at five here So we'll play it too. Okay Well, you know what I noticed just now in our grid display. We're not actually giving that We're not actually printing out the first Loop here like the side wall. I totally didn't say that Sorry, that probably didn't make much sense but Okay, so now we'll just move over our Top half because I didn't even realize That I wasn't displaying that side. I was just trusting the Okay, cool. Now we have a oh, I guess I don't need a space following that I'm just trying to protect to perfect the display that we have here Okay, cool. So now we actually have a grid and the the leftmost wall there The leftmost side is actually displayed. Sure. Whatever. There's our tic-tac-toe grid and now the game works just fine The computer will play it too. I Can't play there so I'll play it three Player computer player played at eight so I'm gonna take five because I don't want him to win He moved to six. Let's go to nine and then we win perfect It's probably unlikely That the computer player will win at this point Because he's playing at random. So that makes the game pretty easy but it maybe that's at least a Simulated tic-tac-toe game where there's an enemy player or something someone else that's making turns So in the next video, we'll get into a smarter AI Probably looking at the same logic that we're using to check for whether or not The player has won because we can use that that loop To determine where another smart move might be for the computer player. So sweet. Thank you guys for watching I hope you enjoyed this wasn't a whole lot of code in this video Just that simple function to have the computer player turn and I didn't realize we had Didn't have the left side of the grid here. So we corrected that but thanks for watching Hope you're enjoying these if you are. Hey, please like the video Maybe leave me a comment to put you think what else I can do better with Subscribe if you're willing and if you really really I'm super grateful for that. Thanks again guys. See you later