Python for Absolute Beginners Transcripts
Chapter: Problem solving techniques for writing software
Lecture: Demo: Choose the active player

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Our next step in our divide and conquer is to choose the initial player but in fact we don't even define the players yet
0:07 so we're going to need to figure out how we represent the players and then choose one of them, okay. So it's kind of uncovered something
0:14 that we didn't lay out, is decide who the players are. I guess that's, that maybe in the traditional sense happens up here.
0:23 Right, this is when you're sitting at the table, you say Hey, let's play tic-tac-toe that means the players are me and whoever I'm addressing
0:30 so, well we're going to put it in a different order just so we can have our board up here. Now, this stuff, I've already committed this to GitHub
0:37 so if you want to go and have it to look at you just go and look at the history or this file here, and it'll be there but just to keep us sane
0:44 I'm going to remove that. Now the next thing we need to do is have the players so we could have it like we had in rock, paper, scissors
0:52 We had you and I suppose two it says computer, something like that but you'll remember this resulted in a lot of challenging things.
1:03 We then had to have number of wins for player_1 number of wins for player_2 who is the player, right
1:10 there was a lot of stuff that we had to keep kind of lined up so it turns out that we can go back to our data structures again
1:17 and think about this in a more general sense. Often in programming it's easier to solve the problem in general
1:23 than it is for one of these specific cases, like here. So if we just have the players, and we say There's something that has all the players in it.
1:30 It's easier the coordinating a bunch of variables as we'll see. So let's go over here, and we'll put this into a list.
1:37 Honestly, it doesn't matter if it's a list if it's dictionary, or whatever but it will be a little bit easier for us to figure out
1:44 who the active player is if it's a list. The reason is, we can address these by position. Zero, one, zero, one, zero, one. So if that's the case
1:55 we can come over and say active player. Now you could say active player but let's put active player index is going to be zero.
2:03 That means we can go over here to players and say active player index and it's going to print out whoever that is. It could be zero.
2:11 And when it's time to switch all we have to do is make that a one and now the player is the computer. Put it back to zero, and now it's back to us.
2:20 We could also use our modulo thing, remember that? We talked about that in one of the previous chapters the division.
2:27 So we could do something like this. This is going to be equal to that plus one mod number of players, okay?
2:36 So this is going to go from zero to one, one to two but two is divided by, and that gives us zero again so it just is a way to toggle that.
2:43 So that's kind of nice. I'm not sure it's super important here but if you had like 20 players or something
2:48 you can cycle through them super easily doing that. Okay, so here are our players. And one other thing we're going to need to keep track of
2:56 that might be worthwhile, we'll improve on this as we go but, player symbols. So what do you want to be? Xs or Os? I'm a fan of X.
3:06 I'm typing, so X it is. O goes there. So these are going to be the symbols that player_1 and player_2 play. Well, that's it.
3:15 I think that's all we had to do to, you know choose the players, well, you and computer and then choose an initial player
3:23 so we'll just have active player index that means that's zero so that's the first element in the list. Again, these all start zero, one, two, three
3:30 not one, two, three, four, as people count. So that means we are the initial player. Might as well stack the odds
3:36 in favor of us against the computer, huh?

Talk Python's Mastodon Michael Kennedy's Mastodon