Placeholder Image

字幕列表 影片播放

  • 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.

  • Otherwise, you also could say, um, it could be like we could build this all in, like, one line, so So one option we have with strings is to join and weaken, Join a list comprehension.

  • So I'm gonna show you guys, but just know a list comprehension, Like in this case, Um, you know, it could be Let me just show you.

  • Um, because I think we have actually done one list comprehension here, but s could be equal to s 01 to 3.

  • So three spaces dot Join, um, for actually Lucy here.

  • Three spaces.

  • Let's make it.

  • Let's do this first join and then and then use a list comprehension.

  • So let's just say, uh, String I four I in range of game size size.

  • Oh, we never printed us print as Okay, 01 too.

  • So then let's do, because we know it's two spaces of padding to join.

  • Okay, so then we have 01 to those air spaced correctly.

  • So now we just need to do, and then three spaces Boom.

  • Okay, so that's another way.

  • And that's the way we could do it all in one beautiful line.

  • So again, this is gonna be a case of does like who's on your team and who's reading your code.

  • And do they need to understand this line because anybody who's like an intermediate above python programmer they're gonna understand what join does.

  • Um, and they're gonna understand this comprehension.

  • If you're working with people that are just getting started, they might understand this.

  • This might be really confusing.

  • So depending on who you're working with, you can use one of the other methods or not.

  • Let's come over here.

  • And now we use that hoops.

  • No way.

  • We're gonna just print it.

  • So?

  • So we'll just do you print boom.

  • Okay, let's come back up here.

  • Let's run it.

  • Let's make sure.

  • Uh oh.

  • Games size not defined.

  • Of course I in range, Len.

  • Game map.

  • Save that.

  • Let's come back over here, Brooke for Burberry cologne again.

  • Okay, there's are a beautiful game.

  • Lovely.

  • Okay, so now, in theory, you could make a bigger game.

  • Um, now, depending on now, somebody mentioned you could use number I zeros.

  • Here's a problem.

  • You you could use numb pie zeros.

  • And if you're not familiar with no pie zeros as totally fine.

  • Numb pie is 1/3 party package which we haven't really talked about installing third party packages.

  • But if you want Thio um you can either use pip install, you know, package name, or you could do pie Dash 3.7 for the specific version of Python dash M pip install.

  • And then the package.

  • So splits Grab, dump I really quick.

  • And, um, pie is a number crunching library, and we'll see if this even works is we do have 64 bit python.

  • Historically, this was always really hard to get 64 bit packages like numb pie that are like, see, you know, python wrappers around C code.

  • But I believe it.

  • That's all been fixed.

  • We're about to find out.

  • Wait for it.

  • Okay, we've got numb pine.

  • So And that's kind of why I like people always say like, Oh, is she using Anaconda?

  • These other packages that are, like standalone installation of python I don't really agree, because it's actually it's pretty challenging sometimes with those, but anyways, okay, we grab dump, I Let's just check.

  • Let me show you what numb pie does.

  • Pedestrian heaven here.

  • Ah, we can import numb pie as NP and numb pie has all kinds of mathematical operations for you.

  • So if you're into, like math and stats and stuff.

  • Numb pie is your best friend.

  • But for now there is a method and numb pie called numb pie zeros.

  • So it starts you off with an array with N number of zeros.

  • So we could say five and it's gonna return five zeros.

  • We could also give it a shape.

  • So five it's just a one D array or one dimensional array.

  • You could say in, um, pie zeros.

  • And then we could specify to pull here.

  • So off five by five.

  • Or let me show you the three by three Xs.

  • Really?

  • What?

  • I'm trying to get out anyways, So there's a three by three.

  • But this isn't a ray.

  • It's not pretty.

  • It's not gonna look good on our screen when we show it's gonna be hard to format.

  • We gotta reform at, like all our numbers and stuff.

  • I don't really want to do it.

  • You could convert it to a list so you could, uh uh, you could do N p zeros dot to list.

  • And then it looks like that.

  • And then you could generate, But again, it's 0.0.

  • So then you could go and move the decimal, blah, blah, blah global.

  • It would be freaking annoying.

  • So I don't think Nope.

  • I zeros is the good idea.

  • But I know if I don't say something about it someone but you could use non pi zero.

  • Okay, but I can't.

  • All right.

  • So glad we figured that one out.

  • So coming back over, coming back over here.

  • Okay, We got our dynamic size, and, honestly, tic tac toe is done.

  • If you're done with tic tac toe, you are free to leave at any point.

  • Honestly, you've learned all the basics you need.

  • There's only one more basic concept.

  • And, uh, eventually, we could probably cover it, but it's dictionaries and dictionaries or just a way to have keys and values.

  • So key one 15 key, too.

  • You know, 32 dictionaries, key one eyes.

  • How you would reference 15.

  • So if we printed that really quick, you'd see 15.

  • You can also set new values.

  • Dictionaries.

  • You could say, uh, hi there equals 92.

  • And then let's just print dictionary so you can see what I'm yakking about.

  • Boom.

  • It's been added.

  • Okay, there you go.

  • Dictionaries in, like, two seconds.

  • I was hoping we would find it used for a dictionary.

  • I just didn't.

  • So there's a lot of stuff that we still have yet to cover.

  • We didn't cover all the built in functions either, but I do think the data type of the storage of dictionaries is pretty important.

  • So there you go.

  • Dictionaries.

  • Okay, so now we're really done.

  • But I just kind of wanna play around now and make the game size dynamic.

  • So again, to Toros over.

  • You want to get out of here?

  • Go on, get.

  • But we're gonna make the game size dynamic.

  • So, uh, let's say okay, we'll keep game sized variable.

  • Um, let's say our game is gonna be an empty list.

  • Then what we could say is, four I in range of the game size.

  • We're going to say Row is an empty row four.

  • I in range.

  • Also games.

  • Eyes were just kind of kind of assume The game is square.

  • Uh, row donna P and zero.

  • And then when we're all done with that game dot upend row, but a boon but of being frank game.

  • Okay, we got a three by three.

  • We want a five by five Boone done five by five.

  • Okay, Now while I still have you.

  • Um, just for the record, you can again.

  • How?

  • Man.

  • So I got my computer.

  • I go to python programming dot net more than I go to python dot org's so it always comes up.

  • But don't forget, you can always a search, like list comprehension and learn more about this comprehension if you want to learn more about how that works on how this works, but I'm gonna take this list, put it on one line, it is important we get this tick tack toe game done in under 100 lines.

  • Um, actually, right, we kind of did it already, but I want to add the dynamic game size, and that's gonna push us over 100 lines.

  • It's really important that we stay under 100.

  • It matters.

  • Um, let's see it define two things.

  • Yeah, there's definitely gonna be want at least one more to tour because we're going to have to talk about sublime packages and stuff too.

  • I forgot about that.

  • Uh, OK, so, uh, where was I?

  • Uh, okay, So we're gonna move this to be one, um, a one liner for loop.

  • So, first of all, um, this will just be like this.

  • Here would be Just write it out.

  • OK, so it would be fore I in range.

  • Game size would be one of the things.

  • So in this case, um, it won't be, you know, normally would be, like, eye for eye if you were going to write a thing.

  • But instead, actually, it's this, you know, collection of zeros.

  • So then it's actually this list for Ryan Range game site.

  • So how many lists do we want of the zeroes?

  • What we want as many as four eye and range game size.

  • So I'm gonna build this list, and this one is zero for I in range, game size.

  • And now it's just one big list comprehension.

  • So boom, Boom.

  • We say game equals and I'm gonna comment these out just to show clips too far.

  • Prince game.

  • OK, we've done it.

  • Um, now I'm okay with it.

  • Sounds like a lot of curious why it came up twice left in the making a sense.

  • Okay, so now we have ourselves totally dynamic game.

  • So then you could have, um you could say game size equals in fluid.

  • Uh, what size?

  • Game of tic tack toe.

  • Great.

  • Okay, now they could do it.

  • So now we can come over here.

  • Copy that.

  • Come down to game here.

  • Pasted in Tab this over.

  • Uh huh.

  • Are we good?

  • Yeah.

  • 99.

  • Good, Good, good.

  • Okay, let's say that.

  • Come over here to quit and tutorial.

  • 14.

  • What size game?

  • A two by two.

  • Oh, okay.

  • So our input We, uh this needs to be in it.

  • Save you guys said you want a longer tutorial to cool.

  • So it's a 00 and then let's do 01 for player, too.

  • And then 11 player one is the winner.

  • So not only is our game, you know, itself dynamic.

  • We also have thegame size, all the winds or dynamic.

  • So you could play any sized game, tick tack toe, whatever your heart desires.

  • That's pretty cool.

  • Okay, so we have gone ahead and made the most absurd game of dynamic tick tack toe.

  • I think that's it.

  • I don't think I want to go any further.

  • There's so much stuff that we really could do, though, um, we could go and convert this.

  • You know, before we display, we could convert zeros is bases and one's toe exes and Tuesday owes we could do colors like we could use the color Rama package with python and make you know, player one green and player, too.

  • I don't know, um, purple or some other color orange and then you have space and we could make it look really good.

  • We could do a lot.

  • Um, but I think I'm going to stop it here with tic tack toe.

  • If you want to continue improving it and share it, please do feel free to post in the comments posted in the discord dot g slash Centex wherever you want.

  • And I think now I've got one more tutorial in store for you.

  • I'd like to install 1/3 party package besides numb pie that we installed real quickly just to run an example.

  • Kind of show you going through documentation and stuff like that.

  • But everything all in like one video, I still haven't decided really What I want to do if it's gonna be like pandas with data analysis or flask for a quick webbed of example.

  • Uh, I really don't know, but, uh, yeah, if you got it's probably by the time you see this video.

  • I have already decided so.

  • But you could leave a blow of you wanted, but I'll probably already decided.

  • Um, but yeah, probably just one more video on that.

  • And then, honestly, get out of here.

  • Go do stuff that's interesting to you.

  • You could even probably leave right now.

  • But if you want to check out that other video there next video, go for it.

  • Otherwise goto python permit and that get some ideas for what you want to do.

  • Even if you don't want to follow these exact tutorials.

  • There's just so much stuff that you can do in Python that you just shouldn't be in the basics anymore.

  • I think you've got enough.

  • Any time you hit an air, Google it.

  • You want to do something in Python?

  • Google?

  • It, um it's pretty simple.

  • So anyways, next video really just kind of wrapping up Siri's.

  • But if you've got questions, comments, concerns, whatever, do feel free to leave them blow.

  • Otherwise I'll see you guys in the hopefully last basics video.

  • You watch and basics tutorial.

  • You D'oh!

what's going on already.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

收尾TicTacToe--Python 3編程教程第14頁。 (Wrapping up TicTacToe - Python 3 Programming Tutorial p.14)

  • 3 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字