BYU logo Computer Science

Blue Ocean Transcript

[00:00:00] INSTRUCTOR: Howdy! Let’s solve the blue ocean puzzle. So it’s simple. Bit is in an empty grid and you’re supposed to fill the whole grid with blue. Now there are a couple strategies we could think of in trying to do this.

Start visual description. Text on slide reads:
Blue_ocean.py
Fill the world with blue.
An example of the grid being completely painted in blue is shown.
End visual description.

[00:00:18] You could say do a zigzag where bit moves down and up and back. Here is our last time. So as we’ve got this grid you can think bit comes down and moves up and moves down and moves up and back and forth.

[00:00:39] And then at some point there’s a border that you’re going to run into and know to stop. Maybe you come down and then you know he’s not going to turn because it’s blocked. So there’s an option, something like that.

[00:00:55] You could also imagine maybe like a spiral. You go down and up and over and down and just keep working your way in until eventually it’s all blue. And as a human it would be easy for us if you were given a bucket of paint in a big room.

[00:01:17] To follow one of these two strategies. It saves on space. You never did the same space twice and it’s easy for us to keep track of our orientation and know what to do next. But if we were to start to think about how bit would do this, we run into some trouble.

[00:01:34] Bit doesn’t know which way he’s facing. There’s no is facing north or is facing south or up or down or anything like that. And so at some point when bit is in the middle somewhere of this room, how does bit know when he’s facing a wall that he’s facing the right wall versus facing the left wall and he’s supposed to turn left versus turn right.

[00:02:00] Or if you’re zigzagging around, you know, it’s a lot trickier to know when does bit stop, which way is he going to turn, and it can get kind of complicated. Once bit has gone in the blue world sense. if bit’s moving down, comes up and around, at some point he’s going to walk all the way down into the blue and now you’re going to have to turn around and move back up.

[00:02:26] And so if you start to scope out a possible solution just on paper, no code yet, and start to think through the steps, okay, what would this look like in code? What kinds of pieces do I have?

[00:02:41] You can sometimes catch potential solutions that would be a little tricky to do, and it’s better to catch it on the paper stage than after you’ve gotten into the code and then you realize, oh, this is, this is hard.

[00:02:54] Is there another way? And you step back to the paper and think about it. Here’s another way to think about this problem as well, but a little less obvious sometimes to people because time is so important to us.

[00:03:07] But here we’ve got bit sitting down at the bottom of the grid. And what if we had a verb that said, fill a column with blue and come back. So bit fills this with blue and comes back right where he is.

[00:03:22] Then we can simply advance one step and do it again, and advance one step and do it again. Now this little pattern here looks an awful lot like the move paint pattern that we’ve seen and know and love.

[00:03:37] It’s just that instead of move paint, it’s move paint column. We can make that word up and define it in a minute. And with that basic idea, then this whole problem turns into just the simple move bit to the end of the row painting as you go.

[00:03:53] Now that seems to have some promise. It seems simple and you’re going to go with it because you’re watching my video. So let’s go to the code and kind of sketch that idea out and see what that would look like.

Start visual description. Instructor sketches different paths to solve the coding problem. End visual description.

[00:04:08] So here we’ve got the Blue Ocean script. Again, you can download that using the files link on the course schedule associated with the lecture. And we want to implement this strategy of moving bit along, filling each column one at a time.

[00:04:29] So the move paint pattern. While bit front clear, bit.move. And now instead of paint, we want this other thing. We want paint column. And now bit will just paint each column. And as long as we do that, you’ll fill everything up.

[00:05:00] Another question is what does this mean. So we can pull up our little menu here, have it auto generate the function stub for us, and now we want to go implement this. And what does the paint column process mean?

[00:05:13] Well, which way is bit facing when we start? Here we’re showing bit facing right. We want the column to the left to be painted, and at the end of the day we want bit back down right where he started.

[00:05:26] So let’s document that. Bit starts facing right, bit ends at the same position facing right, bit paints until blocked, the column to the left bit. So now that we see what we’re trying to implement, it’s easier to write the code and get the actual code correct, what we were intending to do.

[00:06:05] So with bits facing right, we want to fill the column to the left, we know we’re going to have to turn left, and then instead of writing all the code right here, let’s again break this down right, we can say go blue bit. Turn around, bit, go, bit.

[00:06:28] Assuming that go blue is going to be paint until blocked, turn around, go, move until blocked. You could call it that I guess, and then at the end bit’s going to be facing down, so it’s going to run up, run back, it’s going to be facing like this, and so we need to do a left turn to have them reoriented the way we had agreed here, so we do bit.left, and now we’ve painted this column.

[00:06:58] Now we’ve got to go implement these. so I can say unresolved, create that function, go blue, and here we were just saying bit paints blue until blocked in front, so while bit.frontclear, we want to say bit.move, bit.paint blue, and we know that the first one we want to get painted, so paint that as well.

Start visual description. Instructor types the following code:
def paint_column (bit):
“””
Bit starts facing right.
Bit ends at the same position, facing right.
Bit paints until blocked the column to the left of Bit.
“””
bit.left()
go_blue (bit)
turn around(bit) go(bit)
bit.left()
End visual description.

[00:07:32] Alright, now before we get too much further, let’s just come down here and stub these out. and let’s just see where that gets us. In fact, we can even go so far as just to throw in a little snapshot right here, bit.snapshot, finished, go blue.

Start visual description. Instructor types the following code:
def go_blue (bit):
"""Bit paints blue until blocked in front"""
bit.paint(‘blue’)
while bit.front_clear():
bit.move()
bit.paint(“blue”)
End visual description.

[00:07:56] And that snapshot will allow us to jump just to this point in time and see if this much of the problem is working. We want to test as we go. So we run this. Very slowly.

[00:08:29] We got stuck in an infinite loop. That’s when it took so long. But here’s what we want. This is where we put the little snapshot in. I can jump to the first snapshot and we can see. What is that?

[00:08:40] Is bit where we thought you would be after the first go blue? Yes. Why did everything else go wrong? What happened there? Well, bit finishes this, and then he turns left.down here, right, these don’t do anything, so he turns left, and then the front’s clear, so he moves, and then he turns left, and then he goes right, and so there’s these side effects of things that keep happening.

[00:09:01] And we can see, you know, where this is going. and so we can jump, and move, and jump, and right, and we can see he’s just going to go in this little circle right now. But the key point here is that first little bit was correct.

[00:09:17] Let’s go add to it, so we can see how it builds up. Alright, so now we’ve got blues correct, let’s implement turn around. Bit.left, bit.left, we can turn around now. Let’s implement go while we’re at it.

[00:09:35] Turn around is pretty simple. So go is just a simple, bit moves until blocked. Alright, so with that we would say while bit.front clear, bit.move. Great, now with those actually implemented. paint column should now do the right thing, so maybe we’ll take this snapshot off.

[00:10:04] We’ll come down here and say finished column. Now let’s jump to that first snapshot, and let’s see if we’ve painted the first column correctly. So do that, let’s run. Hopefully also we’ve fixed our infinite loop. Which we have. We’ve painted all these columns.

[00:10:28] If we jump to the very first spot we can see the first columns painted, and we can jump and see each column as it gets finished. Awesome, why isn’t the first one painted though? Well, what’s the very very first thing that happens?

[00:10:46] We check if the front is clear, and then if it is, we move. We want to paint before we make that first move. And we’ve seen this pattern before, where we have to like, bit that paint, then we move paint.

[00:10:59] But in this case we’re not painting, we’re paint columning, and so let’s just copy that and say, hey, paint the column, and now, if the front’s clear, move and paint another column, and proceed.

Start visual description. Instructor types the following code:
@Bit.empty_world(6, 6) def run(bit):
paint_column (bit)
while bit.front_clear():
bit.move()
paint_column (bit)
End visual description.

[00:11:12] Let’s run that. There you go. Now it’s working. So a couple, in summary, a couple things to think about as we look at this problem. One, how are you going to break it down? Some ways to break things down might seem intuitive to us at first, as we approach it in a way that would be native to a human.

Start visual description. Instructor runs code and sees that it is running properly with the entire grid being painted in blue. End visual description.

[00:11:43] But if you start to sketch it out and actually think about that process here and think about what you’re going to tell bit in order to navigate individual pieces of the puzzle, you can catch certain designs that aren’t going to work well while you’re still at the design stage and just working on paper.

[00:12:03] I recommend that you practice that process and get good at it and learn how to express an idea, an algorithmic idea on paper and think about it on paper so that you can try and vet out multiple different options before you get into the code itself.

[00:12:25] And sometimes you get into code and you realize this is a dead end. This isn’t the best way to try and solve this. And so you delete that code and you go back to the drawing board and draw something that’s out and try that.

[00:12:36] That’s okay. That’s a real process. What we see also in this particular puzzle is the power that comes from returning to a known state. Having bit down against the bottom, facing that same direction, doing something and then returning there makes it easier for us to think about repeating that process because it always comes back to the baseline, if you will.

[00:13:10] Then you’ll see that pattern come up over and over and over in bit and then in code in general. This idea that I will have something I need to do, I’ll do it and then I’ll come back to a known stable position so that I’m ready to do it again and then do it again and do it again.

[00:13:28] If the code takes you, if your strategy takes you all over the map, then it’s going to feel difficult to understand and difficult to maintain and fix, let alone write in the first place.

[00:13:43] And so look for patterns that allow you to address a subproblem and then return to a known position that you can work from again. With that, thanks for watching.