Python for Absolute Beginners Transcripts
Chapter: Problem solving techniques for writing software
Lecture: Demo: Play until someone wins
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
The next thing we need to do, that's not checked off
0:02
is we have to go and play until someone wins.
0:06
So, notice I put some comments in here
0:07
create the board, choose initial player.
0:09
I wanted to put that in there to kind of highlight
0:11
what problem solving bit we were doing.
0:14
Also, I've decided to just call this symbols.
0:16
I don't want that to be super, super long
0:18
until we work with it.
0:19
So here we're going to being doing until
0:24
Well, how do we do that?
0:25
Remember, I said at the beginning
0:27
that until a condition is True or False
0:29
that's going to be a while loop
0:31
and what we have to do is put some kind of test in here
0:34
like check or find winner.
0:37
What we want to do is actually go
0:39
and look through this board
0:41
and see if there's somebody that has X, X, X
0:44
or O, O, O or more tricky X, X, X and things like that.
0:49
We're not going to do that yet, but because that's complicated
0:52
let's write a function and pass the board over there
0:56
and maybe we need to pass some more information.
1:00
For now, I think we're just going to pass the board
1:02
and we'll say yeah, there's a winner
1:03
or something like that, but it could be
1:05
that we want to pass this over so it can say
1:07
you know, you are the winner or computer's the winner.
1:09
We'll have another way to keep track of that actually.
1:11
So, we're going to find a winner.
1:13
Now, notice, this has got red curlies.
1:14
That means find winner doesn't exist yet
1:17
but it will, it will
1:19
and then we're just going to go play a round
1:21
a round like this.
1:24
So we've got to go and create this function.
1:25
Remember, your function stuff from earlier
1:28
we can go and type def, this, and do the bits
1:31
or we could just hit alt, enter in PyCharm
1:34
and say create the function.
1:36
Don't really like that it puts it at the top
1:38
but it's the safest thing it can do
1:39
but it's because the way main works
1:42
it's not necessarily you want main to be the top.
1:44
Okay, so let's just for now return false.
1:48
We are going to take a while to figure out who the winner is.
1:51
This is going to be a really elegant solution
1:54
but it's going to take a little bit of thinking through
1:57
so let's not worry about that now.
1:59
We're not at that step yet
2:01
but it definitely has uncovered the need
2:02
for us to be able to, given a board
2:04
and how it's been played
2:06
determine, has anybody won this just yet.
2:09
Before we worry about the winners
2:11
let's figure out how we might just play a round.
2:13
Now, let's go ahead and run it
2:14
and have a little bit of fun, see what happens.
2:19
Ah, yes, so I had that backwards.
2:21
It's going to do this as long as it finds a winner.
2:24
No, you play the game as long as
2:26
there's not finding a winner.
2:28
There we go.
2:29
Play a round, play a round
2:30
oh my gosh, it's an infinite loop, stop.
2:32
There we go, stop, stop, stop.
2:33
Well, that part works.
2:34
We're doing it over and over as you saw
2:37
and we're checking for a winner.
2:38
Right now we can have a to do
2:41
implement how we check for a winner.