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

Login or purchase this course to watch this video and the rest of the course contents.
0:00 It's time to create the board
0:01 but real quick before we do, let's just create a little bit
0:04 of a function that we're going to be able
0:05 to run here, like this.
0:09 I always like to put my code inside functions
0:12 even to help us organize it
0:13 because maybe you want to break them out later
0:15 and that turns out to be easier to do
0:16 if you have a higher level organizing function.
0:19 So, here we go board equals, well what?
0:22 What are we going to put here for the board?
0:25 This brings us back to another one of my pieces
0:28 of advice, is to think about data structures.
0:31 How might we store this information in here?
0:35 And, it really comes down to how we want
0:37 to think about the board, one way
0:39 let's think more about how we're going to use the board.
0:43 Let's say it's none for a second.
0:44 What we could do is we could say
0:46 well how do we want to get this information back?
0:48 We could go to the board and we can get thing
0:50 at one comma two or something like that.
0:54 We could maybe go over here and say we want a first row
0:59 and then for the row, we'll have the columns.
1:02 There's other ways to store it, I'm sure
1:04 but we got to think about how we're going to do this.
1:07 Now this side, over here, this bit
1:11 this probably is something like a dictionary.
1:13 We could have a bunch of different keys.
1:15 We could define a dictionary like this.
1:17 We could say board is this and
1:18 that zero comma zero position.
1:22 Well, if you wanted you could have 1, 1
1:23 if you prefer that way of thinking about it
1:26 or you could have nothing for the moment.
1:28 And then, go across, maybe let's say that this is the row
1:32 this is the column, go across the top, end like that.
1:36 And, we're going to have 1, 0. 2, 0.
1:40 This is okay, but there's a couple of challenges here.
1:43 I can't easily ask how many items are in a given row
1:48 or in a given column.
1:49 That turns out to be something that's tricky to do.
1:52 This would work but also these strings
1:55 there's really weird situations
1:57 like if there's a space here, then that's not the same
2:00 as not having a space.
2:01 I mean to humans it looks the same
2:02 but to computers, it's like, I have the string zero
2:05 comma space zero, that's not the same as
2:07 the string zero comma zero, alright?
2:09 So, it's not quite probably the best data structure.
2:13 So, that was a possibility.
2:15 I'm going to say no for that one
2:18 which means that this use case here
2:19 doesn't make a lot of sense.
2:20 So, what about this one, if we come over here
2:22 and say give me the first row
2:25 and then from the row, we can get the first column element
2:28 cell one, cell two, cell three.
2:30 Remember you always count at zero
2:31 so it's off by one for humans.
2:33 So, to me this is a little bit nicer
2:36 because then I can just ask for the first row
2:38 and say well, how many items are in the row?
2:40 Or how many rows are there at all?
2:44 How many columns, how many rows at all?
2:46 It's a little more discoverable.
2:48 So if we want to put stuff in order like that
2:52 so we can have the first row
2:55 second row, third row going down.
2:58 That turns out to be a list.
3:00 We want something ordered, we can access it
3:02 by index and we go back to those data structures.
3:06 In that world, maybe a typical board would look like this.
3:10 It would have a list of row 1, row 2, row 3.
3:16 Yeah, well what is a row but a column of three columns
3:22 basically or three cells.
3:25 We know how to say three cells.
3:26 We can access in order by index.
3:28 Well, that's cell 1, cell 2, cell 3.
3:34 What we have is a list but instead of having
3:36 scalar items like numbers or strings in there
3:38 each element in the outer list
3:41 the list of rows is going to be a list of cells.
3:45 See down here, this will be like row 2
3:50 column 1, it's not great.
3:52 Just do an underscore.
3:55 That's the first item then row 2, cell 2.
3:58 Row 2, cell 3.
4:02 And down here, same thing but it's going to be row 3.
4:05 Cell 1, cell 2, cell 3.
4:07 Does that make sense?
4:09 This is one option.
4:10 This is also an option.
4:12 There are others still, we could just have a straight
4:15 list of cell 1, cell 2, cell 3, cell 4
4:17 cell 5, cell 6, cell 7, cell 8, cell 9
4:20 and then just do the math to figure out
4:22 if you want row 2, column 2, that's the fifth thing
4:27 in the list.
4:28 I really don't like that one.
4:29 I like either of these better than that, I think.
4:32 Yeah, so what we're going to go with.
4:33 This is what I've come up with.
4:35 This looks like Tic Tac Toe, like if we put
4:38 in numbers here, let me comma this out for a sec.
4:41 Make a copy.
4:45 Let's just put in, X, X, O
4:50 X, blank, O
4:53 X, O, blank.
4:56 That looks like a Tic Tac Toe board, doesn't it?
4:58 So to me, I like this structure best.
5:00 This one works but it doesn't look like Tic Tac Toe.
5:03 The one where it's like nine in a row
5:05 also works but doesn't look like Tic Tac Toe.
5:08 This one looks like Tic Tac Toe and it lets us
5:10 really easily answer questions like how many rows are there.
5:13 Well, that's the len of board.
5:15 That's how many rows.
5:17 If you want to know how many columns there are
5:19 you just go to any row and you ask how long that is.
5:22 If you want to get something in row 2, cell 3
5:28 and where everything's off by zero
5:29 so you would say one, two like that.
5:32 That's how you get it.
5:33 So it's really easy to get over there to those items.
5:36 I think this data structure of a list containing
5:40 a list of rows where each row is a list of cells
5:44 I think that that's what we're going to go with, okay?
5:46 So we're going to comma that out again for a minute.
5:50 Leave this here for a sec.
5:53 Let's go ahead and fill this out.
5:54 Now, what we need is we need the board to start out empty.
5:59 So there's a couple of things we could do.
6:00 Remember none is the programming way of saying empty.
6:04 What we'll do is we're going to say there's a board
6:07 it has three rows like this
6:10 but we got to put three things in here as well.
6:13 Now I could just do this.
6:14 This would be totally good.
6:16 Like this and do like that.
6:19 When we print out the board, I'll have to see.
6:22 It'll show up none, none, none unless we do something
6:24 to replace none with a different character.
6:27 I think actually, I'm going to go with this.
6:29 We're going to have a little function that'll say
6:30 show the board or some idea of showing the board
6:32 and it's just going to say is there going to be an X?
6:36 Is there going to be an O?
6:38 Or if it's none, we'll just put something like
6:41 a little underscore so that there's spaces
6:43 a little character.
6:44 But it needs to start out with no plays
6:45 so this is what we're going to use for our board.
6:48 I'm going to put like, this is NVA.
6:50 Board is a list of rows.
6:54 Rows are a list of cells.
6:57 This is a data structure I'm going to start from
6:59 and you're going to see over and over again.
7:01 It makes it really easy to play Tic Tac Toe
7:03 once we know how to record the entries for each player.