CSci 1301: Lab 3

Due: Monday, October 2nd at 11:59pm by e-mail

What to submit

The lab is done in groups of 2 or 3. In the beginning of each file please write (in comments) the names of all group members.

At the end of the lab please send me and your group partner(s) all your Racket files as e-mail attachments. My e-mail is elenam at morris.umn.edu. The subject of your e-mail must be "1301 Lab 3", followed by your initials, followed by "Final" or "Not final", depending on whether this is a final submission or you are still working on it. If you need to finish it, make sure to set up a time with your group partner(s) to finish the lab.

About this lab

Your task is to explore and modify world programs. Use the examples we used in class (on the resources page) for reference; there is also a link to the book chapter there. Make sure to ask questions when you are stuck.

Task 1 (12 points): key handling and ending the world

For this task you start with the following world example: world-car-example-click.rkt. It has the starting code for handling keyboard keys and for ending the program when the car reaches far right end of the canvas. You need to fill in the missing pieces. Specifically:

  1. Study the function move-on-key. It is supposed to move the car to the initial position when the left arrow key is pressed. However, as is it just returns the current car position and fails two of the check-expect tests. Change it to return the correct value. Test it both by making sure that check-expects work and by running the program and using the left arrow on the keyboard to move the car back to the start.
  2. Add another case to the cond in the move-on-key to make it so that pressing the right arrow key brings the car to the rightmost position of the canvas. Uncomment the check-expect for the "right" key and replace ??? for this case by the correct value. After you've made both of these changes, test your program both using check-expect and by actually using the right key.
  3. stop-when part the big-bang attaches a function that checks the world state to see if the program needs to end. It ends when the function returns #true. Uncomment the part of the big-bang that uses stop-when and the end? function below in the file. Currently end? always returns #false, so the program never ends. We would like the program to end when the car reaches the right side of the canvas. Examine the test cases and fill in the correct expression for end?. Again, test the program to make sure that everything works as expected.

Task 2 (20 points): a world with a falling object

For this task you start with the following world example: humpty-dumpty.rkt. Your task are as follows:

  1. Read the description of the world above main to see what the world program is supposed to do and what the world state is.
  2. Change render to place the glass ball at the x coordinate specified in the constant ball-x-coord and the y coordinate given by the world state (it starts at the top of the canvas). Note that check-expects give the correct behavior and provide a hint for the expression in render. You don't need to implement the ball breaking as it reaches the ground.
  3. Once you added rendering of the ball, you need to change the fall function to make the ball falling at the number of pixels per clock tick that's given in the variable speed. Uncomment the check-expects, fill in the right expression, and run the program to test. The ball doesn't need to stop at the bottom of the canvas.
  4. Now you need to make it so that when one clicks on the ball, it returns back to the top. In order to implement it, write two helper functions, distance and within-radius?. Make sure to read the signatures, the descriptions and the check-expects. Note that the within-radius? helper function uses the distance. You might want to look up the formula for distance between two points.
  5. Using the within-radius? function, write the move-to-start function according to its comments and tests. Uncomment the line in big-bang that uses it as a mouse handler. Test your program.
  6. Change the fall function so that once the ball reaches the ground (see ground-size constant), it stops falling and remains at the same position. That would require making fall into an if or a cond. Add tests to check for this case.
  7. Extra credit (4 points): change rendering so that once the ball is on the ground, it breaks into several triangles. Just show the triangles on the ground, you don't need to show the breaking process.

CSci 1301 course web site.