Problem Set 0: Turtle Graphics
The deadlines for this problem set are shown on the course calendar.
|Safe from bugs||Easy to understand||Ready for change|
|Correct today and correct in the unknown future.||Communicating clearly with future programmers, including future you.||Designed to accommodate change without rewriting.|
- introduce the tools we will use in 6.102, including TypeScript, VS Code, Mocha, and Git;
- introduce the process for 6.102 problem sets;
- and practice with TypeScript and start using tools from the standard library.
You should have already read and completed the Getting Started guide, to install the software you need.
Read and do the exercises in Git 1: Version Control.
Create your remote repository with the starting code for the problem set by visiting didit.mit.edu/6.102/sp23. Go to
psets/ps0and click the button to create your repository. You will only be able to create a repo after the course signup forms are processed and the problem set is announced, which happens after the first class meeting.
If you can’t create a repo after the ps0 announcement, you are not registered for 6.102. Contact the staff (how?) to check your status.
If you are unfamiliar with using the command line, Basic Git introduces key commands like
Clone your repo. You will access the remote repository over SSH from your laptop.
Note: Getting Started has setup you must perform before using Git.
git clonecommand at the top of your
psets/ps0page on Didit. It is a long command that looks something like this:
git clone ... firstname.lastname@example.org:6102-sp23/ps0-«username».git
Copy this entire command, paste it into your terminal, and run it. The result should be a directory named
ps0-«username»with the starting code for the problem set. Keep the terminal window open — you will also use it to commit your code.
git clonesays “does not appear to be a git repository” or “could not read from remote repository”, check your repository URL for typos. Make sure your repository is listed on Didit.
Check that you can authenticate to github.mit.edu in the last part of Getting Started step 7.
If you are a listener or not taking 6.102 for credit, you can clone problem set repositories using a modified URL: after
ps0, omit the dash-username. You will not be able to push to this repository, it’s a read-only remote.
You should see something like “audited 138 packages” – the exact number may be different, but it should be much greater than 1. If you see “audited 1 package” instead, then you are probably in the wrong folder. Make sure you
cdinto your clone.
But if you see error lines labeled “npm ERR!”, then something is wrong. Get help.
Command line: Run
codium .) in your terminal window to use VS Code’s command line interface to open the current directory in a new VS Code window. If you instead get an error message, configure VS Code to launch from the command line, close and reopen your terminal, and try running
If VS Code asks you “Do you trust the authors of the files in this folder”, then click the checkbox “Trust the authors of all the files in the parent folder” (so that you don’t have to answer this dialog box in the future, assuming you put all your 6.102 code in the same parent folder) and then answer Yes.
I can write my solution while sitting near (or on a video call with) another person writing theirs.
I can write my solution while sitting near (or on a video call with) another person writing theirs, talking about specific algorithmic steps to make sure we both got it.
Read about Slack Days in the general information handout.
Go to the Caesar code-reviewing system and click “request extension” to see what the extension page looks like, including your budget of slack days for the semester.
Read and do the exercises in Basic TypeScript.
Logo is a programming language created at MIT that originally was used to move a robot around in space. Turtle graphics, added to the Logo language, allows programmers to issue a series of commands to an on-screen “turtle” that moves, drawing a line as it goes. Turtle graphics have also been added to many different programming languages, including Python, where it is part of the standard library.
forward(units)Moves the turtle in the current direction by
unitspixels. Following the original Logo convention, the turtle starts out facing up.
turn(degrees)Rotates the turtle by angle
degreesto the right (clockwise).
Your first task is to implement
drawSquare(turtle:Turtle, sideLength:number), using the two functions introduced above:
turn. The specification comment above
drawSquareleaves several things unspecified, like which direction to turn, so those decisions are up to you as the implementer.
Once you’ve implemented the function, go to the
mainfunction creates a new turtle and instructs the turtle to draw on screen. There should be a call to
drawSquarealready there, but commented out. Uncomment this call so that your
drawSquarewill be called when the code is run.
From the terminal, in your
From VS Code: open the Run pane with View → Run, and choose “Run npm start” from the dropdown at the top of the pane, as shown on the right. Then click the green play button next to the dropdown or use Run → Start Debugging from the menubar. This will open a terminal pane inside VS Studio and run
If your Run pane just has three big buttons on it, like the image shown on the right, then make sure you have
ps0-«username»as the root folder in your Explorer pane, as described above.
npm startruns, you will see any output or errors from your program. If the program succeeds, the resulting drawing will be written to
output.html, and a browser tab will pop up showing the drawing.
which shows you files that have been created, deleted, and modified in the project directory. You should see
turtlesoup.tslisted under “Changes not staged for commit.” This means Git sees the change, but you have not (yet) asked Git to include the change as part of your next commit.
will actually commit the changes locally, after opening your default editor to allow you to write a commit message. Your message should be formatted according to the Git standard: a short summary that fits on one line, followed by a blank line and a longer description if necessary.
If Git warns you about configuring your default identity or you can’t edit your commit message, you did not follow the instructions in the Getting Started guide. Getting Started has setup you must perform before using Git.
to see the history of commits in your project. (
git lolis not a misspelling; it is an alias for the
git logcommand that we configured in Getting Started.)
Important: only the local history has the new commit at this point; it is not stored in your remote repository. This is one important aspect where Git is different from centralized systems such as Subversion and CVS.
At this point, the remote repository on github.mit.edu now has the same history as your local repository. It is important to remember that we will be grading the history in the repository on github.mit.edu; if you forget to push, we won’t see your commits.
Didit: when you push your commit, the Didit system will run a subset of the autograder on your code. Didit clones its own copy of your code from what you’ve pushed to github.mit.edu, compiles it, and runs some tests.
Visit Didit after you push to see the report.
Right now, all tests will fail since you’ve only implemented
drawSquare and it has no public tests.
As you work, and especially as a deadline approaches, it is your responsibility to check Didit’s build report and ensure that we can compile and run your code.
github.mit.edu: you can browse the contents of your remote repository by visiting
github.mit.edu/6102-sp23 or following links from Didit to github.mit.edu.
In this problem set, you should only need to edit
You should not change any of the function signatures (what’s a function signature?) in that file, or you risk receiving zero points on the problem set.
chordLength, reading the specification comment above it carefully to understand what it should do. (You can also click this link to see the same specification on a web page.)
You may also find the built-in Math functions useful.
test, and add
testto the name of the source file they are testing. So the public tests for this problem set are in
From the terminal, in your
If your implementation of
chordLengthis correct, you should see green check marks ✓ next to the
chordLengthtests in the list of results, as shown on the right. (You may have to scroll your terminal window upwards to see this.)
The red lines in the remaining sections of the test report, and the “6 failing” in the summary at the bottom, indicates that those tests have failed. That shouldn’t surprise us, since the other tests are exercising methods we haven’t implemented yet. Once they are passing, all those tests will turn into green check marks!
chordLength 1) acute angle, integer result 2) right angle, decimal result 3) obtuse angle, decimal result distance 4) for p1.x = p2.x, p1.y != p2.y 5) for p1.x != p2.x, p1.y != p2.y 6) for p1.x = p2.x, p1.y = p2.y ... 0 passing (20ms) 10 failing 1) chordLength acute angle, integer result: Error: implement me! at chordLength (src/turtlesoup.ts:29:11) at Context.<anonymous> (test/turtlesoupT at processImmediate (node:internal/timer 2) chordLength right angle, decimal result: Error: implement me! at chordLength (src/turtlesoup.ts:29:11) at Context.<anonymous> (test/turtlesoupT at processImmediate (node:internal/timer ...
- If Ctrl-/Command-clicking on a filename doesn’t open it, then make sure you are in the Terminal pane of Visual Studio Code (not in the Debug Console pane, and not in an external terminal window).
Try Ctrl-/Command-clicking on one of the
test/turtlesoupTest.tslines in the stack trace, and look at the code for the test. On that line, you should see a call to your
chordLengthimplementation with particular values for its arguments, within a call to
assertAlmostEqual()that checks whether your implementation’s return value was correct. A Mocha test generally has this form: it calls your implementation, then makes one or more assertions about the value that your implementation returned.
Assert that two numbers are equal within a given error tolerance.
actual— answer actually returned by the module being tested
expected— answer expected by the test
delta— actual is considered equal to expected if it’s within +/- delta; defaults to 0.001
message— optional message with extra information about this test case, to display when the assertion fails
Passing the public Mocha tests we provide does not necessarily mean that your code is perfect. You need to review the function specifications carefully. In future problem sets, we’ll also expect you to write your own Mocha tests to verify your code.
You may notice an opportunity to DRY out
drawApproximateCircle. Take advantage of the opportunity, but be careful not to change the specification of either function.
Commit to Git.
Once you’re happy with your implementations of these functions, commit and push!
Committing frequently – whenever you’ve fixed a bug or added a working and tested feature – is a good way to use version control, and will be a good habit to have for your team projects.
Pro tip: for short commit messages, you can skip the editor popup using
git commit -m, e.g.:
git commit -m "implemented chordLength and drawApproximateCircle"
This function calculates the distance between two points, where the points are represented by the
Pointclass defined in
This function finds a path (a sequence of turns and moves of the turtle) that visits a list of given points. You should use your
distancemethod as appropriate, and you may want to define other helper functions to make your code clearer.
In this function, you have the freedom to draw a piece of art you wish, with some complexity and aesthetic appeal. Your work will be judged both on visual complexity and interestingness, and on the code used to draw it.
Your art should need at least 20 lines of non-repetitive code, so just drawing a few lines or polygons is not sufficient. Use helper methods, loops, etc. rather than simply listing forward and turn commands.
Here are some examples of the kinds of images you can generate procedurally with turtle graphics, though note that many of them use more commands than what we’ve provided here.
Make sure you commit AND push your work to your repository on github.mit.edu.
We will use the state of your repository on github.mit.edu as of 10:00pm on the deadline date.
git push, the continuous build system attempts to compile your code and run the public tests (which are only a subset of the autograder tests).
You can always review your build results at didit.mit.edu/6.102/sp23.
- There is no guarantee that Didit tests will run within any particular timeframe, or at all. If you push code close to the deadline, the large number of submissions will slow the turnaround time before your code is examined.
- If you commit and push right before the deadline, it’s okay if the Didit build finishes after the deadline, because the commit-and-push was on time.
- Passing some or all of the public tests on Didit is no guarantee that you will pass the full battery of autograding tests — but failing them is almost sure to mean lost points on the problem set.