CS 151: Lab 8

Title image Project 8
Fall 2019

Lab Exercise 8: Classes

The purpose of this lab is to give you practice in creating your own classes. In particular, we will convert both the lsystem and the turtle interpreter modules into classes. Ultimately, this will make it easier to build a more complex system.

If you have not already done so, take a look at the book 'The Algorithmic Beauty of Plants', which is the basis for this series of lab exercises. You can download the entire book from the algorithmic botany site

The other piece we'll be implementing this week is handling multi-rule L-systems. Adding this capability will enable you to draw more complex L-system structures.


Tasks

Most of the exercises in lab will involve building up an L-system class. This week it will be important for you to use the method names provided. The next several labs will expect the L-system and TurtleInterpreter classes to have certain methods with particular names.

  1. Setup

    Create a new working folder and a new lsystem.py file. Label this one as version 2 in the comments at the top of the file. The lsystem.py file will hold the Lsystem class and a test function for it.

  2. Define an Lsystem class

    Begin a class called Lsystem. A class has a simple structure. It begins with the class declaration, and all methods are indented relative to the class definition, similar to a function.

    class Lsystem:

    The first step in creating a class is to define the __init__() method, which is executed when a Lsystem object is created. The __init__ method should set up the fields of a class and give them reasonable initial values.

    Init methods often take optional arguments. You can make an argument optional by assigning a value to it in the parameter list of the method declaration. For example, the following function has two arguments, the second of which is optional with a default value of 5.

    def boo( a, b = 5 ):
      print(a, " ", b)

    The Lsystem init method should have two arguments: self, and an optional filename. If the method receives a filename, it should read Lsystem information from the file by calling the read method of self (which you'll create below), passing the filename as the argument.

    def __init__(self, filename=None):

    The Lsystem init should create two fields: base and rules. The field base should be initialized to the empty string. The field rules should be initialized to an empty list.

    + (more detail)

    To create a field of a class, put the prefix self. in front of the name of the field and assign something to it. The line of code below creates the field base and assigns it the empty string.

    self.base = ''

    Here is the template for the init method

    def __init__( self, filename = None ):
      # assign to the field base, the empty string
      # assign to the field rules, the empty list
      # if the filename variable is not equal to None
        # call the read method of self with filename as the argument
    								
  3. Create mutator and accessor methods

    Accessor methods are a set of functions that should be used by a program working with an object to obtain internal values from the object. Mutator methods are a set of functions that allow another program to modify the internal values of an object.

    Write the following mutator and accessor methods.

    • def getBase(self): - the getBase function should return the value of the base field of the object (self).
    • def setBase(self, b): - the setBase function should assign to the base field (self.base) the value in b.
    • def getRule(self, index) - the getRule function should return the specified single rule from the rules field of self.
    • def addRule(self, newrule) - the addRule function should append newrule to the rules field of self.
  4. Write a numRules method

    Create a method def numRules(self): that returns the number of rules in the rules list.

  5. Write a read method

    def read(self, filename):

    Similar to last week, the read method should open a file, read the L-system information, and store the information in the appropriate fields. Make sure the base string and rules list are both set to empty before starting to read the file. Use the mutators to update the values of the L-system fields.

    + (more detail)

    You can use the following template for the read function.

    def read( self, filename ):
      # assign to a variable (e.g. fp) the file object created with filename in read mode
      # for each line in fp 
        # assign to a variable (e.g. words) the result of calling the split() method on the loop variable
        # if the first item in words is equal to the string 'base'
          # call the setBase method of self with the new base string
        # else if the first item in words is equal to the string 'rule'
          # call the addRule method of self with the new rule (the slice of words from index 1
    
    
      # call the close method of the file
  6. Test your code so far

    Once you are done with all of the above steps, download and run this test function. It should nicely print out a single rule L-system read from a file and then print out a second L-system created in the test program. Running the program with systemA1.txt as the command line argument, you should get the output below.

    Case 1 systemA1.txt
    F-F-F-F
    F -> F-FF--F-F
    
    Case 2
    F--F--F
    F -> F+F--F+F
  7. Write a replace method

    In order to handle multiple rules, we need to write our own replace method for an L-system.

    def replace(self, istring):

    The replace function should start by creating an empty string to hold the output. It should then loop over each character of the input string istring. For each character, if there is a rule in the rules list with a matching symbol, add the replacement string to the output string. If there is no matching rule, add the character itself to the output string. Finally, return the output string.

    + (more detail)

    The algorithm template is below. It scans through the string, and for each character it tests if there is a matching rule. If a rule exists, it adds the replacement to a new string, If there is no rule that matches, it adds the character itself to the new string.

    def replace(self, istring):
      # assign to a local variable (e.g. tstring) the empty string
      # for each character c in the input string (istring)
        # set a local variable (e.g. found) to False
        # for each rule in the rules field of self
          # if the symbol in the rule is equal to the character in c
            # add to tstring the replacement from the rule
            # set found to True
            # break
        # if not found
          # add to tstring the character c
      # return tstring
    								
  8. Write a buildString function

    Create a buildString(self, iterations) method. It will be almost identical to the buildString function from version 1 except it will use the replace function from above. The code outline is below.

    def buildString(self, iterations):
      # assign to a local variable (e.g. nstring) the base field of self
      # for the number of iterations
        # assign to nstring the result of calling the replace method of self with nstring as the argument
      # return nstring
  9. Test your class

    Copy the following main function and put it at the bottom of your Lsystem class file. Call it like we did last week. (Note: This main function really is a function, and is not a method in any class.)

    python lsystem.py systemA1.txt

    Put an import sys at the top of your file. The test code should print out the lsys object (which will look odd, the base string, and the first rule. In addition, it will print out the result of running 2 iterations of replacement on the base string.

    def main(argv):
    
        if len(argv) < 2:
          print('Usage: lsystem.py <filename>')
          exit()
    
        filename = argv[1]
        iterations = 2
    
        lsys = Lsystem()
    
        lsys.read( filename )
    
        print( lsys )
        print( lsys.getBase() )
        for i in range( lsys.numRules() ):
          rule = lsys.getRule(i)
          print( rule[0] + ' -> ' + rule[1] )
    
        lstr = lsys.buildString( iterations )
        print( lstr )
    
        return
    
    if __name__ == "__main__":
        main(sys.argv)

    You can download and run the file on any of the following examples. Systems C through G require multiple rules.


  10. Define a TurtleInterpreter class

    Create a new file called turtle_interpreter.py. Label it as version 2 in the comments at the top of the file. You'll want to import the turtle package, and probably the random and sys packages as well. Begin the class definition for a TurtleInterpreter class.

    class TurtleInterpreter:

  11. Write the __init__method

    Create an __init__ method with the definition below. The init should call turtle.setup(width = dx, height = dy ) and then set the tracer to False (if you wish).

    def __init__(self, dx = 800, dy = 800):
      # call turtle.setup with dx and dy as the arguments
      # set the turtle tracer to false (optional)
  12. Write a drawString method

    Create a drawString method for the TurtleInterpreter class. You can copy and paste the drawString function from version 1 except that the new method definition will need self as the first argument (and you will need to remember to indent it properly).

    def drawString(self, dstring, distance, angle):

  13. Copy the hold method

    Copy the hold function from last week and paste it into your class. Again, indent the function so it is part of the class block and add self as the first argument to the definition.

  14. Make some useful action functions

    One of the goals of building the turtle interpreter is to encapsulate all of the turtle commands. Therefore, we need to make some turtle interpreter methods that let us place and orient the turtle and set its color.

    Add the following methods to your turtle interpreter class.

    • def place(self, xpos, ypos, angle=None): - the method should pick up the pen, place the turtle at location (xpos, ypos), orient the turtle if the angle argument is not None, and then put down the pen. You can use the turtle.
    • def orient(self, angle): - the method should use the setheading function to set turtle's heading to the given angle.
    • def goto(self, xpos, ypos): - the method should pick up the turtle, send the turtle to (xpos, ypos), and then put the pen down.
    • def color(self, c): - the method should call turtle.color() with the argument c to set the turtle's color.
    • def width(self, w): - the method should call turtle.width() with the argument w to set the turtle's width.
  15. Test your new class

    Download the test function and run it using one of the 2-rule lsystems as an argument (e.g. systemC.txt, systemD.txt, or systemF.txt). Note, the test function will look better if you define the character 'L' as a leaf.

When you are done with the lab exercises, you may begin the project.