CSci 150: Foundations of computer science I
Home Syllabus Assignments Tests

Assignment 9: Mushrooms

Due: 8:00am, Thursday, March 11. Value: 30 pts. Submit to Sauron.

Background

Imagine a field in which some mushrooms are growing. We model the field as a grid of squares, where each grid square can potentially hold a mushroom, and each square potentially goes through the following cycle.

Except for a few random squares that start with spores, each grid square starts out empty. The empty square becomes a spore with 50% probability if one or more of its four neighbors are mature. Once it becomes a spore, it remains a spore with 20% probability in each following cycle, until it becomes mature. It spends one simulation cycle in the mature state, then dies for the next cycle, then decays. Finally, after one cycle of decay, the grid square reverts back to being empty.

The following illustrates the first three steps of one simulation.

Initial state: All squares are empty except for a few random squares with spores. The black border squares are walls; they do not change over time.
The user clicks the mouse to advance the simulation by one time step. In this case, some of the spores will mature into mushrooms.
After two steps: Some grid squares become spores due to a mushroom in an adjacent square. The mushrooms die, while some spores from the previous step mature into mushrooms.

Your job

I've written starting code, which sets up the initial grid and random spores. Your job is to modify this to simulate the field of mushrooms as described above. Represent the state of each grid square with the colors shown in the above diagram: green, pink, red, dark red, dark green. The boundaries of the grid, where mushrooms can never grow, are represented with black squares.

The starting program defines two functions get_grid and set_grid that you will find useful for determining and altering the color of a grid square.

get_grid(grid, index)

Returns the color name associated with the square numbered index in grid. The grid squares are numbered in reading order: Square 0 is the upper left corner, 1 is the square to its right, 2 the next square to the right; once we reach the row's end (index 14 if grid_size is 15), the following square (index 15) is the leftmost square of the next row.

set_grid(grid, index, color)

Alters the color for the square numbered index in grid to be as named in the color parameter. The grid squares are numbered as with the get_grid function.

Important notes