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:

In lab, you will complete the logic for the fish to both solve the maze and avoid the sharks!

Completing the Lab

  1. Open the Lab: Click here to open the Scratch project, as it appeared at the end of lecture.
  2. 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.
  3. 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.
  4. 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

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