CS 151: Lab #11

### Lab Exercise 11: Stochastic L-Systems

In this lab, we'll make the final extension to our L-systems. We'll enable our L-systems to have several different replacement strings for each rule. These are called stochastic L-systems, because when a symbol has multiple replacements, the replacement is chosen randomly. The result is more realistic looking structures and branching patterns.

To implement stochastic L-systems, we need some method of specifying a rule set. The current method is to use a list of rules, where each rule is a 2-element list. We're going to continue to use that idea. The set of rules will be a list of rules, where each rule is an N-element list. The first item in the list will be the symbol, while each of the remaining elements will be a different replacement string.

We will continue to pass a list of rules into the set method of the LSystem class. Internally, we will continue to use a dictionary to store the rules. The keys will be the same, but each dictionary entry will be a list of possible replacement strings. The list of replacement strings may have only one element or it may have many.

1. Make a copy of your lsystem3.py code from the last project. Call it lsystem4.py. If you wish, you can build upon my working version.
2. An example of a stochastic LSystem rule set is given below.
` [ ['a', 'ab', 'aa'], ['b', 'a', 'c' ] ]`
In the above example, the symbol 'a' (first element of first list) has two possible replacements 'ab' and 'aa'. The symbol 'b' (first element of the second list) has two replacements 'a' and 'c'.

Modify the set function of LSystem so that if the above list were passed in as the rules parameter, it creates a dictionary entry for the symbol that consists of a list of possible rule replacements. Note that the key is still the same, but the value needs to be a list containing all of the replacement strings. (Hint: The notation alist[1:] returns a list of all the elements of alist except the first one.)

3. Modify the generateString method of LSystem so that it randomly chooses one of the possible replacement strings. Note that the dictionary entry for a symbol, if it exists, is a list of replacement strings. The random.choice() method chooses a random element from a list.
4. Modify the read function so that it can handle multiple replacement strings. Note that words[1] is the key, and the value should be a list containing all of the remaining words on the line (from index 2 on).
5. Modify the write function so that it writes out all of the possible replacement strings. This will involve putting a loop over the replacement strings inside the loop over the keys in the dictionary.
6. The lsystem4.py code linked above contains a reasonable test function. Test out all of your changes and make sure that the results make sense.
7. Create a new python file. Import zturtle, lsystem4, and graphics into the file. Then create a function that does the following. Note that, if your zturtle.py has any issues, please use lab time to get it into working shape.

• Creates a GraphWin window
• Creates a ZTurtle object and stores it in the variable tree.
• Creates an LSystem object and stores it in the variable pattern.
• Sets the LSystem to have the rule set:
`[ ['F', 'fF[+F-FL][FL][-F+FL]', 'fF[+F-FL][FL]', 'fF[-F+FL][FL]' ] ]`
with three iterations, a distance of 8 and an angle of 15
• Positions the turtle (tree) in the middle of the window and towards the bottom pointing up.
• Calls the lsystem method of the turtle (tree), and passes it the LSystem pattern.
• Calls the draw method for the turtle object.
• Calls the getMouse() method to hold the program, then terminates.

The above series of steps should draw a single tree in the middle of the window. Running the program multiple times should produce a different tree each time.

Now you have an L-system class that can do multiple rules, and multiple stochastic rules, which allows you to make more realistic plant models. Hopefully, you also have working ZTurtle and LSystem classes.

Once you've finished the above exercise, keep going on the current project.