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