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.
$ update21 $ cd cs21/labs/11 $ 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. 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 types of simulations, the world is broken up into a grid of cells, where 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.
The cell class represents 1 square unit in a 2-D grid in the Graphics window. A Cell object has some some state including:
A Cell has the following methods:
The Grid class is used to represent the world. It has some state:
For a grid with 4 rows and 2 columns, your list of lists of Cell objects will look like this:
[ [r0c0,r0c1], [r1c0,r1c1], [r2c0,r2c1], [r3c0,r3c1] ]where r0c0 is the cell object in row 0,column 0; and r2c1 is the cell object in row 2,column 1.
The Grid has the following methods:
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.
class Cell(object): # any class-wide definitions go go here # these are fields that are associated with the class as a whole # their values that can be accessed using # <name of class>.<name_of_field> (ex) x = Cell.ON ON = 1 # This is the constructor for a Cell object. Remember that all # method functions have a 1st hidden parameter, self, that is # a reference to the object on which the method is invoked. # A call to this method might look like: c = Cell(3,4,Cell.ON) def __init__(self, row, col, val): # here is an example of what other method definitions might look like # a call to this might look like: c.foo(Cell.OFF) def foo(self, val) row = self.row # row is a local variable inside this method # self.row is a data member associated with this object
self.win.setCoords(0, 0, cols, rows)By doing this I can position Cell objects using their (row, column) index, rather than having to perform a conversion from row number to pixel row value and column number to pixel column value.
self.cell_list = [] self.cell_list.append( [Cell(0,0,Cell.OFF), Cell(0,1,Cell.OFF), Cell(0,2,Cell.OFF)]) self.cell_list.append([Cell(1,0,Cell.OFF), Cell(1,1,Cell.OFF), Cell(1,2,Cell.OFF)])
cell = self.cell_list[r][c]
from time import sleep
Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.