Python for Absolute Beginners Transcripts
Chapter: Problem solving techniques for writing software
Lecture: Demo: Find the winner, good version
0:00 Well we had our find winner done. I mean it's technically working but remember, this loop is the same as this loop is the same as this loop
0:10 so let's think about this for a second. What we want to do is we want to come up with some lists that are just a bunch of cells and check them.
0:18 So here we're saying, the lists of cells we want to check and are the rows. And we want to go through each row and check it.
0:25 Here we say the list of cells that we want to check are the columns and then we want to go through each one of those, here and we want to go through
0:32 each one of those and check it. Here are the list of cells we want, are the diagonals. We want to go through and check it.
0:38 What we can do that's better is we can say come up with a function that says, give me all the lists of cells you need to check.
0:45 Give me the rows, give me the columns, give me the diagonals because once you have those, it's just three symbols
0:50 in a row, and it doesn't matter if they originated from thinking about a diagonal or thinking about a row or thinking about a column.
0:57 The test that you're going to do is just these three lines so let's make this a lot better. Let's say, def get winning, winnable, winning, sequences
1:07 from the board. So what we're going to do is we're going to come up with a list that isn't just the list the rows
1:14 or just the list of diagonals, but it's the rows the columns, and the diagonals so let's come over here again.
1:20 I want to go get the rows, take that away. I'll even take the little comment bit here 'cause that's still relevant. We want to have the columns.
1:35 And we want to have the diagonals. Go back over, okay. I'll say sequences is empty and then instead of just saying create separate columns
1:51 we're just going to say when we find a column put it into the sequences you care about. When we get the diagonals, put them in there
1:57 and now if you have a list and you want to put all the items in there, you can say this and say instead of append which is put one item, say extend
2:04 which means take all the items in the other sequence and put 'em one at a time in here. So we want that there and we also want that here like this.
2:12 Extend rows. Alright, we could just in line this but I think it makes a little more obvious what's happening.
2:18 So the sequence gets all the rows, all the columns and all the diagonals and we just return the sequence.
2:24 Now we don't need to have three loops over here. And we just say, sequence is equals find what'd we call it? Get winning sequences for the board
2:37 put those there call this cells, cells, cells. And tighten that up a little bit. Look how much nicer this is. That was yucky.
2:48 And thinking about this, this makes it really easy to understand when we're over here, what are we doing?
2:53 We're saying, there's different ways you can win. You can win by filling up a row. You can win by filling up a column
2:59 and you can win by filling up a diagonal. But as far as checking those, it doesn't matter. Just if any of those are filled up, we're good.
3:05 So we can break that into two parts and make it much easier to reason and maintain both of them. See, this isn't too bad anymore.
3:13 I guess we should check. Play a few games to make sure this works. I'm going to try to win by the diagonal. And it's easy 'cause I get the first pick.
3:24 Alright, so 3, 3 this should be the end of the game. Boom, Michael wins, game over. Let's try another where the computer wins in a column
3:32 and we'll call it good if that works. Alright, getting close. Every one. All right, I know if I fill up that two, two one
3:47 I'm going to be good but the computer's going to beat me with a 3, 2. Boom, game over. Isn't that cool?
3:54 Isn't that a super slick way that this comes out? The realization that all we have to do is verify a bunch of different sequences of cells
4:03 or of symbols, makes this so nice. So, here's three, so we add three rows and those are just three lists each of which has the cells to verify.
4:11 Here we have to come up with the columns. Kind of going against the grain of our data structure but not badly.
4:16 And we put the three in there, and we put the two diagonals. Then we just go to this and say, you know here's a bunch of different potential ways
4:23 somebody could of won, check them all using this common algorithm. Are they all the same symbol? Done. So this, I think, is really nice way
4:32 to cap off this project. We thought about the most simple thing we could do we broke it up using divide and conquer we started working on them.
4:41 The data structure was important. A lot of the stuff was pretty straightforward. This one though, maybe we didn't have a great way
4:47 on the find the winner. We just said, well we're going to do something gross and just replicate, duplicate a bunch of code
4:53 make it really just hard to read. But let's get it working. And then we came to this realization like
4:59 actually, this is a lot better and this is what I was getting at with the get started. I didn't have this idea when I first created this thing.
5:06 You know, I haven't technically gone through it before I hit record, but the first time I did go through it I didn't have the idea that it's just
5:12 it doesn't matter when you're verifying it if it came from a row, column, or a diagonal. I just said, well there's these cases I got to check
5:19 and then I realize, this could be put together much, much, nicer. So you don't have to have all the answers
5:24 or the slickest way to do something to get started. You build it out and as you build it you get better visibility into it, more insight
5:32 and it comes up with what I think is a really nice version of the game here. Let me do two super quick things.
5:38 At the beginning let's just say print out something. Welcome to Tic-Tac-Toe from Talk Python. There we go, and then down here
5:49 I'm not a huge fan of this. Let's do a little print and let's just do game over or something like that.
5:59 'Cause I want to make sure that this is a little more obvious. Right now, it's not entirely clear that the game is over.
6:05 I'll do one more print at the end. There we go. I think our game is done.