Python for Absolute Beginners Transcripts
Chapter: Problem solving techniques for writing software
Lecture: Demo: Find the winner, naive version
0:00 Our tic-tac-toe is almost done we just have to figure out how to find a winner. Again, this comes back to our data structure
0:08 which is really, really helpful in putting this together. So let's sort of sketch this out here. We're going to do win by rows
0:17 win by columns, and win by diagonals. Now, what I'm going to do is I'm going to write this in the most straight forward
0:26 immediate, obvious way I can think of it solving these three problems. But what we're going to find out, is
0:32 that this is actually not the best way to do it. There's sort of a couple of things we're going to have to do that will make this a lot better.
0:40 So let's first write out what the rows are. Well, the rows, we want this as a list or a sequence we can go through that's exactly what the board is
0:49 though the board is going to be the rows like row 1, row 2, row 3 that's what the board holds. The cells, or the columns, rather
0:58 this is going to take some work. So what we need to do let's just go through them as numbers. It's going to be easier that way. So if we say for col
1:07 how do we go through just numbers? We could use a while loop but there's a cool thing we can do called range. And if we go here we can say start = 0
1:17 and then not sure it's actually going to not stop at the right spot so we can go click on this and get the quick documentation.
1:25 So it returns an object that produces a sequence of integers from start inclusive to stop exclusive. So that's a little annoying
1:32 you got to remember to put a 3 here so it goes 0, 1, 2. Do we call them 0, column 1 and column 2. And then what is one of the columns
1:43 let's call index, so we're going to create a column it's going to be a list and what goes into this list? Just like each row is a list
1:52 we want to list here. So the way we're going to get it is we're going to go row 0, row 1, row 2 so like this, this is close not quite done, but two
2:06 and then for highest middle, bottom and then here we need to know which column are we in. Are we in the first column which means it's always like 0
2:15 no column index here and here, and here. Is it always the first one in row 1, 2, and 3? Is it the second one in row, 2, or 3, and so on?
2:25 Okay, so that's going to do that. And then the diagonals the diagonals are a little bit funky as well. This is going to be an empty list
2:33 and there's just going to be, well actually, let's not make it empty we'll just do it like this. This is going to be board of 0, 0
2:41 with a forward that's called a forward diagonal. And then 1, 1, and 2, 2. Well, that one's easy but now we got to switch it.
2:50 So let's go from the front row it's going to be the end coming backwards and then here like this. Yeah, that should do it. For all of these
3:02 we're going to have to go through and then loop over them and see if there's a winner. Now, how do we know there's a winner?
3:08 There's a winner if it's the same symbol across the board, right. If there's a row of X, X, X or if there's a column of O, O, O
3:18 or there's a diagonal of O, O, O, or X, X, X. Alright, so that's going to be a little bit tricky here.
3:24 And what we need to do is loop over all of these ideas. So just do it here for the win by row first. Let's say we're looking for four each
3:37 or row in rows what we need to know is what symbol is in the first place if there's a symbol in the first place at all.
3:46 So we'll say symbol one, I guess we'll call this, I'll say, row of 0. It's going to be the first cell in there.
3:53 Now, there's a couple of things we can do we can say, if not symbol right, so that means there's nothing here, right
4:00 we don't want to win because it's None, None, None 'cause then we'll just say continue. I'm going to make this nicer in a second but if it's all that
4:07 then we can say four cell in row we're going to go across we'll say, if not cell cell not equal to see how you're saying not equal to, symbol one.
4:22 So if there's some other symbol including potentially none, then we're going to continue. And here we're going to say return True. Now, that is a mess.
4:32 Let's go down to the bottom and say, yeah, it still says returns False. We can do a lot better and I'll show you here in just a second.
4:38 But this is the most naive way of writing this. Go through each row and we're going to see if it's a winner. Now, the first symbol
4:46 got to make sure it's something, not nothing and that's what this test is and then we go through every cell
4:51 and just make sure that they're all the same. If there's any that are not the same one was an X, another is a None then we don't carry on down here
4:59 and we keep going. Whoo, okay, let's run it for the moment and just see that we can win across the board. So I'm going to play one-one
5:07 nobody's won yet, the game's not oh, Michael has won the game. Actually that would be wrong. All right, rather than figuring this out
5:16 let me just rewrite this in the way that I wanted to write it in the first place. So what we're going to do over here is we're going to replace
5:22 all of this stuff here with something better. I'm going to say if symbol that means it's not empty and we're going to do one other test.
5:32 There's a really cool way that we can write it we haven't talked a lot about it but I think you'll dig it.
5:36 So what we can do is we can use this all function and we can call all and what it's going to do is it's going to check that something is True
5:45 for every item in a collection. And then see where it says arable here? And by them we can write little tiny expressions.
5:53 This one will be called a generator expression and what we can do is we can just say something like the value for some collection.
6:02 So what we want to do is we want to say symbol is equal to cell for symbol I guess it's cell in row, like that. Let's go over symbol one. There we go.
6:15 So here what we can say is if this is not nothing and for every one in the row every cell in the row it's equal to the first one
6:25 and we're going to return true. Otherwise we're going to return, you know we're never going to hit this it's going to go down and check the others
6:32 and then check the others and then eventually return False. So if I've got everything right this time I should have this working.
6:39 Alright, 1, 1, that didn't work let's put 1, 2 for the computer and I'll put 1, 3 for me. Let's have the computer take the second row
6:50 so 2, 1, I'll do 3, 1 2, 2, we're getting close I'm going to play 3, 2. Now, the computer is going to play second row, third column
7:05 it should win right here. Boom, process is finished the game over, computer has won with this board right there.
7:12 Perfect, so now we got this check working. But I hope, hopefully you can tell this is starting to get kind of gross over here.
7:20 So we did this for the rows I haven't checked the columns yet, have we? Oh yeah, we haven't checked the diagonals yet, either. So while this works
7:30 I'm going to just sort of pause this for a minute and we're going to come back and say, well, I guess I could put this down here for you, couldn't I?
7:37 So let's just come down here and say columns.append and I'm going to pass in a column so this is going to be a list that has all the columns
7:44 and then, again, I guess the diagonals they already got that. So let's go down here and we're going to do this check again
7:51 but it's going to be col in columns and start to feel this is not great. Col zero, column, and guess what? I'm going to do the same for the diagonal.
8:06 Call it diag, I don't know what you want to call it. Now, whenever you have code that looks like this there's this bit, and then there's this bit
8:20 and there's bit, and it's the same, right it's exactly the same it's just a matter of what goes in here what goes in here
8:27 the variable doesn't really matter. It's clear that we should be doing something different. But let's just show that this is going to work.
8:35 Oh, we didn't quite build our diagonals right. This has to be that list and this has to be that list, okay.
8:43 Alright, so now we should be able to win diagonal. I guess I'll win diagonal this time. Oops, that's not going to work, is it?
8:50 And play two-two, computer can play 1, 3 alright, I'll play 3, 3 this should win the game. Boom, game over, make that a little more obvious.
9:00 But that's good, and then let's have the computer win by column. What am I going to play play that one, the computer can play 2, 2 and then, let's see
9:11 one-three, the computer will play 3, 2 and it should win. It does, allright so we've got winning the game working but here, this is a lot of a mess
9:21 and this duplication, and not a little bit this is complicated duplication this is not good so we're going to come back and fix this.
9:29 But here's our naive version of winning the game. Technically, tic-tac-toe is over but we can do better.