 Alright, what's going on guys? John Hammond here coming back at you with another YouTube video and we are getting back into our C++ program to make a simple tic-tac-toe game. So we created just some small testing stuff in the previous video where we could define our tic-tac-toe grid, actually generate it in the data form in C++ and the actual programming language, and then display it out to the screen. So let's create an object that will actually be able to keep track of the stuff and bundle it all for us. So C++ specifically, because that's where you get classes, C doesn't have classes just as structs as far as I know. You can tell me some crazy voodoo thing, but you got to specify a private or public kind of selectors or a modifier or whatever the name. The specific word is for the variables that this class keeps to itself. So for one thing, we have the grid and now let's create some functions like show grid or generate grid. And I'm going to have a void type because they aren't going to return anything and they don't even really need arguments. So we can go ahead and just copy all the code we had written before and put that in our generate grid function. Do some nice indentation. So that will automatically create the grid and we can probably put that in the constructor of our object. And we'll do the same for our show grid here. Okay, cool. So we do want a constructor. So constructor is a special, special function because it has no return type and it is the name of the class. We don't need any arguments for this one, but we do want to generate our grid and then let's show the grid. So have that done here. And once that's done, let's make sure we create the object in our main function. Go ahead and compile this. And okay, it looks like it's still working just fine. Now we're just encapsulated in an object in our game class. Okay, so let's move on to actually asking the player where they would like to put their turn. Like where do you want to place your character? So let's create a function for this. I'm going to call this ask turn. Again, don't need any return value here. And I'm going to store the user's input in a string, C++ style string. And we have that included so we can do that. String input, make sure it's declared and ready. And I want a loop to keep asking for this person's input. So while true in this case, so while one and we can break out of it as we need to. But we want to be able to see, okay, are you going to give me proper input? Can you tell me something for real? And if not, I'm going to keep asking you something over and over and over again. So I'm just going to use puts to put this on the screen and ask them where would you like to play? And I'm just using puts because it automatically gives me a new line character rather than printf and it doesn't. So that's handy. And I'll just use get line for sin, see in and input the file or sorry, not the file, but the variable that I want to store the input in. So while that's being processed, once we get that line back, we can test, okay, if input is not equal to like nothing or an empty string, then we can handle it. If it's actually equal to an empty string, we can just tell the user, hey, you have to like actually enter something. So let's display that you must enter something. Cool. Have our semicolon because we're in C++. And if they do enter something inside this condition, we want to get out really just the first character, right? Because they're going to input either a one or two or three or four or five all the way up to nine. But we only want the first character because that's really all that's going to be really valuable, whether it's a one or nine, that's only one digit long. So just for kind of ease and convenience on our end as the developers, let's just kind of cut out the first actually entered character that they supply. So I'm going to call this entered. And I'm going to use that same kind of messy tactic as earlier where I take the C++ style string, convert it to the C string, and then just take the first character out of it with an index like that. So, okay, that's handy for us because we get what the user entered as a character. And since characters in C are the exact same thing as integers in a weird way. You can actually do like tests and comparison operators like less than or greater than with other characters. So this works in a convenient way for us because we're able to test. Oh, is the entered string greater than or equal to like the character of one or or and even and less than nine. Because we want to make sure what they enter is in the range of one, two, three, four, five, six, seven, eight, nine. Any other character like an A or a W or weird stuff would be wrong. So again, inside this condition, we can handle it. But if they don't supply that, if it's outside of that range, we can say like you have to play in the range like one through nine. So they know you have to specify a one through nine to work with. So now let's actually make sure that's working. Let's say you entered, and let's do entered here. Weird that I'm combining C out inputs. Yeah, I'm sure you guys can yell at me in the comments for that. I know I'm weird. I'm sorry. I know it's bad. I'm awful. I'm the worst. Don't don't do anything that I do. All right, so now let's after we show the grid, we can ask for the turn. Make sure that's in there. Where would you like to play? I'd be like what? And it'd be like, no, you have to enter something in range of one through nine. If I enter nothing, it says, oh, you have to enter something. So let's say five, it says, okay, great. You entered five, nine, et cetera, et cetera. Cool. Now let's actually make a decision and handle what's done with this input. So we're going to get the integer form of our entered character now. And we can do that in kind of a cheap hack by saying the entered number equals entered number. A different variable name is equal to entered character minus the character form of zero. And again, because C characters are all really just integers in the background, this will kind of bring it back to a decimal base and get it as an integer form. So that is one interesting way we can promote that character back to an integer. So now if we want it in zero base, because right now this is going to be one based one through nine, we want it in zero through eight or whatever the case may be, we can say index, a new variable for it entered number minus one. Okay, great. We may be able to actually just combine this. We probably don't need the separate variable, but I'll let you handle that whatever. Now, we have an interesting number like zero through eight, which has to be placed on our grid somewhere. So thankfully for us, since we're working in those, I'm sorry, those three by three grid, if we're going to be zero based, we can do like zero divided by three, or one divided by three, or two divided by three, or eventually three divided by three, and then we'll be able to see, okay, what row or column are we in by simple division or determining what the remainder is, like zero, mod three is still going to be zero, but one mod three is going to be one, two mod three is going to be three, three mod three is going to be zero. So we can use the modulus operator to determine what column we're in, and we can use the division operator to determine what row we're in. And you'll see that it's just wrapping around in a kind of a convenient way, because it's all that same division. So in row will equal to zero based index divided by three and in call for column will be index modulus three neat. Okay, so we can test now what grid position they're actually in by seeing the grid variable at that row and column. Great. So let's say the character grid position equals that. So see out. You want to play at grid position, and then just display the grid position. This doesn't really look like it's done anything special for us because it's going to give us the exact number that we had supplied, but it's still doing good stuff because it's finding that position in our grid, like array or multi-dimensional array. So that's pretty handy for us. Okay, so now we can test whether or not that grid position has been used up or not, if we haven't already placed something there. So if that character is something like an X or if that character is something like an O, we can say, hey, that grid position is already taken. But if it's not, then we can successfully put something there. So we can say that grid value at the row and the column that we want is going to be our X character. We are going to make our move there. We're going to play there, and then we can break out of our loop so we are done with this turn. But everything else does not break and will continue in the loop. So you'll keep asking the same question, where do you want to place your move? Cool. So now we can go ahead and put that kind of in like a main loop for our game. Let's go to constructor, and after we've generated the grid, we can do a while true, show the grid, and then ask for your turn. So every time we show the grid, it'll update between the move that we've made after our turn and it'll keep asking for us. So we don't have a computer to play against right now, but if we wanted to play at position one, you'll see it's filled with an X right here. Wanted to play a position nine, filled with an X. If we wanted to play a position nine again, it would say, oh, you can't do that because that grid position is already taken. We could say five, and hey, we would have one. In fact, we did win in the regular game of tic-tac-toe, but we don't have implementation for that yet. We haven't actually programmed the fact that we can win. In fact, if we wanted to, we could just play on every single board here, and now we wouldn't have any position we can move into, so that's a peculiar thing. But that's all for now. That's all I wanted to showcase is now we have an ask turn function that lets the user or the player actually make a decision and place a turn on the tic-tac-toe grid or the tic-tac-toe board. So, cool. Thank you guys for watching. I appreciate you putting up with me and my weird C out inputs and print def combinations. Hope that's not too intolerable, but thank you guys for watching. If you did like this video, please do click that like button, comment what I can do better, comment how you're doing this, what cool stuff you can do in programming. If you're feeling up for it, subscribe, and if you want, thank you guys. I'll see you later.