Lab 10: Cell and Grid Classes for 2D Simulations

Due 11:59pm Tuesday night, April 19

Run update21b which will create the cs21b/labs/10 directory. Move down to this directory and edit the file to implement your solution. You will be defining two new classes within this file.


For this assignment, you will write two classes, called Cell and Grid, that are designed to be used by programs that implement time-step simulations of some phenomena on a simplified two-dimensional world. Examples of such simulations include modeling heat dissipation over a sheet of metal, an insect infestation on a crop of wheat, or climate modeling. In these type of simulations, the world is broken up into a grid of cells, where each cell summarizes the information for its small part of the world. At each discrete time step, a cell's value changes based on some function that involves its neighboring cells' values. So for example, in the insect model, if one cell is infested, it is likely that in a few time steps it's neighboring cells will also become infested.

In the next lab assignment, you will use the Cell and Grid classes to implement a 2D simulation program. In this lab, you will use the graphics library to draw the world represented by a Grid object that contains a list of lists of Cell objects. For our implementation a Cell can have one of two values (True or False) and you will color cells differently based on their current value. Once you have the Cell and Grid classes implemented you will write a program that uses mouse clicks to draw a pixelated picture within a grid.

1. Implement the Cell class

Create this class in the file. The Cell class represents 1 square unit in a 2D grid in the Graphics window. A Cell contains the following data:

A Cell has the following methods:

2. Test the Cell class

Once you have written your Cell class, test it with the following main program:

def main():
    w = GraphWin("Cells", 500, 500)
    w.setCoords(0, 0, 5, 5)
    c1 = Cell(0, 0, True)
    c2 = Cell(0, 4, True)
    c3 = Cell(2, 2, False)
This program creates a graphics window and then resets the coordinates of the window so that the lower left corner is (0,0) and the upper right corner is at (5, 5). By doing this, we have modified the scale of what will be drawn. A rectangle that is 1 unit wide will take up a fifth of the graphics window.

This program should draw three cells, two are that "on" (they store True) and one that is "off" (it stores False). The two "on" cells will be at the lower left and upper left corners of the window. The one "off" cell will be at the center of the window.

3. Implement the Grid class

Create this class in the same file, The Grid class is used to represent the 2D simulated world. It contains the following data:

The Grid has the following methods:

4. Test the Grid class

Modify the main in your file so that it will:

  1. Create a Grid object.
  2. Use a for loop to draw a line in the graphics window using the setNextValue method to designate the next values of appropriate cells to True
  3. Use the grid's update method to change the values and the colors of the cells.

For example, here is a 10 by 10 grid with a horizontal line:

Once you are convinced that your classes are working correctly you can move on to the next step.

5. Getting key presses from the Graphics Window

Now we'd like to test the Grid and Cell classes by allowing the user of our program to draw a picture on the grid by clicking on cells to change their colors. We need a way for the user to be able to indicate that they are done with their picture. We can't use a mouse click because we will be using mouse clicks for drawing. It would be nice, for example, if they could press the 'q' key on the keyboard to signal that they are ready to quit.

Recall that their are two ways to get mouse clicks from within a graphics window:

There are analagous methods for key presses:

Create a little test program called, as shown below, to try out using the checkKey() method:

from graphics import *

def main():
    w = GraphWin("Testing key presses", 500, 500)
    intro = Text(Point(250,250), "Press any key, or q to quit")
    result = Text(Point(250,275), "")
    while True:
        key = w.checkKey()
        if key != None:
            result.setText("you pressed %s" % key)
        if key == 'q':

Once you understand how this works, you can move on to incorporating the use of the checkKey() method into your final main program.

6. Drawing pictures with mouse clicks

The final version of your main program should use a while loop to continually:

For example here is a 15 by 15 grid that was drawn on with mouse clicks:


Once you are satisfied with your program, hand it in by typing handin21b in a terminal window.