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

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Here we are on our next step
0:01 in our breakdown that we had here.
0:04 Toggle active player.
0:05 And because we chose such a
0:07 cool way of representing this.
0:08 I think this might actually be the
0:10 easiest thing we do yet.
0:12 So we're going to come down here.
0:14 And again if you don't make it
0:15 past choosing a location you don't get to play.
0:18 But here we're going to go and
0:20 we want to toggle the active player.
0:23 So there's going to be a little bit of
0:24 a catch we got to be careful with here.
0:26 But save the player.
0:28 Active player index.
0:29 We could just say += 1.
0:32 But remember that that is actually
0:35 going to overrun this really quick.
0:36 I go 0 then 1.
0:37 And boom, it's going to crash when
0:38 we try to get the thing 2.
0:40 And thing 2 doesn't exist.
0:42 You want to use our modulo trick here.
0:45 And say it's that + 1.
0:48 And we could just go and say, % 2.
0:50 because we know there's two players.
0:52 But for some reason you wanted to be
0:53 more general we have these players.
0:55 And the real constraint here is
0:57 that it has to fit within the
0:58 number of items here.
0:59 So we could actually go
1:01 by the length of players.
1:03 That way if there's three
1:04 it goes 0, 1, 2, 0, 1, 2
1:06 If there's 5, it goes 0, 1, 2, 3, 4.
1:07 And then wraps.
1:09 But right now I'll just go 0, 1, 0, 1.
1:11 The remainder of dividing by 2
1:14 is either 0 or 1.
1:16 Alright, let's just see that this works.
1:18 And I'll put my toggle active player.
1:22 Here, 1, 1.
1:23 It's computer's turn.
1:25 What does computer play? They play 1, 2.
1:27 Oh yeah. Michael's turn. 1, 3.
1:30 Computer's going to go for the
1:32 ever so awesome 2, 2.
1:35 Look at that. Toggle player.
1:36 Told you is wasn't bad.
1:37 Works really well.
1:38 The challenge is we're going to
1:40 want to go along here and
1:44 be able to print out who won the game.
1:48 Yeah I guess that we can do this.
1:50 If we just did one little trick up here.
1:54 Like this.
1:55 We just list out who the current player is.
1:58 And then we always swap it right here.
2:00 We'll be able to just print this out.
2:02 Because this won't reset the player
2:04 until it makes it back through the loop.
2:05 Which won't happen if there is a winner.
2:07 Okay, I think that's what we're going to do.
2:09 But when I said there was a little catch.
2:10 This is going to flip it to the next player.
2:12 And then if there is a winner.
2:13 We're going to exit out.
2:14 And somehow we need to report that.
2:16 So, let's go ahead and
2:18 print the end of the game here.
2:20 Game over. Player has won.
2:24 With the board. Like this.
2:26 And here we can show the board again.
2:28 Remember I told you if we wrote it out here.
2:30 That would not let us reuse it.
2:32 So what we want to do is show it every round.
2:34 And when the game is over
2:36 also show it here.
2:38 Pretty cool. So I think this toggle active player thing.
2:41 Working great.