Placeholder Image

字幕列表 影片播放

  • What's going on?

  • Everybody Welcome to Part 13 of our tick tack toe and python three Basic Siri's Ah, we are coming down the home stretch, but we actually have, like, we basically have everything that we need done, done.

  • We just have a lot of polishing and putting together of everything.

  • So let's go ahead and get into it.

  • So the first thing that we've done is we have done all the code for validating every winner.

  • We just don't have it all together yet.

  • So what I'm gonna do is start bringing it all together.

  • So this was code for vertical winner.

  • This here is code for diagonal winners.

  • This was just a quick example of a diagonal winner, and then this was code for horizontal winners.

  • So in fact, let me cut all that will go to the tippy top boom.

  • And then we will tab all of this over and okay, we are all set.

  • I'm just gonna leave some notes here for myself.

  • I'm gonna say this is horizontal, uh, and then die ag and all.

  • And then vertical.

  • Okay, Um and then the other thing I wouldn't mind doing is like when we say we have a winner.

  • It would be actually kind of nice to say, Hey, we have a you know, such and such winners.

  • So, like, for what reason?

  • So instead, what I'm going to say is, I'm gonna make this an F string so we can pass in variables, and we're going to instead say player and the player is going to be whatever it was at Rose zero if they won, um, they're in a lot of locations, but that's good enough.

  • Player is the winner.

  • Horizontally.

  • Cool.

  • So I'm just gonna take that copy.

  • Come.

  • Oh, and you know what?

  • We don't have the checks for the diagonals.

  • We gotta add those into.

  • I can't believe we didn't do this last time.

  • Paste tab that back and then paste, they're if Roe And rather than row, it needs to be diag.

  • Sze bags.

  • Oh, my gosh, this is a lot.

  • Uh, this is a perfect example of something we absolutely must fix.

  • This is so air prone to be doing this, But first, let's fix this, and then we'll go ahead and make the function that fixed this.

  • Um, but we will do it.

  • I promise.

  • Um, okay, so that's done.

  • So this would be, um, player Blank is the winner.

  • Uh, diagonally I gone on lee, and, uh, this would be Let's see, which one was this one.

  • So this is First, we're going to do the reversed and then the other way, So reversed would be a regular upwards motion.

  • So I'm gonna just put in parentheses a slash like that.

  • So?

  • So it's, like, a little more clear.

  • Okay, They won, you know, escalating diagonally.

  • I get the right word.

  • Anyways.

  • Copy.

  • Come down here to have this over.

  • There we go is the winner diagonally, and this one will be a backslash.

  • Now, a backslash in python is a ness cape character, so it escapes the value of a different character.

  • So, for example, um, if you wanted to say, Let me just do a new thing here If you want to say a string was equal and you wanted to use always double quotes.

  • But then you also had, like, let's do single quotes, cause it'll make more sense.

  • So single quotes.

  • And then you wanted to say don't.

  • But you wanted the apostrophe t Well, that's going to be a violation.

  • And python, right?

  • Because you closed it off here and they've got this t this thing.

  • So what you can do to make it legal is backslash You escape the value of that quote and instead, it's just the string element of it, and this makes it valid.

  • And if we were a print string, it would just be don't it wouldn't be Don't backslash you know, our dawn backslash quote, t quote or whatever.

  • So the same thing is true with the backslash.

  • If you actually want to see a backslash on a string, we're going to do double backslash.

  • So we escape the value of a backslash.

  • So that's why you have to back slashes.

  • They're okay.

  • Vertical.

  • Uh, And then again, I'm just going thio copy this paste and it's not check zero.

  • It's diet.

  • It's not the AG zero, it's check zero.

  • Close that off.

  • And this is, uh, vertically and boom.

  • Oh, right.

  • So and I mean, honestly, we probably don't need anything for vertically, but we have the bar now.

  • It makes me want to do one for the horizontal, but, um, no.

  • Anyway, um, let's see.

  • So the next thing that we're gonna do is.

  • So we've got all the winds handled for now.

  • So, um, so now we're ready to bring in the rest of the game.

  • And this is how you would, like, handle?

  • For if a game has been one and really quick, so we should get a vertical wind there.

  • Uh, got a diagonal win.

  • Oh, Okay.

  • Sorry.

  • So stupid.

  • OK, now we should get a vertical win.

  • Okay, Great.

  • And then let's test a horizontal wind, too, and then we'll put it to here.

  • Player to has is the winner of horizontally.

  • Okay, great.

  • So Okay, we've got that.

  • Now we need to bring in all of the gains stuff, and we haven't touched game since.

  • I believe this was our part nine.

  • So we will open up part nine, and then we're just gonna copy and paste game.

  • We'll just copy and paste all this stuff.

  • So the whole game board thing and then some examples of our game board.

  • We're going to bring all of that in.

  • So I'm gonna come over here, and, um, we're going to escape this.

  • I'm gonna delete win game for now.

  • I'm gonna scroll out just so we could do some modifications paste that in.

  • So now I've got code to determine a win.

  • That's great.

  • And then now we've got our actual game board, um, in here.

  • So now what we want to do is actually kind of program in the ability for us to, like, kind of play a game and iterated through and stuff like that.

  • So the first thing I would say is, let's just comment desires one so I don't have to type it in a minute.

  • But basically, we're say, play is let's say equal to true here.

  • And then we're gonna say players player errors is equal to you.

  • Got, like, a player one in two and then wow, play because eventually because basically, we would want to let people continue playing tic tack toe until they don't want to play tick tack toe no more.

  • And so, uh, so we're gonna let them do that.

  • Now, I'm gonna copy this.

  • Come down here while play.

  • Okay, So now the game actually is gonna be redefined every time on start tab over, and actually, those should all be zeros.

  • Lucy, is this okay?

  • This one's in all zeros, so this might be quicker.

  • Just a copy and paste this one, to be honest, then tell Like, fix all those paste.

  • OK, tab over.

  • Great.

  • Um, somebody was complaining about trailing Thomas.

  • Fix it.

  • Okay, um, and then maybe add a couple here.

  • Right?

  • Okay.

  • Anyway, so while play, Okay.

  • So every time we start a new game of tic tack toe where you are going to want to re set the game, Okay.

  • So now what we're gonna do is we need to while we could randomly pick a starting player if we wanted.

  • Um, but I don't really think you want unnecessarily random.

  • Like, uh, it's gonna cause some trouble later on what we can talk about randomly starting with a player.

  • But you guys like where is playing the game could also just generate who you know, with each moderate, you know, work that out with each other.

  • Who gets to be player number one and two.

  • I don't really think that's a central that we change player one into.

  • There's really no reason for that.

  • So But if you want to check it out in the text based version, I kind of talk about what you could do to do that to randomly pick.

  • But for now, I don't really think that we're going to do that.

  • So So the game begins and then we wanna have You know, basically, we want to say Game one equals false right?

  • And then for a CZ long as Game one is equal false.

  • We want to continue, first of all, switching between players, but also, you know, displaying the game board and inputting and taking in players movements that they want to actually play.

  • So, um so obviously at the gate.

  • Because, like while not while play, that means we want to continue playing tic tack toe.

  • So this is really, like game wide.

  • Like, Are we still playing a Are we still interested in playing tic tac toe?

  • But then we need a loop for the specific tick tack toe game that we're playing right now.

  • So then we would say, Wow, not Game one.

  • So this will continue to run run until the game has been won.

  • And basically, at this point, we're going continue taking in player movement.

  • So, for example, if you want to take in input from a user you are going to use, um, basically, it's you would say column choice equals in an input.

  • And then you could say, What call?

  • Um, do you want to play?

  • And then let's go ahead and just remind them you have 01 in two and then for input, always add a little space.

  • Otherwise, when they type something, it's going to, uh, it'll appear immediately after the strings.

  • So it's always nice to add a little space there, so that's column choice will copy that paste, and then we'll do a row choice row choice.

  • Um, also, we want some way to pick a current player and all that.

  • But for now, I think I'm gonna let that go.

  • Well, just say current player equals one.

  • We're gonna will come back to that, I promise.

  • So Current player calling choice road choice.

  • And then you would want to let them actually play the game so we would do this was a game equals game board rather than game board.

  • That was.

  • We used that back when we were trying to throw a throat purposely throwing error player equals.

  • And so again, because we have, like, decently named variables.

  • We don't need to do the whole, you know, play you don't have to give the actual parameter name will just pass these variables.

  • So current player.

  • And then we had ro choice and then column choice.

  • Okay, so we play the game.

  • But remember, before we actually play, the game would be useful just to display the game board.

  • Once.

  • Every time this is run, the game board will be displayed.

  • But for now, let's go and copy.

  • And then before the game actually runs, we will go ahead and pass.

  • Game equals game board, and then it will just continue to keep running.

  • Okay, so, um, so one option.

  • Let's see.

  • Let's go and save this.

  • Actually.

  • Wow.

  • Not so we'll always be player one, and then let's try to run it.

  • Now, if I recall right input in sublime.

  • It's kind of doesn't really work, and it still doesn't really work, so we can't use sublime anymore.

  • We'll have to use the good old, uh, command prompt.

  • So cmd pie, Dash three point seven and then tutorial.

  • Third tutorial 13 dot pie enter.

  • Okay.

  • What?

  • Colin, do we wanna play?

  • Let's just play zero.

  • Oh, we'd said call him twice.

  • Okay, So Okay, so we made a couple of mistakes.

  • So first of all, um, we said calm.

  • This should be row, row, and then hear this input returns.

  • Always a string, because whenever you input something it doesn't know is this is gonna be a float, an aunt or whatever.

  • So it's always a string.

  • So now what we want to do is convert this to an ent.

  • We can do that with a built in function literally int in parentheses, whatever you want to convert, so I'll just do that isn't So now we've converted it to an integer.

  • I'm gonna save that.

  • Come back over here.

  • We're gonna break out, rerun it with the up arrow.

  • Enter.

  • Let's try again.

  • 00 Okay, great.

  • So he played at 00 We could play a 11 Now it's it's always gonna be player one, because we're not changing to two.

  • Okay, great.

  • So that's working.

  • Um, pretty well, now, what we want to do is change which user, um, or which player is actually playing now?

  • There's a bunch of different ways that we could do this.

  • And I think what I'd like to do is just show some examples.

  • So what?

  • I'm gonna do is just creating new thing.

  • We're gonna call it testing grounds or something.

  • We'll open up in sublime, and I'm just gonna clear this out.

  • And so So there's, like, a bunch of different examples of how to, um, how to change players.

  • So, for example, um, trying to trying to find the way I initially had done it.

  • Uh, it's in the text based version.

  • And now I can't I don't know which one it was, anyway, but basically what we could search like, let's just search it.

  • So So we're looking Thio.

  • Oh, let's say rotate between players rotate between items and a list so, like, rotate between items in a list high thon.

  • Okay, Python list rotation is the first thing that comes up.

  • Rotate zero, rotate one.

  • So this actually appears to be rotating the list.

  • 00 this is pseudo code.

  • Yeah, you wish.

  • Okay, I've known I was like, I've never seen.

  • Okay, this is some pretty curious looking code will continue running.

  • Okay.

  • Dake you there is a rotate here, and you can rotate it by one.

  • This seems to be like more for, like, longer lists.

  • Like what if we typed um, python flip between, um, we could do zero in one.

  • We don't really want 01 We want one into, but we could always add plus one.

  • So we scroll down.

  • Uh, yeah.

  • Okay.

  • You could do that.

  • One minus vow would get you where you want it to be.

  • That's kind of funny.

  • Um, and then you could use tour falls.

  • Yeah, it's still not quite what I'm looking for.

  • So that one's kind of curious.

  • Like what?

  • If we like?

  • Let's go to the testing ground here.

  • Let's say we have players equals list.

  • It's one.

  • Actually, we probably need to use your own one here and actually probably not zero on one.

  • We probably do you?

  • Yeah, 10 So what this is going to do is this is going to kind of abuse the fact that Index it's gonna abuse the fact that we want to use your own one, and then we can use the fact that one's index zero zero's indexes one.

  • So what if we said four I in range 10.

  • We'll just Normally this would be like a wild troops like this would be our tick tack toe loop, but for now we'll just go through 10 generations.

  • We could say, Um, let's a current player equals one So current player equals one.

  • Um or let's say, in fact, let's a choice equals one.

  • Then what we could say is, um, current Well, say current player equals choice plus one, because that's actually like we always have to add one in this case so we could print clips print current player.

  • But then we can say choice equals players with the index of choice.

  • So run that and you'll see here we're going to generate between player one and two and one and two and one and two.

  • And if we didn't add one, it would be zero and one.

  • Maybe that would be a little more clear to everybody.

  • So that's one way we could just generate between players every single time.

  • Um, so that's an option.

  • It's come back here.

  • I'm just kind of looking through.

  • Okay, so then flip between zero and one python.

  • Uh, cycle values in list.

  • Starting at a circuit.

  • Certainly.

  • I'm not sure what if that would be useful, but what I'm looking for, right.

  • So what I was looking for was inter tools has a method called cycle, so we can use dot cycle and what?

  • That's gonna d'oh!

  • Um, and I'm trying to see So the term I had originally searched was actually python flip between, um, python flip between numbers.

  • We'll see if this will come up with the same one scroll that.

  • Okay, so this is the one I had found and I actually never heard of it.

  • Er tools dot cycle because as I was writing my method of doing it the way I was doing it was so ugly that I was like, Surely there's a way.

  • And then also, I was like, Surely there's a way to, like, actually rotate.

  • So we've seen I think it was a day que Africa.

  • Which one it was that we found before That actually has a way to rotate.

  • And then you could always use, like, the index of zero or something like that.

  • But, um, so coming on, let's see.

  • So let's try using inter tools cycles.

  • So So, looking down here, you can import psycho.

  • This is just a range of two.

  • So that would be like 01 Basically, um, they're calling dot next on now, with my experience.

  • I know that's not gonna work anymore, But let's try.

  • Let's try how to, uh, how we would actually like do that.

  • So So the other option we would have is import it, er, tools.

  • And then we could say Player choice equals it or tools dot cycle.

  • And then we're gonna cycle between player one and two because we can.

  • And then for I in range of, let's say, 10 what could we do?

  • Well, we could just print player choice not next now already know this is not gonna be happy.

  • But let's say you get this error right and don't forget like this stuff is sublime.

  • Really?

  • If we ran this in a console, we would see this.

  • So I understand what this means, but you might not.

  • So again, you copy that error.

  • Come over here, paste it in, go to this first option.

  • Who cares what they're doing?

  • Let's just look at this guy's what he has to say.

  • Okay dot Next was removed in Python three used next hitter instead, right?

  • So then he says, all you have to do is take dot next and then converted to next as a as the built in function that it is.

  • So let's do that.

  • So let's just say we get rid of dot next, and then we print instead.

  • Next player choice.

  • Boom.

  • Okay, so that works.

  • And that's actually much more condensed er than what we were writing anyways, so that's pretty good.

  • Um, I think that's I think that's what I would go with.

  • Um, I think that makes the most sense.

  • Only confusing thing maybe, is next.

  • But all next does is does the next thing in and it terrible.

  • So, for example, if I said, um, ex, actually, let's leave that Codex we're gonna we're gonna use it pretty soon, probably.

  • So I'm gonna do this if I said x equals range.

  • Five prints.

  • Next X.

  • Oh, I thought that was gonna work.

  • Oh, we can't.

  • Fine.

  • Okay, uh, we should be able to convert it to a list, though.

  • No, you're still not gonna let me do that.

  • Okay, uh, we have to generate over the next a way.

  • This is beside the point.

  • I'm going to go down that route, but any time you have an ID aerator, not it's not the same as an honorable That's the mistake I'm making here, Um, you can just use the next to grab the next value from it.

  • Um, but anyways, let's go back to the tutorial here, Um and so to to actually, like, pick our player.

  • We're just going to say, um let's say player choice equals better tools.

  • So basically, all we're gonna do is we're gonna come up here, and this is our honorable under the rarest of circumstances, I will go back and edited video, and this is one of them.

  • So you know the question of it.

  • Aerator versus Itaru Bull is the type of thing that's gonna show up on it on a test.

  • Thio do like a gotcha moment.

  • Basically.

  • So I think it's pretty important that we cover and understand this.

  • So, first of all, an eter, a bull?

  • This is a thing we can iterated over.

  • Okay, now in it, error it aerator.

  • This is a special object with next method.

  • Okay, so now in a rubble is honorable and object Everything in pythons and objects.

  • So a list, you know, that's a list object.

  • It is an object, but it aerator the thing to keep in mind is it has some extra little methods associated with it.

  • That one you can use next, but to also have a stop it oration.

  • And this is what's gonna, um it's just a major difference that depend on your use case.

  • You may or may not want it.

  • So, for example, let's just go over a couple examples real quick.

  • So say X is a list.

  • It's 123 and four this literal.

  • Is it an it aerator?

  • No, not in it aerator because we can't do print, uh, next X right.

  • We can't do that.

  • But we can, you know, for Ryan X print I, right, we can generate over it.

  • No problem.

  • So that is an honorable now recall.

  • What we're showing here is import it, er, tools.

  • And then down here, let's say, um, we'll call, uh n equals better tools dot cycle X.

  • Now this is it aerator for sure, right?

  • We were pretty confident If we're doing something with her tools on it, it's probably a generator, so it should have a next method.

  • So let's just print next and run that.

  • Okay, we get a one now.

  • What if we print what we do to next ends.

  • So as you can see 123 it's remembered where it is.

  • But what if we continued to do this copy base base?

  • Interestingly, we get a multiple cycles through here because this thing basically infinitely cycles.

  • So the next question is, Well, is it it horrible for eye in end prints?

  • I looks like it isjust forever because it cycles on and on and on, so also venerable.

  • Now, what if we don't really want to do that?

  • Like, what about X?

  • So we've already to stab established.

  • The X is not.

  • While it's honorable, it is not it.

  • Array tour.

  • Can we make it, innit?

  • Aerator absolutely.

  • Using a built in function called ITER.

  • So we could say why equals ITER X And then the question is, what if Can we now Can we iterated over?

  • Why?

  • For eye in Why Print tie And you see Okay, it appears that we're still able to iterated over Why so yet again, Uh, it isn't enrage her also honorable.

  • But then the question is, What about here?

  • What?

  • What about after?

  • Like what if we try to do it twice?

  • You see, we can't actually do that and also, for example, we could, um, let's print.

  • And in fact, let's not even print next.

  • So first of all, actually will do it for now.

  • We'll say print next.

  • Why KC one?

  • And you kind of see the same thing that we saw one, too, with cycle.

  • But the interesting thing, too, is you could say next Why?

  • But then for I in why print I So what you're going to see now is it says 234 because we actually, because it's an iterated her.

  • We we did the next here and now the only remaining elements are 23 and four.

  • And then it's done.

  • You can no longer continue to iterated over why you've reached the end of you know you've reached this.

  • Stop it oration.

  • So the big thing to take home here, isn't it?

  • It's like you know you can generate over it aerators with the interim bull.

  • It's not necessarily in it aerator, but some it aerators could be literal.

  • Okay, and the big thing to take away in a rubble, it's literally can we generate over it and iterated as in like with a four loop or something like that, um so anything you could do a string A list.

  • These kinds of things are It's horrible, but they're not It aerators.

  • And the thing with it aerator is it comes with the special next method.

  • Also the stop it oration.

  • You get this kind of behavior from it, you reach an end.

  • Typically, because again, this is an iterated er also in literal.

  • And as you saw, it goes on forever.

  • It's It's like it's just a cycle, right?

  • There is no stopping oration.

  • It just goes back to the beginning, All right.

  • Bonus material for you guys.

  • So anyways, player choice equals better tools dot cycle.

  • So then current player.

  • Well, in this case, we just need to always call next player choice.

  • And now we have our next players is going to test that Really Quick.

  • Break this.

  • Run that hoops.

  • We didn't imported her tools.

  • Let's go to that.

  • Come up to the top, impor ITER tools.

  • Go and save it.

  • Come back over here.

  • Run it again!

  • Okay, Well, so, player, uh, we're not printing out what player we are.

  • We'll do that again to let's just do 11 Okay, you can see that it's which players.

  • But we should also probably print out which player we are.

  • We'll come back down here.

  • Current player equals next.

  • So print.

  • Um, f what have I done?

  • Oh, F area current player, and then this will be a current player.

  • Okay, say that.

  • Let's run it one more time.

  • Current player is 100 Okay, then.

  • 11 Okay.

  • So we could see a current player.

  • Two plays.

  • Okay, great.

  • Okay.

  • So I think I'm gonna stop here.

  • This is getting pretty long already.

  • Um, and we still have quite a few things to do, for example, where players can still play on top of each other.

  • So, for example, player number one could now play over 11 and they just took that spot.

  • We just got a funny, but obviously we don't want to let that be happening.

  • We're not actually using our win here.

  • We've got a lot of repetition.

  • Also, where's our little lovely print?

  • This is not scalable and probably a couple of things that I'm not thinking about.

  • So we still have ah, few more things that we need to clean up.

  • But I'm hoping that I could do all of it in the next video if I don't let myself get hung up on trying to show you guys a miserable Okay, so questions, comments, concerns, whatever.

  • Feel free to leave and below.

  • Otherwise, I'm gonna see you guys in the next video.

What's going on?

字幕與單字

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

A2 初級

把東西放在一起 - 迭代器/可迭代 - Python 3編程教程第13頁 (Bringing things together - Iterators/Iterables - Python 3 Programming Tutorial p.13)

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