You may work with one partner on this assignment
Run update21, if you haven't already, to create the cs21/labs/10 directory. Then cd into your cs21/labs/10 directory and create the python programs for lab 10 in this directory (handin21 looks for your lab 10 assignments in your cs21/labs/10 directory):
$ update21 $ cd cs21/labs/10 $ vim grid.py
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
In your grid.py file, 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:
A Cell has the following methods:
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. You can add calls to other Cell methods to test them as well.
Once you are satisfifed that your Cell class is correct, comment out
(or delete) all this Cell testing code from your main function.
The Grid class is used to represent the 2D simulated world. It has some state:
The Grid has the following methods:
Modify main in your grid.py file so that it will:
Once you are convinced that your classes are working correctly you
can move on to the final step.
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.
Below is an example run of my program, where I clicked on grid cells to "draw" my initials in the grid.
$ python grid.py Click on cells to change their color. click on cell (0,0) to stop the toggling session. Click to close
Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.