Lab 10: Cell and Grid Classes for 2D Simulations

Due 11:59pm Tuesday night, April 13

You may work with one partner on this assignment

Run update21 to create the cs21/labs/10 directory. Move down to this directory and edit the file grid.py to create your solution.

Introduction

For this assignment, you will write two classes, Cell and Grid, and then write a program that uses these classes to draw a picture to the graphics window. The Cell and Grid classes are designed to be used by programs that implement time-step simulations of some phenomena on a 2-D 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, each cell summarizes the information for its 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, in the insect model, if a neighboring cell is infested, it is likely that in a few time steps the cell will become infested too, and after some more time when the insects have eaten all the wheat in that cell, they will move on. In the next lab assignment, you will use the Cell and Grid classes to implement a simulation program like one of these.

In this assignment, 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 to the graphics window.

1. Implement the Cell class

The Cell class represents 1 square unit in a 2-D grid in the Graphics window. A Cell object maintains the following state:

• x and y position in the 2-D grid
• current value (True or False)
• value for the next time step (True or False)
• Rectangle object representing the cell in the graphics window

A Cell has the following methods:

• __init__: the constructor takes as parameters the location of the cell as x, y, and an initial value and creates a new Rectangle object at that position
• getX: returns value of x
• getY: returns the value of y
• getValue: returns the current value stored at the cell
• getRectangle: returns a reference to its rectangle field
• setNextValue: set the cell's next value to a new value passed in as a parameter
• update: change a cell's value to its next value and updates its color

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)
c1.getRectangle().draw(w)
c2 = Cell(0, 4, True)
c2.getRectangle().draw(w)
c3 = Cell(2, 2, False)
c3.getRectangle().draw(w)
w.getMouse()
w.close()
```
This program creates a graphics windows 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). This program should draw three cells, two are "on" (they store True) and one 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

The Grid class is used to represent the 2D simulated world. It has some state:

• the width and height of the world
• a GraphWin object
• a list of lists of Cell objects
The outer list will be indexed by x. This will return an inner list that will be indexed by y. To construct this list, create an inner list for each column of the grid and then append it on to the outer list. For example in a 3 by 3 grid the first inner list should contain cells for (0,0), (0,1), and (0,2).

The Grid has the following methods:

• __init__: the constructor takes the width and height as parameters. It first makes a graphics window of a size 500 by 500. Next, it uses the setCoords method of GraphWin to make a grid of size width by height within the window. It also creates a list of lists of Cell objects and draws each one in the graphics window. You can use the cell's getRectangle method to get a reference to the Rectangle object associated with the cell so that you can invoke its draw method, as was done in the testing program above.
• getWidth: returns the width of the grid
• getHeight: returns the number height of the grid
• getValue: takes x and y as parameters and returns the value of the cell at (x, y)
• setNextValue: takes x, y, and a new value as parameters and sets the next value of the cell at (x, y)
• update: simulates a time step where each cell's value should be updated to its next value
• clear: sets the next value of every cell in the grid to False and then updates the grid
• close: prompts the user to click to close the graphics window and waits for a mouse click
• toggle: takes an x and y as parameters and allows the user to toggle the value of any cell, except the special one at (x, y), from True to False or from False to True. The user indicates which cell they want to toggle by clicking on it. This should cause the cell's value and color to change and the method will return False. When they click on the cell at (x, y), no change will occur and the method will return True.

4. Test the Grid class

Modify the main in your grid.py file so that it will:

• create a Grid object
• 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
• use the grid's update method to change the values and the colors of the cells
• use the grid's close method to wait for a mouse click and then close the window

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 final step.

5. Drawing pictures with mouse clicks

The final version of your main program should use a while loop to repeatedly call the toggle method using (0,0) as the special cell. This will allow the user to interactively draw a picture on the grid by clicking cells on or off. Once the user clicks on the special cell at the lower left corner (0,0), the program should end.

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

Submit

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