Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT
23.8. Interpret turtle drawing commands
Medium
Topics
This question is in
23. Text processing and interpretersTurtle graphics is the smallest real programming language you can build: a "turtle" sits on a page, holding a pen, and obeys commands such as forward 10 and right 90. repeat 4 [ forward 10 right 90 ] draws a square. Writing an interpreter for it teaches the same steps as any language tool, from a formula field to a template engine: split the text into tokens, parse the tokens into a tree, and run the tree against some state.
Write runTurtle(program, options) in turtle.js. It returns the lines the turtle drew and where it ended.
Signature
function runTurtle(program, options) // returns { x, y, heading, penDown, lines }program: the program text.options.maxSteps: the most steps the program may take (default100000).optionsmay be left out.- Returns
{ x, y, heading, penDown, lines }, where each line is{ x1, y1, x2, y2 }, in drawing order.
| Command | Short form | What it does |
|---|---|---|
forward n | fd n | move n along the heading |
back n | bk n | move n the other way (the heading does not change) |
right d | rt d | turn d degrees clockwise |
left d | lt d | turn d degrees counter-clockwise |
penup | pu | stop drawing |
pendown | pd | start drawing |
repeat k [ … ] | run the commands in brackets k times |
Requirements
- The turtle starts at
(0, 0)with heading0(north, toward positivey) and the pen down. Movingnchangesxbyn * sin(heading)andybyn * cos(heading). - A move records a line when the pen is down and the distance is not 0.
- The heading is always reported between 0 (included) and 360 (excluded).
- Every returned number (position, heading, line ends) is rounded to 6 decimals and is never
-0. Rounding is for the output only: the turtle keeps exact values while it runs. - Words are separated by any whitespace; brackets need no spaces around them; letter case does not matter;
#starts a comment to the end of the line. A number has an optional-, digits and an optional decimal part (10,-5,.5,1.25). - Repeats nest to any depth.
repeat 0runs nothing. - The whole program is parsed before anything runs. A
SyntaxErroris thrown for an unknown command, a missing or non-numeric argument, arepeatwithout[, a[that is never closed, and a]that closes nothing. ARangeErroris thrown, while parsing, for a repeat count that is not a whole number of 0 or more. - Each command run counts as one step, and so does each round of a
repeat. Going pastmaxStepsthrows aRangeError, even when the repeat's body is empty. - Each call starts from a fresh turtle.
Examples
runTurtle("repeat 3 [ fd 10 rt 120 ]");
// { x: 0, y: 0, heading: 0, penDown: true, lines: [
// { x1: 0, y1: 0, x2: 0, y2: 10 },
// { x1: 0, y1: 10, x2: 8.660254, y2: 5 },
// { x1: 8.660254, y1: 5, x2: 0, y2: 0 },
// ] }runTurtle("pu fd 10 pd fd 5").lines;
// [{ x1: 0, y1: 10, x2: 0, y2: 15 }]
runTurtle("lt 90").heading;
// 270runTurtle("repeat 2 [ fd 1"); // throws SyntaxError
runTurtle("repeat 2 [ fd 1 ]", { maxSteps: 3 }); // throws RangeError: 2 rounds + 2 moves = 4 stepsEnvironment
- Plain JavaScript in a browser page; the tests call the global
runTurtlefromturtle.js. - Procedures, variables, colours and drawing on a canvas are not part of this problem.
- Any built-in is allowed.
evalandnew Functionare blocked in the page.
Hints
- A
repeatholds a list of commands, which can hold arepeat. What shape of data, and what kind of function, fits that? - JavaScript's
%keeps the sign:-90 % 360is-90. How do you get 270? - If only commands count as steps, how long does
repeat 1000000000 [ ]run?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it
Back to the question
Loading the editor…