Computer Science

# Lab 11 — Grids

In this lab you will practice using a two-dimensional grid. Download the lab 11 files to get started. Write your code in the file called `grid_practice.py`.

Open the terminal in PyCharm and type:

``pip install byugrid``

This will ensure you have Grid installed in your environment.

## Peeps to Reese’s

Write a function called `peeps_to_reeses(grid)`. This function takes one argument:

• `grid`: a grid with peeps, represented by `'p'`

Change every peep to a Reese’s Peanut Butter cup, but converting it to an `'r'`. You will need to loop through every grid location and use both `grid.get()` and `grid.set()`.

Reminder: This function should return the modified grid.

We have provided doctests for you so you can learn how to test whether a grid has been modified properly.

## Can Bubble Up

Suppose there is a grid where every square is either a bubble `'b'`, rock `'r'`, or empty `None`:

A bubble can be bubbled up if the location directly above it is in bounds and empty.

Write a function called `can_bubble_up(grid, x, y)`. This function that takes three parameters:

• `grid`: a grid with bubbles and rocks
• `x`, `y`: coordinates (x, y) in the grid

The function returns True if the element in this location on the grid can be bubbled up, and False otherwise.

1. First walk through each cell of the grid and write down by hand (not using code) if it can be bubbled and why. This should help you determine the cases you will need to check in the code.

2. Draw out a variety of situations and then write doctests that cover every possible case, such as if the grid location is empty or a rock.

3. Now write and test your code.

## Bubble Up

Write a function called `bubble_up(grid, x, y)`. This function takes three parameters:

• `grid`: a grid with bubbles and rocks
• `x`, `y`: coordinates (x, y) in the grid

This function takes a bubble that is known to be able to bubble up and does the bubbling. This moves the bubble up one row, replacing its former position with `None`. This function returns the modified grid.

1. Write out a plan for how to implement this function.

2. Draw out a variety of situations and then write doctests that cover every possible case.

3. Now write and test your code.

Note you will need to return the modified grid and then test that the returned grid is correct. Here is an example of a doctest:

``````>>> grid1 = Grid.build([[None], ['b']])
>>> bubble_up(grid1, 0, 1)
[['b'], [None]]``````

## Bubble Up All

This is optional, for those who want a bigger challenge.

Write a function called `bubble_up_all(grid)`. This function takes one parameter:

• `grid`: a grid with bubbles and rocks

The function iterates over the grid, from top to bottom and left to right, finding bubbles that can bubble up. It then returns a new grid with all possible bubbles moved up one row where this is possible. Note that if you run from top to bottom you may open space for a new bubble to move up that had previously been blocked by another bubble.

1. Write out a plan for how to implement this function.

2. Draw out a variety of situations and then write doctests that cover every possible case.

3. Now write and test your code.

## Lessons

What we want you to get from this lab:

• You can use a Grid to get and set values

• You can write functions that operate on grids

• You are comfortable thinking about problems in two dimensions

• You can test your functions with doctests

• You can figure out what went wrong when something unexpected happens