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