Turtle Demo 4 Command Interpreter
Aus ProgrammingWiki
Archivierte Version vom 2009-10-27T14:57:10+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);</eval>
String[] fCmdList = { "F30", "R90", "F20", "L180", "F20", "R90", "F30", "R90", "F40" }; String[] tCmdList = { "U", "R90", "F40", "L90", "D", "F60", "L180", "F20", "R90", "F10", "L180", "F20" }; String[] hCmdList = { "U", "R90", "F70", "L90", "D", "F60", "L180", "F30",
"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]);
}