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