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