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 in our breakdown that we had here. Toggle active player. And because we chose such a cool way of representing this.
0:09 I think this might actually be the easiest thing we do yet. So we're going to come down here. And again if you don't make it
0:16 past choosing a location you don't get to play. But here we're going to go and we want to toggle the active player.
0:24 So there's going to be a little bit of a catch we got to be careful with here. But save the player. Active player index. We could just say += 1.
0:33 But remember that that is actually going to overrun this really quick. I go 0 then 1. And boom, it's going to crash when we try to get the thing 2.
0:41 And thing 2 doesn't exist. You want to use our modulo trick here. And say it's that + 1. And we could just go and say, % 2.
0:51 because we know there's two players. But for some reason you wanted to be more general we have these players. And the real constraint here is
0:58 that it has to fit within the number of items here. So we could actually go by the length of players. That way if there's three
1:05 it goes 0, 1, 2, 0, 1, 2 If there's 5, it goes 0, 1, 2, 3, 4. And then wraps. But right now I'll just go 0, 1, 0, 1. The remainder of dividing by 2
1:15 is either 0 or 1. Alright, let's just see that this works. And I'll put my toggle active player. Here, 1, 1. It's computer's turn.
1:26 What does computer play? They play 1, 2. Oh yeah. Michael's turn. 1, 3. Computer's going to go for the ever so awesome 2, 2.
1:36 Look at that. Toggle player. Told you is wasn't bad. Works really well. The challenge is we're going to want to go along here and
1:45 be able to print out who won the game. Yeah I guess that we can do this. If we just did one little trick up here. Like this.
1:56 We just list out who the current player is. And then we always swap it right here. We'll be able to just print this out.
2:03 Because this won't reset the player until it makes it back through the loop. Which won't happen if there is a winner.
2:08 Okay, I think that's what we're going to do. But when I said there was a little catch. This is going to flip it to the next player.
2:13 And then if there is a winner. We're going to exit out. And somehow we need to report that. So, let's go ahead and print the end of the game here.
2:21 Game over. Player has won. With the board. Like this. And here we can show the board again. Remember I told you if we wrote it out here.
2:31 That would not let us reuse it. So what we want to do is show it every round. And when the game is over also show it here.
2:39 Pretty cool. So I think this toggle active player thing. Working great.


Talk Python's Mastodon Michael Kennedy's Mastodon