Python for Absolute Beginners Transcripts
Chapter: Problem solving techniques for writing software
Lecture: Demo: Choose a location

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