字幕表 動画を再生する 英語字幕をプリント what's going on already. Welcome apart. 14 of the python three Basic Siri's where we have been building a game of tic tac toe in order to learn most of the basics in Python. So, uh, where we left off, we were kind of bringing everything together. We've got a game sort of functioning, but we still have to connect like the win. We've got some copied code here in the wind that we'd like to fix up. Players can play over each other. This is hard coded. We gotta fix that, um, stuff like that. So let's go ahead and just get started. So the first thing I think will solve is players playing over each other. So the first thing is basically right. You know, with this input, we could just quickly check the game map and make sure hey, is anything in that position? Because there's some things in that position we don't want toe let someone play there. So all we're gonna say is if game map, um and then it is, uh, row and then column. Uh, if game map row column Basically, if that doesn't equal zero, that means somebody has played there, so so we just want to return. Uh, this position position is occupied. Pato choose another. Choose another, Okay? And then we'll also return a false and then we can kind of make this entire thing. If it doesn't return a game map, let's make a return fall. So it's also add return falls here and return falls here. If we don't do that, basically, if you enter anything invalid or hit an air, you're you lose your turn, which we you know, that might be a feature, not a bug. But for now, ah, well, return false, and we'll just continue giving them an opportunity to fix it. Also, I'm gonna remove this definition a game that doesn't need to be there. Cool. All right, so now this returns false for a variety of reasons, So we need to handle for that as well. So we've got a current player and we can print the current player. Um, now what we want to do basically will come down here and then we're going to ask a question of played, and then we're going to say that's equal false. And then we're gonna say while not played, we're going to attempt to run this block of code. But then if game is true, So if game, um, we're gonna say played equals True. So this should break us out of, um, over a little, uh, loop here and switch players basically. So let's go ahead and test that really quickly. Uh, so it's just run this 14 Cool will play at 11 k. We switched player to a straight 11 again, positions occupied. How about 01 bull object is not scribble. I played a game. Um huh. Boule objects play players, play palladio equals false. So while it's the case that we've not played, you know, I guess part of the issue here. Like if this did return falls, um, later. If you tried to input Laki. See, we're pride getting in trouble really quickly, actually, Um, because if that returns false, the game map has been redefined. Uh, and we can't really let that happen now, can we? So why don't we We'll have to change this ever so slightly to be ah, return game. We need to return game map and the Boolean statement. So let me just, uh we'll do this Eso game map False and then here will return true. And then here game, Matt Falls game map False. That way. Now we come down here and when we play the game game, um, will also get a return. Now, in this case, we don't care about the returns on Python. If you don't care about one of the things that's gonna be returned, you use an underscore and underscored Just means it doesn't matter basically. But then down here game and then played equals the following. So now I guess I could just do this. And now let's save that. Let's rerun that and see how he did. So Andi think. And that's why it says bull is not script herbal. I think what was happening is it returns falls, we try to play another location, and we're attempting toe choose an index in a bowling, which obviously doesn't exist. So 11 Okay. Current player to 11 We can't do that spot. How about 01? Okay, so now player to complain at position 01 Okay. Um, I think that works. We don't have the winds all set up yet, so we also, uh, need to account for the win and we also kind of want to consolidate our winds or win here. Basically, we're always doing the exact same thing in the wind. And like this line now, what happens if we stop using zero as like the space? Like maybe we give it a space. We start formatting things. We use exes and oh's and Spaces. Well, if we do that, then we gotta come through and change all these. But what's the chance that you know, we're on this screen and we're thinking, Oh, there's only three things we change. 123 save exit. And darn it, we forgot this one, right? We're gonna be in trouble. So, um, you generally want to consolidate where you can and again, any time you've got copied things, it should just be a red flag to you that something is probably wrong. So the next thing I want to do is create a quick little function in here because define all same, all same. And we passed a list and we determine is this thing all the same? So let's just copy any one of these. So l is for a list that's gonna be our parameter. So if that is the case we return True. It's true. With a capital T ells return falls. And then rather than being row, we want it to be l and l will and l Okay, so this is our nice little function to determine all same. So then we just need to replace this if all same. And in this case, it's row that we're using now. Uh, well, let me replace all of these, and then we'll we'll talk about something for a moment and let me just make sure I get this right. So if and then this one is brother than row, it's diag. Sze die ags And then down here, it's the same things. I'm just gonna copy paste, and then here it's the same thing. But this time it is a check. Okay, cool. So we also have you know, this little statement like this statement here is pretty repetitive s. So you could maybe also include the print statement here and then pass a parameter of like, what type of win it was. Maybe that might be a good idea. And I'm not really sure they're, um because it's a string where we are like this is going to inherently be pretty hard coded, but you might want to do, like, you could probably do that and then you to eliminate four lines. So anyway, there is that, um Now, what we want to do is basically, this function also needs to We need to know, Is there a winner? And if there is a winner, we want to also return things. So this is like a you know, this is ah, you know, function within the function. But hear this function actually doesn't return anything this return stuff, but only to this parent function. But hear this function, actually, only just prints stuff doesn't actually do anything. So, um, if there is a win, we would like to return True. All the more reason why you might want to put this in here and then say who the winner is and then maybe pass the are still trying to decide if I want to do it. I think I'm gonna hold off, but homework. You should do it. Um, it would make more sense. We should just we deprive, want to do that. But anyways, for now, let me just continue this way. Return true. I'd like to wrap up. We could spend forever on tick tack toe, and I definitely want to wrap it up. Um, and I want to get you guys out of the basics. I want you guys to go into your own thing. So I'm gonna just say that you probably should also pull that in. But the more important thing for us to have fixed is this Getting this to be an all in one spot anyway, Return to return. True. Okay. And then we want to return true here. You could save eight lines now anyway, uh, and then at the end of this function, let's return a false. So has this work. So basically, if there is a win, the function returns true. And whenever a function returns something, that's it. It's not gonna run any further in that function. So, like if someone won both horizontally and diagonally like you just started the game like that, it's not gonna get It's only gonna find the horizontal winner. It wouldn't even recognize it would never get to the code to run diagonal. But if this is not the case, none of these returns will return. You'll get to the end of the function. Boom returns false, which means nobody won. So this makes sense. It makes sense. And it maps to name the name of the function pretty well, because win is either false or if someone one win is true. Okay, So, um, now what we want to dio is if someone has one So hat here. Wow, not game one. We play, and we just continue to play until the play actually works. Great. So then inside of this, not game one. This is where we need to determine if there's been a winner and all that. So first of all, if I win and we want to pass game, if there is a win to the game, First of all, let's go ahead. And Game one equals true. That will, um that would basically end everything for us. I'm not really sure I want to do that immediately, but, um, the next thing we should do is ask the player. Hey, do you want to play again? And so again, is this gonna be equal to input? The game is over. Would you like to play again? And then we'll just put Yes, no in parentheses, um, at a space here. And then we'll just ask, you know, we'll try to discern what What did they want? So if again, And maybe the user had caps entered or whatever. So I always like to Whenever I'm getting text input, I like to kind of normalize it to the extent that we just do lower. And all this does is if there's any upper case letters, it just lower case is them. You can do the exact same thing with upper. So these air just methods that we can run against a string, But I'm gonna go with lower. So if again dot lower is equal to, um basically, yes, we'll just print, um, restarting, because play my play is equal to true. So So this loop here is just gonna run forever. Okay, um, so if they said yes, we just restart, and we're gonna bop all the way because now this. Lissy, this loop is over with, right? This loop is over with, so then we come back up here. We start with a fresh game in the cycle continues. But if again dot lower is equal to end. Um, what would we would like to do. Well, they don't want to play anymore. So by right, um, and then we can say play equals false, and then we're done. Now, one thing to take note with is, if this is true, it's gonna print, restarting, and it's still going to get to this and run this statement. So instead, what we prize should say is l If so, if something's the case else, if something else is the case is what LF kind of means now, possibly this doesn't handle for everything. So possibly the user, um, you know, maybe they hit be okay. Uh, so we want to handle for if the user has fat fingers, we want to say, um, not a valid answer. So see you later, alligator. Okay, play equals falls. So, uh, so then, you know, we actually could condense this quite a bit. We could have probably said, um, you know, instead of that block, we actually probably have said if if again dot lower equals n, we'll see. How can it Lucy, I guess really. What we could have said is, why else by or something like that? Anyways, I'll leave it here. Okay, So now we can exit the game and be done with the game if we if we so choose. So, uh, let's test this code really quickly. We've been writing a lot of junk, so let's do 001 to 01 and then to one. So player, one should win. Did I screw something up? What? What happened to one? I have no idea what just happened. Uh, zero. We definitely want. Let's see columns in this player. One column, one row two Whoa. Player One is the winner vertically. The game is over. Would you like to play again? Uh, let's hit. No, I just want to test that first by. Okay, great. And then Now let's just do the same thing again. 001 201 21 zero two. Okay. Ah, player 11 Let's play again and just see that it refreshes the game. Okay? It does. Player One continues to one. Okay. Okay. Great. So that's looking good. So what else do we have now? Um, the next thing would be I think, for us to look for water. Next thing would be for us to fix this. Okay, um, I try to think. Do we have anything else here? Is that the last thing? I think that's the last thing besides this. We really should consolidate that. I'm probably gonna do it in this video, but we really should, um I really think that's it. Like, that's the last thing I wanna, um, do. So let's talk about that string. So I've left my testing grounds. So let's talk about the rule of this string. So first of all, let me go get it, because this is the string we're gonna attempt Thio d'oh! And then let's just say, uh, game size eagle's three. Okay, so where's the rules of the string? Well, we started with 123 spaces and then each number increments by one. And then we have 12 spaces between them. So the first thing that we could say is like s on then s could equal. Um, let's say 123 We start with three spaces and then four I in range of game size. So this will be another example where I is completely Listen well, actually, I is important in this one is about to tell you you could use underscore don't do that. Okay, Before I in range, game size, uh, we would like to s plus equals, uh and then it's the string version of I plus two spaces. So space space, right? And then we could print as and then we can see So the 1st 1 gets printed out, and then this is the second when we calculated okay, so that makes sense. The other thing is, you could look at it another way where it's actually one space, and then two spaces lied before because, actually, but we've just written is three spaces. And then when it's actually printing out, is this so it's actually not the same. So if we lead with one space and then said plus two spaces and then the number is probably a little more two spaces plus string, I That's a little more true to what is going on here. Now, the next thing is So this is this measure. Yeah, So that's a valid way we could build that Now, um, if you want, you can stick with that.