Buildings and Trees
The purpose of this project is to give you more practice working with classes and some experience with dictionaries in Python. You'll modify your L-system class to use dictionaries and handle multiple rules. Then you'll use the string interpreter that is part of your ZTurtle class to draw the trees and combine them with your buildings from project 6.
You must use your ZTurtle class to draw the trees and execute all turtle actions or it will not work with the Zelle graphics.
- Make a copy of your lsystem2.py file from the last project and call it lsystem3.py. As you go through the steps, modify your test function so that it tests all of the methods you are modifying (set, toString, generateString, read, and write). Make sure you know what to expect the program to print or do.
Update the set method in your Lsystem class to put the (possibly
multiple) rules into a dictionary instead of a list. Recall that the
rules are passed in to the set method as a list of lists. Each rule is
represented by a 2-element list where the first element in the list is
the symbol, and the second element in the list is the replacement
string. The algorithm will be as follows:
Initialize the field self.rules to an empty dictionary for each rule in the list of rules passed into the set method: assign rule to the dictionary location with the key rule
Update the toString method to properly print out the rules. The
algorithm for printing the rules will be as follows:
assign 0 to i for each key in the field self.rules: add to s the formatted string with i, the symbol, and the replacement string increment i
Update the generateString method to use dictionaries and multiple
rules. The algorithm is as follows:
copy self.baseString to a local variable base for the number of iterations: set a local variable newbase to the empty string for each character ch in base: if the dictionary has a key like ch add the replacement string to newbase else add ch to newbase assign newbase to base return base
- Update the write function to properly write out the rules. Loop over all of the keys in the dictionary and write out each rule to its own line.
- Update the read function to properly read in the rules. Each line with a rule will split into three parts. The first part is the word 'rule'. The second part is the symbol (also your dictionary key) and the third part is the replacement string (also the dictionary value).
- The final step is to write a program that uses at least one loop to create several (≥ 3) trees and other graphics objects in the same scene. You will need to use your ZTurtle class and the lsystem method from the last lab to draw the trees. You should use at least one L-system that involves multiple rules. You can use code from project 6 to generate the non-tree parts of the scene, if you wish.
The following are suggested extensions.
- Make use of lists, loops and other control structures in your final image to make the code as compact as possible.
- Make a class for your building, or other graphics objects. Feel free to use the AggregateBase class from the example in class. Just cite it in your code.
- Add other methods to the turtle besides lsystem that make other interesting objects.
- Create new L-systems that make interesting trees.
- Pick anything from ABOP to augment the L-systems and run with it.
- Generate a sequence of images where some aspect of the scene changes so that you can generate an animated sequence. Trees waving in the breeze would be interesting.
Create a web page for your writeup. Your writeup should include the following.
- A brief description of the task (in your own words).
- Explain how a dictionary is different from a list, and why it is appropriate for the task of identifying the proper rule.
- Describe your algorithm for creating the final scene.
- Show the L-system for each of your images.
- A description of and pictures for any extensions you did.
When your writeup is ready for viewing and linked to your index page, send me an email.
To hand in your python code, place it on the Academics fileserver in the private folder. Do not post it on the web. You may include snippets from your code in your writeup to demonstrate how certain aspects of the function work, but please do not include the whole thing.
To place the code on the fileserver, go to the server fileserver1 and select the Academics volume. Within that there ought to be a CS151 folder, and within that there should be a folder with your username, and within that ought to be a folder labeled private. You can read and write files to the private directory and I can read them, but no one else can access them. Put your python files in that directory to hand them in. Please organize your files into folders by project.