Spiral Transcript
[00:00:00]
I’m going to show you how you can approach the spiral problem using some of
the things that you’ve learned recently about if and else, and or and what we call
the event stream pattern.
Start visual description. Screen reads spiral.py. As long as Bit can move forward or left, keep going! End visual description.
[00:00:14]
So in this situation, Bit’s supposed to move forward or left as long as either of
those options are available right and paint as he goes.
[00:00:23]
But we can also see that there’s certain situations Bit’s going to need to respond
to as it moves along. We can’t just say you know while front clear move, because
eventually you run into a wall. There needs to be something a little bit more than
that.
[00:00:54]
And we can see that the event that Bit needs respond to is in fact running into a
wall while there’s a left turn available to it. So under those conditions we want a
turn to happen. And so, these give us some of the pieces that we can start
breaking things down.
[00:01:16]
So first, I’m going to come over here to PyCharm and I’m just going to sketch out
in some comments some of the things that just describe right. So Bit moves until
he cannot turn left. Cannot go forward nor turn left.
[00:01:44]
When Bit runs into a wall and can turn left, turn left. Right, now this starts to give
us the actual basics of what we want right. So we’ve got this overall pattern. Bit’s
moving until some goal so we can create this outer loop that controls that and
then we can create some functions to handle some of these smaller details. So
we’ve got while should keep moving bit.
[00:02:29]
Bit dot move. Okay great. And then we also have like this question of deal with,
maybe we’ll call like this, maybe turn bit. So Bit should keep moving, move. I
guess we have a bit dot paint in there right? Bit dot paint blue. While he’s
moving. And then maybe we need to turn. And this is the overall structure now
of what we need.
[00:02:59]
Now let’s fill these two details in. So we should create a function keep moving.
And what was that criteria? Right Bit moves until he cannot go forward or turn
left. So if the front is blocked and the left is blocked we know we should not keep
moving. Well let’s break that down a little bit more right.
Start visual description. Instructor types the following code:
@Bit.worlds(‘spiral’)
def run(bit):
# Bit moves until he cannot go forward nor turn left
# When Bit runs into a wall and can turn left, turn left
while should_keep_moving(bit):
bit.move()
bit.paint(‘blue’)
maybe turn(bit)
End visual description.
[00:03:23]
Um, we know that bit dot Front Clear means he can, uh, he should keep moving.
Right.
[00:03:34]
We know that bit dot front clear means he can, he should keep moving, right? So
we could say you know if bit dot front clear, should I keep moving? Yes. Return
true. If Bit can turn left so if the front is not clear but I can turn left should I keep
moving? Sure, right? Bit dot left clear. Return true. But if neither of those are true
then no. Stop going.
[00:04:10]
This pattern here, where you see if this condition return true otherwise if this
condition return true, we can simplify that right? So if bit dot front clear or bit
dot left clear return true. Else, return false. Right if the front is clear or the left is
clear we know we should keep moving. Okay well that’s good. And when you see
a pattern like this where you have if condition return true else return false, well,
we can simplify that so this right here just evaluates to a true or a false.
[00:04:52]
So if true return true, else return false. So why don’t we just return the true or
the false that we have? Right? So we say return yes.
[00:05:03]
So if true, return true. Else return false. So why don’t we just return the true or
the false that we have right? So we say return this. Okay, so if it’s true we return
true and if this is false return false. And it’s simple. And this is the preferred form
in programming. It’s just to return the value itself as opposed to if true return
true. Alright, so that that allows us to keep moving.
[00:05:22]
If the front is clear or the left is clear Bit’s supposed to keep going. Great. So
while should keep moving. Now maybe turn. Right what happens here? When do
we know that we’ve reached a block right? So if Bit’s coming down the line at
some point, we’re going to want to make a turn and that turn happens when the
front is blocked right.
Start visual description. Instructor types the following code:
def should_keep_moving (bit):
return bit.front_clear() or bit.left_clear()
End visual description.
[00:05:47]
So we could come over here, hover the mouse over that create a function maybe
turn. Great. And so we can say you know if bit dot front clear except it’s not if the
front is clear it’s if the front is not clear, then bit dot left. So maybe Bit turns. This
function handles that logic and away things go. Let’s see if this works.
[00:06:17]
Run it. There’s a play button by if name equals main. And it’s taking a long time.
Which should give us cause for concern. Alright, we hit an error right. So Bit tried
to move but that space is blocked. He tried to run right into this block. So let’s
step back a few steps and see what was going on that caused this to happen.
Right so we’re on line five, should I keep moving right? The front clear is true so
this is going to return true and so we’ll hit line 17 while true and so line 18 bit dot
move right.
Start visual description. Instructor types the following code:
def maybe_turn(bit):
if not bit.front_clear():
bit.left()
Instructor runs the code line by line and observes the behavior of the grid.
End visual description.
[00:07:04]
So the next step is line 18 bit dot move so then we’re going to paint blue and
then we’re going to hit the maybe turn. Which checks is a front clear? Yes, so we
don’t need to turn. So true becomes false so we don’t run this so what’s the next
step? Goes to should keep moving which is going to check if the front is clear. The
front is clear so we don’t even need to check the or. Right, true or anything is
true and so the computer immediately stops checking and doesn’t even call this
function and returns a true.
[00:07:36]
And so while true yes, so we move and then we’re going to paint. And now
maybe turn. Right so while not front clear bit dot left. Front clear is false so if not
false or if true. So Bit dot left’s going to happen. There we are and this is where
hopefully we would have stopped. Or maybe we would have stopped the step
just before. Right maybe this is where we wanted.
[00:08:03]
If we come back to here we can see we wanted to stop without that turn, right.
So maybe we need to rethink just a little bit where we did our conditions. But
what happens here? So we turn and now what’s it going to ask right? Should I
keep moving? Well the front on line five is not clear. How about the left? Well the
left is clear now.
[00:08:25]
Ah, right so we see that the conditions aren’t exactly right. We may need to go
back to the drawing board and rethink this. This happens all the time in
programming where you build something and it looks right and it looks good and
you realize that eh the pieces didn’t quite add up the way we wanted them to. So
what did we see? First you know we saw that we were, we really wanted to be
done here and not have turned.
[00:08:54]
And if that had happened everything would have been fine right? And and so
that says the maybe turn logic might have been off. We could fix that. We could
come in here and say, well maybe turn we only actually want to turn if the left is
clear. So if we run into a wall and the left is clear let’s make the turn, otherwise
let’s not. So we can come in to maybe turn and say if the front is not clear and bit
dot left clear.
[00:09:31]
So if the front is blocked, but I do have a left turn option let’s turn left, otherwise
not. Then I won’t turn and then while should keep moving is going to figure out
that were all finished. Okay, let’s see if that fixes it. Oh really close! What is it
complaining about? The Bit world does not match. This first spot is supposed to
be blue. Right, that’s just the move paint problem.
[00:09:57]
You know how to fix that. Come back around here bit dot paint blue Run it. And
we’re all set! Now I invite you to go through this exercise and try and code it up
yourself without looking at the solution code. And maybe the first time through
you do need to look at the solution code to kind of formulate and see how it
works. But then delete what you have and try and do it again.
Start visual description. Instructor adds bit.paint(‘blue’) in run function and runs the code again to show the completed blue spiral on the output grid. End visual description.
[00:10:33]
And this process of learning how to reproduce other code on your own is really
powerful in helping you master the skills that you’re going to need to be a self-
sufficient programmer down the road. Have fun!