Fix Bridge Transcript
[00:00:00]
INSTRUCTOR: Howdy! I’m going to walk you through the problem fixed bridge.
While I do, pay attention not only to the solution itself to the code, to the while
loops and the functions that we use, but also pay attention to the process.
Start visual description. Text on slide reads:
Fix_bridge.py
The bridge over the river has been washed out. Fix it!
End visual description.
[00:00:19]
What do I do? In what order do I do it? That is a big part of learning how to
capture the art of programming. It’s knowing how to apply the individual pieces.
So at this point you’ve learned a lot of the media, if you will, the paints and the
brushes that you can use, and you’ll see some of that in action.
[00:00:46]
But you’re also watching this video in order to learn the technique and the
artistic style and process of putting those things together into a solution. So let’s
go. Fix the bridge. Bit needs to get across the river, the bit’s been washed out,
the bridge has been washed out, and so we need to to put it back into place.
[00:01:09]
First step is figuring out the high level goals. Let’s draw it out. Come over here,
and so what we’ve got is, bit’s got to run, there’s water down here, water, okay
great, and we need to get to the red square here, red, get across to the red
square on the other side.
[00:01:32]
Bit’s starting right here. First major goal is that I see is, let’s get to the red square,
goal number one, let’s build the bridge, goal number two, and then let’s get up
and out, and to the screen that’s number three, two, number one.
Start visual description. Instructor sketches a solution for the coding problem by drawing a bridge. End visual description.
[00:01:54]
So with those high level goals in place, we can put little function stubs in to
represent those. Let’s come to PyCharm. This is the file, fixedbridge .py, that you
can find in the files folder on the files link on the main schedule on the website.
[00:02:14]
And so what were the three things we had? Get to the start of the bridge, build
the bridge, and then go to the edge of the screen. So I can say get to bridge start,
build bridge, and then keep going.
[00:02:37]
The red squigglies indicate that those words don’t exist yet, but we can make
them exist. So I can say hey, by hovering over you’ll see this little explanation pop
up for why it’s underlined red, and there’s this little blue link that says create a
function.
[00:02:55]
So create a function, highlight over this one, create the function, highlight over
this one, create a function. Now they don’t do anything right now, but at least
they’re there, and things are set up. And if we implement these smaller problems
now, then the overall problem should be correct.
Start visual description. Instructor types the following code:
@Bit.worlds(‘fix-another-bridge’, ‘fix-bridge’)
def go_fix_the_bridge (bit):
get_to_bridge_start(bit)
build_bridge (bit)
keep_going (bit)
End visual description.
[00:03:14]
So let’s get to the bridge start. And to be a little bit more clear about it, let’s
document this. So we say bit starts at the left side of the screen, and we can say
bit ends, which way is he facing? Facing right, and bit ends. Now I have to make a
decision.
[00:03:39]
Do we want bit to end on the red square, or do we want bit to end above the red
square? You can decide ultimately what you want. If he ends above the red
square, then there’s a little bit of glue code you need to get him to the red
square.
[00:03:57]
And sometimes there’s an obvious artistic reason why you would do one over the
other, sometimes it’s up to preference. And often you only figure it out just by
trying and playing with it. For our purposes right now, maybe we’ll say bit runs to
the square above and then we’ll just put in that little bit of glue code to move
him down underneath.
[00:04:19]
So to get to that position, what criteria do we have that shows us bit has arrived?
Well all of these are black, this one is red. One of the key differences between
red and black is black is not clear, but red is clear.
[00:04:35]
It’s open bit can move on to it. So we could use the right clear function to
determine when we’ve passed all of the black. So bit ends above the red, facing
right. So that documentation in place, it’s easier now to implement this little bit
of code because we can see exactly what it’s supposed to do, right there.
[00:05:06]
So we can say bit while bit dot, let’s say we wanted to write clear. Now that’s
going to, while it’s clear he’s going to move, but it’s not clear yet, we want him to
move until it is. So we’re going to use a dot to reverse that logic.
[00:05:28]
So while it’s not clear, don’t move. And now we can test this and see if that little
bit of code, pun not intended, works and does what we expect. So if that does
what we want, it should end up right here above the red square.
[00:05:48]
Right, so there’s bit right above the red square. So far so good. So let’s continue.
So if we get to bridge start, gets us above then down here we know that we’re
sitting above it before we build the bridge then we need to do a little bit of
movement and so we can say you look at this right we need to turn right move
and then turn left to reorient down the line.
Start visual description. Instructor types the following code:
def get_to_bridge_start (bit):
“””
Bit starts at the left side of the screen facing right
Bit ends above the red square facing right
“””
while not bit.right_clear():
bit.move()
End visual description.
[00:06:15]
Bit.right, bit.move, bit.left. That should be good. If you want to make sure you
run it check and see and see when the patterns that we see here is write a little
bit of code, test it, see if that’s working, then build on top of it.
[00:06:35]
So now we’re all set, bit is ready to build this bridge. So let’s move into that.
What does build the bridge mean? It starts on red square facing right, bit ends on
other red square facing right, bit paints red in the middle.
[00:07:02]
Implement. So we’re going from a red square and we’re going to end on a red
square. We also note, if you come over here, that it’s blocked on this side so you
could even do like a front clear if you wanted to.
[00:07:17]
You could try and end on the red or you could just keep going. That’s maybe we’ll
just take advantage of the fact that front is clear you know he’ll run until it’s
blocked and that’ll work.
[00:07:29]
So we can say while bit.front clear, bit.move, bit.paint red. Now in these move
and paint loops right you have to ask you paint and move or move and then paint
and over here we can see right if he’s starting on the red square that’s already
red so a move and then paint seems to work.
[00:07:55]
At the very end he’ll move and then paint that square red again. But that’s okay.
Maybe we won’t worry about that. So let’s see. Does that work? Does that do
what we want? Run fixed bridge. Test it again.
Start visual description. Instructor types the following code:
def build_bridge (bit):
“””
Bit starts on red square facing right
Bit ends on other red square facing right
Bit paints red in the middle
“””
while bit.front_clear():
bit.move()
bit.paint(‘red’)
End visual description.
[00:08:10]
Great. The bridge is built. So now all we need to do is get bit from here to the
edge of the screen. Following the pattern that we did before, let’s just use a little
bit of glue code to get them up here and then we can just run.
[00:08:24]
So that little glue code down here, if he’s facing right, then we’re going to have to
go bit.left, bit.move, bit.right. And now we just have to implement keep going
right here. It starts facing right and goes to edge of screen.
[00:08:51]
Easy. Well, bit.frontlear, bit.move, test it again. And we’ve got it. We should also
check the alternate map and see that this one is also correct. So he moves,
drops, moves over till it’s blocked, jumps up and exits.
Start visual description. Instructor types the following code:
def keep going(bit):
“””
Bit starts facing right and goes to edge of screen
“””
while bit.front_clear():
bit.move()
Instructor runs the code and a grid with a bridge painted on it is displayed.
End visual description.
[00:09:18]
There you go. So some things to note. What was the process then that I followed
that I want you to look at and try and replicate in what you do? One, looking at
the high level picture first. When you see a problem, the first thing you do should
not be, oh, what while loop do I need and what conditions do I need?
[00:09:37]
Instead think, what are the high level functions I need that when put together
solve the overall problem? Put some function stubs in and then start to
implement them. Sometimes you will have problems that are so complex that
those sub problems need to be broken down further as well.
[00:09:54]
So when you go to implement, say, get to the bridge, you might actually end up
writing additional functions, et cetera. that process can keep happening, but
that’s what you want. You don’t want to think about while loops and other little
bits of code until you’ve reached a problem that is solved simply by a little bit of
that code.
[00:10:15]
If it’s more complex than that, you should be thinking about what functions do
you need. Another key idea is that we tested, I tested as we went. so implement
get to bridge, test it, add some glue code, test it, implement the neck function,
test it.
Start visual description. Code on screen reads:
@Bit.worlds(‘fix-another-bridge’, ‘fix-bridge’)
def go_fix_the_bridge(bit):
get_to_bridge_start(bit)
bit.right()
bit.move()
bit.left()
build_bridge (bit)
bit.left()
bit.move()
bit.right()
keep_going (bit)
End visual description.
[00:10:32]
That iterative process of writing code and then testing code is really important.
You’ll use it not only through the rest of this course, but also in your career. And
that process of breaking things down and then testing those little pieces makes it
much easier to build complex programs.
[00:10:53]
It’s easier to find a bug in 10 lines of code than in 100 lines of code. So test 10
lines of code at a time instead of 100 lines of code at a time. And then following
that process, it makes it possible then to write things that don’t even fit on one
screen or even in one file.
[00:11:12]
You’re able to break it down. With that, go forth and do likewise.