BYU logo Computer Science

To start this assignment, download this zip file.

The following guide pages cover material needed for this assignment:

Lab 1c — While

Preparation

5 minutes

Download the zip file for this lab, located above. This zip file has worlds that you will use for Bit. Extract the files and put them in your cs110 directory in a folder called lab1c.

Exercise 1

5 minutes

(a) Can you figure out what this code does?

On a piece of scratch paper, draw out what you think this code will do:

from byubit import Bit


def green_line(bit):
    while bit.can_move_front():
        bit.move()
        bit.paint('green')


def blue_line(bit):
    while bit.can_move_front():
        bit.paint('blue')
        bit.move()


@Bit.empty_world(5, 3)
def go(bit):
    green_line(bit)
    bit.turn_left()
    blue_line(bit)


if __name__ == '__main__':
    go(Bit.new_bit)

(b) Run the code

Inside of lab1c, create a file called exercise1.py. Copy and paste the above code into this file.

Then run your code.

Exercise 2

5 minutes

(a) Can you figure out what this code does?

On a piece of scratch paper, draw out what you think this code will do:

from byubit import Bit


@Bit.worlds('red-spot')
def go(bit):
    while not bit.is_on_red():
        bit.move()

    bit.turn_left()
    bit.move()
    bit.turn_left()

    while bit.can_move_front():
        bit.move()
        bit.paint('blue')


if __name__ == '__main__':
    go(Bit.new_bit)

This code uses the following world:

A 5 x 3 grid with a red square in the middle row, one square from the right

(b) Run the code

Inside of lab1c, create a file called exercise2.py. Copy and paste the above code into this file.

Then run your code.

Be sure that the worlds directory is also in your lab1c folder. You should have gotten this from the downloaded zip file.

Exercise 3

5 minutes

Inside of lab1c, create a file called exercise3.py. Copy and paste the following code into this file.

from byubit import Bit


@Bit.empty_world(5, 3)
def go(bit):
    pass


if __name__ == '__main__':
    go(Bit.new_bit)

Using this code, create this image:

5 by 3 world with 4 red squares and one blue square in the middle row

Now delete that code and create this image:

5 by 3 world with one red, one green, and three blue squares in the bottom row

Exercise 4

10 minutes

Inside of lab1c, create a file called exercise4.py. Copy and paste the following code into this file.

from byubit import Bit


def move_while_clear(bit):
    """ Moves while front is clear. Paints green as it goes. """
    pass


def move_while_not_red(bit):
    """ Moves while current square is not red. Paints green as it goes. Does not paint the red square green. """
    pass


@Bit.worlds('maze')
def solve_maze(bit):
    pass


if __name__ == "__main__":
    solve_maze(Bit.new_bit)

This code uses the following world:

a maze of black squares with a red square as the final destination

Your goal is to have Bit navigate the maze and make it to the red square:

a maze with the correct path painted in green, bit stopped on the red square

While Bit moves, it paints a green path.

To help you with the decomposition, we have given you two functions that you can complete. The solve_maze() function should call these two functions, with some glue code.

Note

It is often helpful to break problems into smaller pieces. Try this:

a) Fill out move_while_clear() first, and call it once from inside solve_maze(). You should be able to paint green while running into the wall.

b) Now call move_while_clear() multiple times inside of solve_maze() with some glue code. Get Bit to the last turn.

c) Finally, fill out move_while_not_red() and call it inside of solve_maze(). This should finish the problem.

Exercise 5

10 minutes

Inside of lab1c, create a file called exercise5.py. Copy and paste the following code into this file.

from byubit import Bit


@Bit.worlds('dive-for-treasure', 'dive-for-deep-treasure')
def dive(bit):
    pass


if __name__ == "__main__":
    dive(Bit.new_bit)

Bit starts in a world that looks like this:

black squares making a cliff with treasure, a red square, at the bottom of the ocean, made up by blue squares, bit at the top left

This represents a cliff, the ocean, and a treasure (red square) at the bottom of the ocean.

First get Bit to the ocean, then dive to the red treasure and take it (leaving blue ocean behind). Your ending world should look like this:

same world, with bit where the red square was and the red square painted blue

Be sure to write several helper functions instead of putting all of your code in dive(). The dive() function should just call your helper functions, with any glue code that is needed.

After you solve this problem, you will notice there is a second world, called dive-for-deep-treasure. You should click the tab for the second world and be sure your solution works for that world too.

Extras

If there is extra time in the lab section, the TA may help you answer these questions:

  • When a while loop finishes, what line of code is run next?
  • What happens when you put one while loop inside of another while loop?
  • Using nested while loops can get complicated. How can functions help with that?
  • How are nested loops different than sequential loops?
# Sequential:
while bit.can_move_front():
	bit.move()
bit.turn_right()
while not bit.is_on_red():
	bit.move()

Grading

To finish this lab and receive a grade, take the canvas quiz.

We are providing a solution so you can check your work. Please look at this after you complete the assignment. 😊