CS21 Lab 11: Writing the Grid and Cell Classes

Due 11:59pm Tuesday night, Nov. 25

You may work with one partner on this assignment

Run update21, if you haven't already, to create the cs21/labs/11 directory. Then cd into your cs21/labs/11 directory and create the python programs for lab 11 in this directory (handin21 looks for your lab 11 assignments in your cs21/labs/11 directory):

$ update21
$ cd cs21/labs/11
$ vim grid.py
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. Simulating heat dissipation over a sheet of metal or an insect infestation on a crop of wheat, or even climate modeling are examples of such discrete time-step simulations. 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 cell's 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 (ON and OFF) 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 them to draw a pixelated picture to the graphics window, by setting Cells values to ON or OFF.

Cell class
The cell class represents 1 square unit in a 2-D grid in the Graphics window. A Cell object has some some state including: In addition, all Cell objects share the same definitions for ON and OFF and their associated colors (the Cell should be colored one color if its value is ON, another if its value is OFF).

A Cell has the following methods:

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

The Grid has the following methods:

Using these classes
At the bottom of the grid.py file that contains the Cell and Grid class definitions, add a main function that will create a new Grid object and then call its methods to draw a pixelated picture to the graphics window by setting Cell to ON or OFF. You could draw a smiley face, your initials, a house, anything you'd like.

For example, here is a run of my program that creates a 15 by 15 grid and draws my initials to it (notice that I have an option to "click to close" so that the program doesn't exit until the user is done viewing it):

$ python grid
click to close

You do not have to use these classes to draw your initials, but use them to draw something that is a bit more complicated than a single line. You can hard-code in the row and column dimensions for the grid for your drawing.

Hints/Getting Started
Optional Extensions
These are not required, so do not try these until you have the basic assignment complete and correct. Here are some extensions you can try:
Submit

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