# Turtle Graphics Eric Roberts Nifty Assignments SIGCSE 2013 March 9, 2013 This is not your typical assignmentCeux ne sont pas vos devoirs typiques.

## Presentation on theme: "Turtle Graphics Eric Roberts Nifty Assignments SIGCSE 2013 March 9, 2013 This is not your typical assignmentCeux ne sont pas vos devoirs typiques."— Presentation transcript:

Turtle Graphics Eric Roberts Nifty Assignments SIGCSE 2013 March 9, 2013 This is not your typical assignmentCeux ne sont pas vos devoirs typiques

The TurtleGraphics Assignment Traditional TurtleGraphics assignments focus on having the students write programs that cause a simulated version of the Project LOGO Turtle to draw figures on the screen. In this assignment, students—typically somewhere in the middle of CS1—build various pieces of the interpreter for the TurtleGraphics microworld. The modules that the students create for this assignment focus on exercising their skills in the following areas: String processing1. Defining and implementing a class2. The beginnings of recursive thinking3.

TurtleGraphics Commands The TurtleGraphics interpreter understands programs, which are simply strings composed of the following commands: FnFn LnLn RnRn U D X n { commands } Move the turtle forward n units Turn left n degrees Turn right n degrees Raise the pen to stop line drawing Lower the pen to resume line drawing Repeat the specified commands n times Students can add various other commands (change pen color, change step size, save/restore position) as extensions.

TurtleGraphics in Action X36 { X4 { F75 L90 } L10 }

Lindenmayer Systems Using strings as the base language allows students to transform programs by making string substitutions. TurtleGraphics programs evolve through repeated application of substitution patterns. Systems that evolve in this way are called L-systems or Lindenmayer systems, after the Hungarian biologist Aristid Lindenmayer, which were originally designed to simulate plant growth.

Evolution through Substitution U R150 F47 L150 D X3 { F81 L120 } F81–>F27 R60 F27 L120 F27 R60 F27

Evolution through Substitution U R150 F47 L150 D X3 { F27 R60 F27 L120 F27 R60 F27 L120 } F81–>F27 R60 F27 L120 F27 R60 F27

Evolution through Substitution U R150 F47 L150 D X3 { F27 R60 F27 L120 F27 R60 F27 L120 } F81–>F27 R60 F27 L120 F27 R60 F27

Evolution through Substitution U R150 F47 L150 D X3 { F27 R60 F27 L120 F27 R60 F27 L120 } F27–>F9 R60 F9 L120 F9 R60 F9

Evolution through Substitution U R150 F47 L150 D X3 { F9 R60 F9 L120 F9 R60 F9 R60 F9 R60 F9 L120 F9 R60 F9 L120 F9 R60 F9 L120 F9 R60 F9 R60 F9 R60 F9 L120 F9 R60 F9 L120 } F27–>F9 R60 F9 L120 F9 R60 F9F9–>F3 R60 F3 L120 F3 R60 F3

Evolution through Substitution U R150 F47 L150 D X3 { F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 L120 } F9–>F3 R60 F3 L120 F3 R60 F3

Evolution through Substitution U R150 F47 L150 D X3 { F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 L120 F3 R60 F3 R60 F3 R60 F3 L120 F3 R60 F3 L120 }

What the Students Do Students are responsible for three pieces of the application: A TurtleTokenizer class. This class breaks a string into tokens, which consist of either a command letter followed by an optional numeric argument or a string of commands enclosed in braces. 1. The Lindenmayer command substitution model. Lindenmayer systems allow for multiple find-and-replace operations that are carried out simultaneously. For example, the pattern L–>R,R–>L interchanges all L and R commands. 2. Interpreting the command string. For the most part, this phase consists of transforming the command letter and argument returned by the tokenizer into appropriate calls to the GTurtle object. The X command requires an application of recursion that seems natural to students and causes almost no confusion. 3.

Why Is This Nifty? Students get to build important pieces of a larger application, which is after all what most people do in industrial software development positions. Students get lots of practice with string manipulation in a context where they can immediately see the results. Students have the chance to define an entire class and then use it in an application. Recursion comes up in a surprisingly natural way as part of the implementation of the X command. Most students seem to love playing with the TurtleGraphics application. There are lots of opportunities for extensions. It is easy to run a contest in parallel with the assignment.

One of the Contest Winners [c255:211:242 [f400 l f400 x400{ l f1 l f900 r f1 r f900}] u r180 f340 r f320 r88 d x4{c142:0:254 r u f40 d l130 f88 r40 f40 x6{r30 f16} r180l30 x7{r30f16} r1 f24 r42 f92 u l130 f70 x3{c255:0:0 u f20 d l130 f44 r40 f20 x6{r30 f8} r180l30 x7{r30f8} r1 f12 r42 f46 u l133.4 f c142:0:254 u f20 d l130 f44 r40 f20 x6{r30 f8} r180l30 x7{r30f8} r1 f12 r42 f46 u l133.4 f} f130r180}] [c255:0:0 u r180 f280 l f120 l d u f30 d l f60 r180 f30 r f25 l f25 r180 f55 r180 f60 l u f35 d l r14 f61 r152 f30 r104 f13 r180 f13 r76 f31 l75 u f5 l94 d f40 r30 f8 r30 f8 r30 f6 r30 f8 r30 f8 r30 f8 r30 f7 r30 f8 r30 f18 u l f21 l88 u f30 l d f40 r30 f8 r30 f8 r30 f6 r30 f8 r30 f8 r30 f8 r30 f7 r30 f8 r30 f18 u l f21 l88 u f30 l d u r f12 d l f25 r l120 f28 r120u f28 r120 d f28 l30 f25 l u f20 f30 l d f60 r160 f50 r220f50 l200f60 l u f5l d u f20 d f30 r30 f8 r30 f8 r30 f6 r30 f8 r30 f8 r30 f6 f30 r30 f8 r30 f8 r30 f6 r30 f8 r30 f8 r30 f5 u r180 f20 l92 f30 d u f18 d l89 f60 l f10 r180 f20 u r f60 l89 f5 u u f30 d l f60 r180 f30 r f25 l f25 r180 f55 r180 f60 l u f35 d l f60 r f25 r180 f25 l f30 l f20 r180 f20 l87 f30 l93 f25 u f5 d l f50 r30 f8 r30 f8 r30 f6 r30 f8 r30 f8 r30 f8 r30 f7 r30 f8 r30 f18 l130 f40 l50 u f4 d r l u l92 f50 r f30 r d r135 x5{f12 l45} x5{f12 r45} f12 ur180 f12 l44 f27 u f30 l d f60 r f10 r31 f13 r30 f13 r30 f23 r30 f13 r30 f13 r30 f10 u r180 f30 d l r14 f61 r152 f30 r104 f13 r180 f13 r76 f31 l75 u f5 l94 d u r f12 d l f25 r l120 f28 r120u f28 r120 d f28 l30 f25 l u f20 r f100 r f300 r182 d l f60 r180 f60 l f20 u f5 d l u f20 d f30 r30 f8 r30 f8 r30 f6 r30 f8 r30 f8 r30 f6 f30 r30 f8 r30 f8 r30 f6 r30 f8 r30 f8 r30 f5 u r180 f20 l92 f30 d u l f60 d r167 f62 l154 f62 r168 u f60 l180 r f5 d l f60 r f25 r180 f25 l f30 l f20 r180 f20 l87 f30 l93 f25 u f5 d l r u 66 r90 d f10 r30 f5 r30 f5 u l149 f10 l f17 u r u f30 l f30 l d r180 r f30 l l f60 r f25 r180 f25 l f30 l f20 r180 f20 l87 f30 l93 f25 u f5 d l x2{f60 r180 f60 l f20 u f5 d l} r l f60 r f25 r180 f25 l f30 l f20 r180 f20 l87 f30 l93 f25 u f5 d l f60 r160 f65 l160 f60 r180 f60 l u f5 d] u r180 f180 r f230 r d r f200 l45 f50 l45 f150 l f200 l45 f50 l45 f150 l f170 r x10 {f30 r f1 r f30 l f1 l} f30 l x10 {f50 r f1 r f50 l f1 l} u f85 l f140 r d x10 {f30 r f1 r f30 l f1 l} f30 l x10 {f50 r f1 r f50 l f1 l} u r180 f25 r f132 d f120 r x6{l f20 r180 f20 l f15} l f20 r180 f20 f120 r f90 r180 f50 l x20{f40 r f1 r f40 l f1 l} l180 u f40 r d f50 x3{r20 f8} l60 u r180 f300 l f50 r d r f100 r45 f25 r45 f75 r f100 r45 f25 r45 f75r f85 l x5 {f18 r f1 r f18 l f1 l} f18 r x5 {f8 r f1 r f8 l f1 l} u f24 r f34 l d x5 {f18 r f1 r f18 l f1 l} f18 r x5 {f8 r f1 r f8 l f1 l} u l r180 f100 lf33 l d f60 l f45 l f60 l f45 l90 f10 l x12{f20 r f1 r f20 l f1 l} r u f40 l f25 r180 d f25 r180 f25 x3{l20 f4} u l120 f58 d r f10 l dx2{f30 r f1 r f30 l f1 l} u f30 r f30 r180 c0:157:15 d f40 r f1 r f40 l f1 l f40 r f1 r f15 l40 f10 r f1 r f10 l140 f25 c255:0:0 x4{r150 f10 x8{l30 f3} f7 h}

The End

Download ppt "Turtle Graphics Eric Roberts Nifty Assignments SIGCSE 2013 March 9, 2013 This is not your typical assignmentCeux ne sont pas vos devoirs typiques."

Similar presentations