Introduction
In lecture on Wednesday, Wade introduced the lab assignment for this week: you will complete the logic
for the fish to successfully transverse the undersea maze. In lecture, you learned that
we have already defined a move
function that will move the fish.
As part of lecture, we began working towards a solution:
-
You wrote a
moveForward
function that moves your fish forward based on the
direction the fish is facing.
-
You wrote a
turnLeft
function that turns the fish left by 90 degrees.
-
You learned that randomly turning and moving resulted in a very poor solution that
would take nearly forever for the fish to finish.
In lab, you will complete the logic for the fish to both solve the maze and avoid the
sharks!
Completing the Lab
-
Open the Lab: Click here to open the
Scratch project, as it appeared at the end of lecture.
-
Create a turnRight function: Use the "More Blocks" tab to create a new function called
turnRight
.
Add code to the function definition to make the fish turn right by 90 degrees when turnRight
is called.
-
Think about solving a maze:
-
Image the fish always keeps its right "hand" (technically a fin, but we will pretend the fish has hands) on
the wall.
-
If you do this (and assume the sharks are a wall), you should image the fish would take a path as seen
by this diagram here.
-
By following the wall, the fish exactly follows the following rules:
-
If there is no longer a wall on my right, the wall must have gone down. In order to keep following the wall,
I must turn right and then move forward.
-
If there still is a wall to my right, I must go forward to keep holding on to the wall.
-
If there is a wall to my right and a wall in front of me, the only path not going backwards is to go left from by
original position.
-
If there's a wall in every direction except backwards, I got myself stuck and should just turn around.
-
In more technical terms:
-
Turn right and try to move forward.
-
If you hit a wall when trying to move forward after turning right, turn left so you are facing forward again and
try to move forward.
-
If you hit a wall again, turn left again so you are facing left and try to move forward.
-
If you hit a wall a third time, turn left again so you are facing backwards from your original spot and move forward.
This is coming back to where you just came from and there shouldn't be a wall!
-
You can tell if this fish hit a wall by checking the
hit_wall
variable. If the variable has a value of
yes
then the fish hit a wall the last time move
was called. If the variable has a value of
no
then the fish did not hit a wall the last time move
was called.
-
Complete the program: Using what you know about Scratch, complete the logic described above inside of Scratch to allow for the
fish to reach the treasure!
-
You should only need a series of calls to
move
, turnLeft
, and turnRight
along side some
conditionals inside of the forever block.
Extra Credit: +1 Problem
-
Even though your fish thinks that the shark was a wall, it certainly is not a wall! In fact, the shark
thinks its about to have dinner.
-
For +1, ensure that your fish never touches a shark! You may need to use a "Sensing" tool inside
of Scratch to sense how close you are getting to the shark.
Submission
To submit your lab, you must download the project to your Desktop and upload it to the CS 105 website.
Click here to upload your Scratch file to the
CS 105 website