An L-System, or Lindenmayer System is a simple but powerful image drawing technique that employs "turtle" graphics.

The recursive nature of the L-system rules leads to self-similarity and thereby fractal-like forms. The forms produced via these rules 'grow' and becomes more complex by increasing the iteration level of the form.

Using an L-System to Create a Drawing

Imagine that a "turtle" with a pen in its tail is placed on the screen.

Based on the characters seen in a given "command" string, the turtle will perform different actions.

As the characters of this command string are examined from left to right, if an 'F' or 'G' is encountered, the turtle will take a step forward by some fixed amount (drawing a line behind it with its pen).

If instead, a '-' or '+' is encountered, the turtle turns by some fixed number of degrees left or right.

If any other letter is encountered, the turtle does nothing.

So for example, if the fixed number of degrees to turn is 90, the fixed distance to step forward is 10, and the command string was "F-F-F-F", the turtle would draw a square with perimeter 40.

However, in an L-System, we actually make things more interesting by starting with a "seed" string of commands, and repeatedly construct a new command string by replacing certain characters in accordance with some fixed rules.

For example, suppose we used the rule


If we started with a seed command string of


then after one round of replacements, we arrive at the new command string of


After a second round of replacements, we would have


As one can see, the length of the command string grows exponentially with the number of rounds of replacements, so one does not want to do this too frequently. Still, even after a couple of rounds the command string can result in a very interesting drawing by the turtle. To see the picture resulting from using this particular rule and seed with a "depth" of 5 rounds of replacements, compile and run the starter code and select "Koch" from the combo box before clicking the "Draw" button.

The Task

Read through the starter code carefully, making sure you understand what it does. Remember to look things up in the Java API when necessary.

Add the capability for the turtle, upon seeing a ']' in the command string, to return to the state it was in when the matching '[' was previously encountered. This "state" includes both the turtle's previous location and its previous direction of movement. You should be able to do this by adding no more than 15 lines of code to what you have been given.

Upon adding this capability, the drawing made by selecting "Mystery" in the combo box should closely resemble the grassy weed shown below.