160 likes | 283 Views
Ceux ne sont pas vos devoirs typiques. This is not your typical assignment. Turtle Graphics. Eric Roberts Nifty Assignments SIGCSE 2013 March 9, 2013. The TurtleGraphics Assignment.
E N D
Ceuxne sont pas vosdevoirs typiques This is not your typical assignment Turtle Graphics Eric Roberts Nifty Assignments SIGCSE 2013 March 9, 2013
TheTurtleGraphicsAssignment • Traditional TurtleGraphicsassignments 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 TurtleGraphicsmicroworld. • The modules that the students create for this assignment focus on exercising their skills in the following areas: 1. 2. 3. String processing Defining and implementing a class The beginnings of recursive thinking
TurtleGraphicsCommands • The TurtleGraphics interpreter understands programs, which are simply strings composed of the following commands: Fn Move the turtle forward n units Ln Turn left n degrees Rn Turn right n degrees Raise the pen to stop line drawing U Lower the pen to resume line drawing D Xn {commands} Repeat the specified commands n times • Students can add various other commands (change pen color, change step size, save/restore position) as extensions.
TurtleGraphicsin 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 AristidLindenmayer, 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 F9 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 } 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: 1. 2. 3. 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. 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. 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.
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 Xcommand. • 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 ru 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 ldu f30 dl f60 r180 f30 r f25 l f25 r180 f55 r180 f60 lu f35 dl 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 ul f21 l88 u f30 ld f40 r30 f8 r30 f8 r30 f6 r30 f8 r30 f8 r30 f8 r30 f7 r30 f8 r30 f18 ul f21 l88 u f30 ldur f12 dl f25 r l120 f28 r120u f28 r120 d f28 l30 f25 lu f20 f30 ld f60 r160 f50 r220f50 l200f60 lu f5l du 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 du f18 d l89 f60 l f10 r180 f20 ur f60 l89 f5 uu f30 dl f60 r180 f30 r f25 l f25 r180 f55 r180 f60 lu f35 dl f60 r f25 r180 f25 l f30 l f20 r180 f20 l87 f30 l93 f25 u f5 dl f50 r30 f8 r30 f8 r30 f6 r30 f8 r30 f8 r30 f8 r30 f7 r30 f8 r30 f18 l130 f40 l50 u f4 drlu l92 f50 r f30 rd r135 x5{f12 l45} x5{f12 r45} f12 ur180 f12 l44 f27 u f30 ld f60 r f10 r31 f13 r30 f13 r30 f23 r30 f13 r30 f13 r30 f10 u r180 f30 dl r14 f61 r152 f30 r104 f13 r180 f13 r76 f31 l75 u f5 l94 dur f12 dl f25 r l120 f28 r120u f28 r120 d f28 l30 f25 lu f20 r f100 r f300 r182 dl f60 r180 f60 l f20 u f5 dlu 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 dul f60 d r167 f62 l154 f62 r168 u f60 l180 r f5 dl f60 r f25 r180 f25 l f30 l f20 r180 f20 l87 f30 l93 f25 u f5 dlru 66 r90 d f10 r30 f5 r30 f5 u l149 f10 l f17 uru f30 l f30 ld r180 r f30 ll f60 r f25 r180 f25 l f30 l f20 r180 f20 l87 f30 l93 f25 u f5 dl x2{f60 r180 f60 l f20 u f5 dl} rl f60 r f25 r180 f25 l f30 l f20 r180 f20 l87 f30 l93 f25 u f5 dl f60 r160 f65 l160 f60 r180 f60 lu f5 d] u r180 f180 r f230 rdr 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 rd 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 rd f50 x3{r20 f8} l60 u r180 f300 l f50 rdr 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 ld x5 {f18 r f1 r f18 l f1 l} f18 r x5 {f8 r f1 r f8 l f1 l} ul r180 f100 lf33 ld f60 l f45 l f60 l f45 l90 f10 l x12{f20 r f1 r f20 l f1 l} ru f40 l f25 r180 d f25 r180 f25 x3{l20 f4} u l120 f58 dr 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}