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