Python for Absolute Beginners Transcripts
Chapter: Problem solving techniques for writing software
Lecture: Demo: Choose a location
0:00 Show the board. It's got its check mark, it's done. The next thing to do is let the active player choose a location
0:07 and so, down here, we're going to something like choose location or play or something like that. And what do we need to pass in?
0:17 Well, it's definitely going to need the board because we need to know what locations are available. Does it need to know the player?
0:24 Probably not, but it does need to know the symbol 'cause when you pick a location it has to put an X or an O into that location so let's come over here
0:33 and say symbol is going to be symbols of active player index. Remember, Michael has X, computer has O's
0:42 so we're going to pass the symbol to be played here. Probably is good, but let's say let's do a quick test because what can happen here
0:51 is I could try to play a space that computer's already played or even that I've already played so we'll say if not, we can do something
0:58 like print that isn't an option. Try again. And there's a really cool flow here for this to work. We can just go back
1:08 and say you know what, just run through this again. Say it's Michael's turn again, show them the board and we can do that by just using the continue.
1:15 Remember, continue just ignores all the stuff below and then, goes back the loop. We didn't do anything. It should still be in a winner.
1:22 The player didn't change so it should still be the same players. The board didn't change. It's still the same board, so this is a really nice way
1:28 for us to try again if they get it wrong and you know they're going to get it wrong, right? We have definition of our choose location.
1:36 All right, we're going to have a couple of options here. Let's say we'll start like this. We'll say row equals input, choose which row
1:45 and now, remember this is going to come back as a text but we want it to be an integer and column. Column, all right, so that's going to be the row
1:57 and the column, so the cell that they want to play is going to be board of row of column, right?
2:04 Almost, almost, they might not choose the right value here. They might put 7. It can't play row 7, but more likely they do choose the right one
2:15 but they choose one, one, meaning the top left and that's actually the middle, right? Because Python, in the programming languages are zero-based.
2:23 Humans are one-based. Those do a couple things. Here, we'll say row -= 1 column -= 1. Then, we can test. We can say if zero row is less than zero
2:36 or row is greater than or equal to, let's say greater than 2 and we could actually do, yeah, this is fine.
2:44 If it's greater than 2, we're going to return false same thing for column. Now, maybe you wanted to generalize this a little bit more
2:54 so you could have larger boards like a five by five. I wouldn't want to play that but we could do this by saying this is less than
3:01 or equal to the length of the board. So that would be the number of rows, remember? Row, row, row is in the board
3:09 and this could be the length of the board of zero the number of cells in the first one and that's got to be equal area.
3:16 Okay, but if that's all okay, we now get the cell. They should be able to play this position if it's not empty, so we say if cell is not none.
3:27 Let's just say if it, yeah, if it's not none that means there's already this thing there. We're going to return false.
3:32 You can't play because it's already been played. All of that, we've done the conversions. We've done the checks that the numbers are okay.
3:40 We've gotten the cell. We verified the cell is not already played. Well, now, it easy. We just say that has the symbol in it, return true.
3:49 Everything worked out great for us. Alright, well, we should be able to go and play this here. Give it a shot. All right, it's Michael's turn.
3:58 By the way, it's always my turn 'cause we haven't gotten to the change your stuff here so I want to play row 1 and column 2.
4:08 That should be that position right there and it should put an X 'cause that's my symbol. Boom, look that, how awesome.
4:14 Guess what? This is going to be an easy one to win. It's my turn; row 1, column 3, row 2. Two, that's in the middle and let's do row 2, column 1.
4:25 Boom, I won by the diagonal. That computer's got nothing on me. It's cool, right? Very, very cool how this is building up
4:32 and I think it's pretty straightforward. Why is it so easy to make this work? 'Cause we chose the right data structure.
4:40 It's hugely important to have the right one. Now, remember I said just get started. That doesn't mean we couldn't have made
4:46 the other one to work, but if you're going through them and finding challenges you're like Well, maybe there's a better way.
4:51 You're also going to see in this next step that these data structures turned out to be really important as well.
4:56 Alright, well, I'm going to give this a little mark there. Actually, we've got one more thing to test.
5:02 Don't forget to test your error handling, so one, one and let's put seven, seven. That isn't an option, try again. Or if I say one, one again
5:11 still shouldn't be an option, right? That isn't an option, try again. And now, how 'about one, two. Done, nailed it.
5:19 You can't see it swapping players, but that'll happen soon. All right, even our little bit of error handling
5:24 we put down here in choose location is all done. Hopefully, you can feel how this is working, right? We've got functions that are almost the name
5:34 of the initial steps that we had. I guess I could go ahead. I could put choose location. Do you know what? These are already doing that for us, right?
5:43 So we don't really need to.