Turtle Demo 4 Command Interpreter

Aus ProgrammingWiki
Archivierte Version vom 2011-01-10T19:19:27+00:00. Aktuelle Version

<eval id="4adad1eed15da"> canvas.clear(); // Zeichenfläche löschen Turtle f = new Turtle(1); Turtle t = new Turtle(1); Turtle h = new Turtle(1); f.penColor(255,0,0); f.penWidth(5); t.penColor(0,255,0); t.penWidth(5); h.penColor(0,0,255); h.penWidth(5); String[] fCmdList = { "F30", "R90", "F20", "L180", "F20", "R90", "F30", "R90", "F30" }; String[] tCmdList = { "U", "R90", "F45", "L90", "D", "F60", "L180", "F20", "R90", "F10", "L180", "F20" }; String[] hCmdList = { "U", "R90", "F70", "L90", "D", "F60", "L180", "F30", </eval>

                     "L90", "F25", "L90", "F30", "R180", "F60", "L90" };

void move(Turtle t, String command) { // command interpreter

 switch (command.charAt(0)) {
   case 'F' : t.forward(Integer.valueOf(command.substring(1))); break;
   case 'R' : t.right(Integer.valueOf(command.substring(1))); break;
   case 'L' : t.left(Integer.valueOf(command.substring(1))); break;
   case 'U' : t.penUp(); break;
   case 'D' : t.penDown(); break;
 };

}; for(int step = 0; step < 20; step++) {

 if (step < fCmdList.length) move(f, fCmdList[step]);
 if (step < tCmdList.length) move(t, tCmdList[step]);
 if (step < hCmdList.length) move(h, hCmdList[step]);

} // now the turtles leave the stage f.penUp(); t.penUp(); h.penUp(); for(int i = 0 ; i < 30; i++) {

h.forward(20); // leading
t.setAngleTo(h);
t.forward(20);
f.setAngleTo(h);
f.forward(20);

}

Ihr Browser unterstützt Canvas nicht.