Python for Absolute Beginners Transcripts
Chapter: Problem solving techniques for writing software
Lecture: Demo: Show the board

Login or purchase this course to watch this video and the rest of the course contents.
0:00 What's next in our list, here? Show the board. Alright, well, we haven't quite done check for winner
0:06 but I'm going to give that a check mark Nonetheless. Alright, so down here, instead of just saying play around let's do a little bit more.
0:15 I'll go ahead and put the comment show the board. We need a few more things here, though. Print. We can say somethin' about who's turn it is.
0:27 Let's just go like this and say player equals remember players of active player index. We'll worry about toggling that in a second.
0:35 But let's say it's player. Here's the board. Let's do a little print. Going to do a print before that as well to give it some space
0:47 and then we want to just say show board. Now, we could do this right here but it turns out that maybe we want to show the board here
0:56 and then the final board when the game is over. It also turns out to be kind of messy, some of these things
1:02 so if we do it here it's going to make it really hard to just read what this loop is doing. In fact, we could probably do a little bit better as well
1:09 if we go down here and say def announce_turn or something like that and we could say player put announce turn, pass in the player
1:19 and put this down here as well. Basically it'll read much more like English, right? Announce the turn, show the board, get the input, so on.
1:31 Let's go down here and find another function. Notice how I'm using functions here to kind of bring some of this to life, right?
1:38 We have a function called show_board. We have a function called check for winner. We're going to have one that lets them choose the location and so on.
1:47 So, let's go and see what we can do with this board here. There's a couple of tricks. I'm going to try a couple of angles here.
1:55 So, we have the board. Remember, the board is a list of rows. We want to go through each row and show its cell. So, that's really easy.
2:01 We just say for row in board, and what are in cells or cell in each row. Well, we could just print out cell. Now, this is not going to do what we want
2:15 but let's just run it and see what happens and I'm just going to do an input here so we can take a little break. Run it and here's what we've got.
2:25 It's you's turn. I'm going to put my name in here. You can change your name in the real version. It's Michael's turn.
2:34 Here's the board. None, None, None, None, None, None, None. Yeah, that doesn't look like a board, does it? So, let's see. Let's do a little bit better.
2:42 Well, the problem here is that these are all going vertically and we want them to go horizontally. So, what we can do is we can go and say the end
2:50 is actually equally to; this is close, but not quite what we want. Put it like this. Now they're all across, okay. Maybe better.
3:00 What we need to do is say after each row we want to print out we want to actually have a new line, so let's try that. There we go.
3:09 None, None, None, None. Okay, pretty good, pretty good. Let's do one more thing. Maybe we want those columns in there before we say print like this.
3:20 Maybe give it a space and say and is nothing. We do it like that, nope. Alright, we want that at the beginning of a row up here. There we go.
3:30 That looks a little bit like tic-tac-toe. However, None is not generally what we would expect to see so let's go over here and we'll say this;
3:39 we'll say symbol equals, and we can do an if statement. We can say if cell is None, then symbol is something else, symbol equals cell.
3:56 Let's put an underscore, like that and then we're going to print out symbol, all right? There we go, that looks like more of an empty board
4:05 doesn't it? And we have Xs in there, what's it going to look like? About like that, okay. I think that's going to look good
4:11 but notice this is a lot of junk to come up with that. Now, Python has a way to condense these if statements
4:18 into one line, and when you have stuff like this actually it turns out to be really valuable. So, watch this; I can say symbol is equal to cell
4:27 if cell is not None else this. So, the stuff below is the same as the stuff above. Yes. So, what it says is the cell is going to be an X, an O, or None
4:42 so if it's an X or O, so that means it's not None we're going to show that. Otherwise, if it is None, we're going to show this. Cool, huh?
4:50 Called a ternary expression. Same output, all right? But if for some reason over here we were to hack on cell
4:57 and say it was an A, now we get a bunch of As. All right, looks like that is working. This is showing the board. I think that's decent.
5:09 I'm going to call this show the board step done.


Talk Python's Mastodon Michael Kennedy's Mastodon