TurtleGraphics

Eric Roberts - Stanford University

In the TurtleGraphics assignment, students build part of a microworld interpreter that supports drawing pictures by moving a “turtle” around the graphics window. Programs for the turtle consist of strings of simple commands, such as F10 (which moves the turtle forward 10 pixel units) or R90 (which rotates the turtle 90 degrees to the right).

Unlike more traditional uses of turtle graphics in first-year courses, the TurtleGraphics assignment focuses less on graphics than it does on string manipulation. The code to implement the graphical user interface is provided as part of the assignment. The students are responsible for the string processing that goes into parsing the programs, executing the individual operations, and implementing a general find-and-replace mechanism. That find-and-replace mechanism gives the TurtleGraphics application the power of a Lindenmayer system, which supports the creation of complex patterns through repeated string substitution.

Lindenmeyer systems allow students to draw fascinating recursive diagrams without having to use recursion in their implementation. As an example, the following screenshot shows the TurtleGraphics application after drawing a Koch snowflake of order 3:

Snowflake image

The program starts out with code to draw a simple triangle, but becomes a fractal pattern after three global string replacements, each of which is similar to the replacement pattern at the lower right of the screenshot.

Resources

TurtleGraphicsAssignment.pdf - Assignment Handout
TurtleGraphics.pptx - Slides about this assignment (Eric's Nifty SIGCSE talk)
TurtleGraphicsStarterFiles.zip - Starter files for the assignment

Metadata

Summary Students implement the string processing components of a turtle graphics microworld in which programs are extended using successive replacements in the manner of Lindenmayer systems.
Topics
String processing, implementing a tokenizer class, stepwise refinement, generating recursive patterns through iterated replacement.
Audience
Students in the middle of CS 1, after they have learned about string processing.
Difficulty
Moderate.
Strengths
Students can generate amazing fractals diagrams including Koch snowflakes, Sierpinski triangles, and fractal approximations of plant growth without having to understand recursion in depth. The assignment provides students with significant practice using strings and forces them to implement a specialized tokenizer class. The assignment also offers extensive opportunities for extensions and serves as the basis for a contest that encourages highly motivated students to exercise their creativity.
Weaknesses
Students sometimes feel that the code they write themselves, which focuses on the string processing necessary to implement the application, is less exciting than the graphical parts of the assignment, which are provided to them.
Dependencies
Works well with the ACM Graphics Library but does not require it.
Variants
Several extensions are described in the assignment handout for students who want to push themselves beyond the minimum requirements.

Extra info about this assignment: