CSC 160 Homework 4

Assigned 24 Feb, due 15 Mar

For all the programming assignments, be sure to follow the design recipe. Write your function contract, examples, and function definition in the Definitions Window, save it to a file, and send me this file. Also test your program: since you've already included examples in the Definitions window, you should be able to hit the Execute button and see all the results (along with what you said they "should be"). Save the resulting Interactions window to a text file and send me this file too. Be sure to choose meaningful names for functions and parameters, and watch for opportunities to re-use functions you (or the textbook) have already written.

Also turn in a log of how many errors of different kinds you encountered in the assignment, with brief comments describing each one ("mismatched parentheses" is self-explanatory, but more complex errors might need more description).

This assignment is to be done in pairs, just like previous homeworks (but with a different partner).

The assignment

Building an animation with random colored dots

  1. There is a predefined Scheme function named random that produces random numbers. Read about it in the DrScheme Help Desk. Try some examples in the Interactions window. Develop a function named random-posn that takes in two numbers (max-x and max-y) and returns a posn whose x coordinate is chosen at random between 0 and max-x, and whose y coordinate is chosen and random between 0 and max-y. Note: In your examples, you won't be able to predict exactly what answer comes out, but you can say something like "should be a posn with 0 ≤ x < 20 and 0 ≤ y < 50".
  2. Develop a function named add-colored-dot which takes a posn, a color, and an image, and returns the same image with a dot of the specified color added at the specified location.

  3. Develop a function named add-smart-dot which takes a posn and an image, and returns the same image with either a green dot or a red dot added at the specified position; the dot should be green if the position is above the main diagonal, and red otherwise. You may assume that the image is square.

  4. Develop a function named add-random-smart-dot which takes an image and returns the same image with a randomly chosen dot added to it, either green or red depending on whether it's above the main diagonal as above. You may assume that the image is square, but you do not know in advance how large it is.

  5. Run an animation which uses an image as its world (so the show-world function is incredibly easy); it starts with a box, and at each tick of the clock, it updates the world using add-random-smart-dot. Turn in not only the Scheme code you used to run the animation, but also a screen shot showing the animation after at least twenty or thirty clock ticks.

Building another animation with a customized world structure

  1. Define a structure named moving-posn which contains a posn and a symbol (either 'right, 'left, 'up, 'down, or 'not-moving).
    Write contracts for all the functions that "come for free" with the structure definition.
    Write examples (with correct answers) of using each of the functions that "come for free" with the structure definition.
    Test these function calls to confirm that they all behave as you expect.
    Note you do not need to write the functions themselves (because they "come for free"), but you do need to demonstrate that you know how to use them.
  2. Develop an animation with a moving-posn as the world, representing the current position and direction of motion of a colored dot on the screen. Every time the clock ticks, the dot moves a few pixels in the current direction. Every time the user presses an arrow key, the current direction (but not the location) changes ('left for the left arrow key, 'up for the up arrow key, etc. If the user presses the 'home key, the direction should change to 'not-moving.

Grading standards

Error log:       /30

Function name Contract Examples Definition Test results
random-posn /5 /5 /10 /5
add-colored-dot /5 /5 /10 /5
add-smart-dot /5 /5 /10 /5
add-random-smart-dot /5 /5 /10 /5
animation /30
moving-posn struct /10 /10 /10 /10
auxiliaries for animation /10 /10 /20 /10
animation /30

General skills:

Following directions /20
Writing contracts from word problems /20
Choosing examples /20
Choosing names /20
Coding /20
Code re-use and choice of auxiliaries /20

Total:         /400


Last modified:
Stephen Bloch / sbloch@adelphi.edu