 What's up guys at John Hammond here welcome back to the YouTube video still creating a C++ program for the tic-tac-toe game that we were asked about and We just created some functionality. We can really let the player Actually interact with the program. It'll ask for a turn. Where do you want to place your move etc? And we can play tic-tac-toe, but it doesn't detect when we actually win So that's the next functionality We want to go ahead and add in is actually being able to determine when someone has won the game So let's jump in we've got everything kind of wrapped and bundled in this game class in this game object So let's go ahead and create another function here. We'll do this Void because it doesn't need to return anything, but we can just check for wins and Simple thing for us. Let's just if someone does win the game Display it out to the screen and then exit close the game because we're just trying to showcase some of the concepts here I doesn't need to be a fully fleshed out incredible tic-tac-toe game alright let's create a Kind of like a database or a group of move sets or possible combinations on how to win tic-tac-toe so This isn't too crazy, right because there aren't aren't too many ways you can win tic-tac-toe Sure, there are a lot of combinations if you actually consider The order and the procedure and the way that someone like finishes the game and actually wins but at the end of the day the actual setup or like the positioning of a solution is Kind of easy there are there are only eight possible ways you can win right you can go across One two three four five six seven eight nine So there's there's those three and if you want to go down one four seven two five eight three six nine Okay, another three. So we've got six right now and you can have the diagonal positions one five nine and Seven five three so eight ways to win at least eight arrangements Certainly not possible combinations That would be much much more, but there are eight arrangements that would win the game So let's create that as a list and loop through them and see are any of those Arrangements met have we have we found the criteria for someone to be in that setup? So the way that I did this was I actually set up An array here of the strings so I Had to have them be Constants and I don't remember why but I'm sure one of you smart people can tell me probably in the comments Or we'll run into it later. So we knew that there were eight possible ways to win So let's define them just like this. We knew we had one two three. We knew we had four five six Seven eight nine Etc. I Don't think I have to say all of these out loud for you I'm sure you get it Sure, you understand No, let's do our diagonals nine and Seven five three Great, so those are all the arrangements that could constitute someone winning the game of tic-tac-toe So now let's loop through all of these possible winning move sets They threw Whoa, I can't type. Oh my lord So let's do this with a simple for loop. Let's just say for Int I equal zero I is less than eight in this case and I plus plus okay, so let's try and try and keep track as to whether or not there has been a winner or we've actually detected someone has won and Now we want to actually kind of use each of these values here as an Index or position in our grid. So Since we want to be able to check, okay, is there the same character at each of these is there the same Either an X or an O at every single one of these So the way we have to keep track of that is by determining. What was the previous value that we saw? What was the previous? Character at the last position we looked at was it an X was it an O or was it even just a number? So we'll keep track. We'll keep track of that. Sorry as a previous grid or something Whatever you really want to call it I'll just set this to zero right now because there aren't there and there's never going to be a zero in the grid We go from one to nine and we have X's and O's, but that's all Okay, so now we can go ahead and extract the winning move that we're looking at and this might be why I had to use a constant here Because when I try and extract it out of the array of winning moves based off that index You do need to have it set up as just a string that you're able to read since this is a constant when it's Entered into the source code like that so now I've got the winning move and Now that that's selected now that that's kind of Kept track of inside of our loop Now we can loop through all the characters in that winning move one two three four five six Etc etc, so we know that that's always going to be three characters long So we can do another four int index Equals zero index is less than three and index plus plus I'm using index here because I already used I as my iterator in the other loop easy peasy so now we can go ahead and Figure out the character that we're looking at since we're using a numeric iterator here We want to actually use that to select and index what character we're looking at in our winning moves Equals winning move index at that index. Cool. So now we've got that character So let's find the integer form of that so we can actually determine Where is that location in the grid? Remember? We had to do that with the integer So we could determine the proper row and column And then actually extract it out of our grid array So we can do that by Going that magic trick we had tried earlier like entered number equals the character We're looking at Minus the character of zero So it puts it as an integer and now the grid space Or that actual zero based grid coordinate is going to be that number Minus one remember it has to be minus one because that is one based one through nine But we need to be zero based if we want to actually determine the row and column and row grid space by two by three or Really grid size I suppose I should be using grid size for all of these that are using three And the column is the modulus here. Okay, cool So now we can determine what character is at that position in the grid So our grid character our grid based off of the row in the column great so Now we have to test What character we're looking at relative to where what we were looking at previously? So our first iteration happens when previous grid is equal to zero so if the previous grid if That previous character we're looking at is equal to zero We know this is the first iteration so we should just set the previous grid character to what we're looking at just now because There is nothing that could have came before this so this has to be the start right good Otherwise we need to determine. Okay is This previous grid character that we've seen in the last iteration is that equal to what the grid character is now Keep in mind this L statement is crucial to this because if you don't have this L statement This if statement will execute and then this if will follow But it won't execute if we keep our else in here. So only one of these things can happen in The case of the first iteration So else if the previous key character is equal to the grid character We're looking at right now and if that's the case we can keep moving because maybe this is a winner Maybe someone has is just about to win But if it's a different character like and series of X's has been cut off by a zero Or it's been cut off by a one or two or whatever other number is necessary in the grid then okay This person did not win. This is this is not a winning move set yet So we will break out of our loop and say this is a false It's not a winner because remember we're assuming everything is a winner If it successfully gets through this loop if all of the characters are the same Based off of our previous gear grid character is equal to the current grid character my mentality so okay now Outside of this inner loop Because after we've tested one winning move if that winning move is a success if Winner is still set to true then Literally someone just want someone someone's won so we can just say out on the screen. Hey, we have a winner Take our Pretty pretty asterisk stars say that there and then we can just print F Looks like whatever character. We've just seen one Congratulations, so that will be able to handle when we have both our playing like our X character the player and the computer our player and previous gear rid it will be The character that we've been looking at all this time So let's exit with return code of zero and then we can break out of our Loop here break out of our for loop. Okay So Now we can use this check for wins function Whenever we want to display the grid out in our game loop So after we ask for our turn we can show the grid and we can check for wins If you want to do it check for wins just the very start that way It will generate the grid the first time it'll check for wins Obviously, there won't be any wins at the very start of the game It'll show the grid it'll ask for our turn and it will enter our turn And then if we won okay now it'll check for wins Etc. Etc. Because this loop is what will happen over and over again If you wanted to check for wins after you display the grid you can do that or before Let's probably do it before or after Whatever we can even show the grid after we ask our turn if we needed to duplicate that stuff And that will happen once we add the computer player turn if we move into that pretty soon so whatever let's just go ahead and Run this I guess compile this hopefully no errors cool. No errors. Let's play one five nine And hey, we have a winner x1. Congratulations. So I did that in order, right? But I could just have easily have done like Seven three five and we still Have a winner even though I was out of order in the way that we programmed it because remember This is just the arrangement of positions as long as there's a a sequence of This character being the same in all of those positions. It realizes. Okay, someone has one It doesn't matter what order they went in there or the chronological sequence of them playing those moves So this is handy for detecting a win but if we wanted to use this mentality for making a smart computer AI player or an actual like enemy or computer to play against We might have to Reverse this loop to check okay Is the is the actual human player going to win in the sequence of one two? Or are they going to win in the sequence of three two leaving that last corner as their victory spot? Can the computer just jump in front of it and make sure that the player doesn't win if they detect? Okay, the player is about to win and that's an interesting thing we can move into in the next video But right now we've got functionality to determine whether or not someone has won the game so one five nine Seven four one awesome and no matter how we get our sequence of Three across three down or diagonal we win we can detect when we win Cool simple tic-tac-toe. Let's get into adding a computer enemy a little AI to play against in the next video Thanks for watching guys. Hope you're enjoying these if you do like this Please like the video if you got something to say tell me what I suck at tell me what I can do better Tell me how you're solving these problems. Please leave a note in the comments if you're filling up for it subscribe Thank you guys for watching. Hope you're enjoying these. I'll see you in the next video