# CS 5010: Problem Set 04: Working with Lists

Out: Monday, February 8, 2016

Due: Monday, February 15, 2016 at 5pm EST

Corrected: Tuesday, February 9 (to correct references to dx and dy, which should have been vx and vy)

The goal of this problem set is to help you design functions that deal with lists, and with the Iterative Design Recipe.

You must use the HtDP Beginning Student Language to solve the problems.

For these problems, download a copy of extras.rkt and put it in the folder with your solutions. Then import this library by including the line

```(require "extras.rkt")
```
at the top of your file with the other requires. Then, for each problem, put in lines that say
```(provide function)
```
for each deliverable function, as you have done on previous problem sets. This will allow our testing framework to import your file and do automated testing on it.

Remember that you must follow the design recipe. Your deliverables include the data definitions (including interpretation and templates), contract and purpose header, code, and tests. Be sure to sync your work and fill out a Work Session Report at the end of every work session. Use the Work Session Report for PS04.

Note: For all universe programs, you may assume the mouse is never dragged or moved outside of the canvas. Once the mouse enters the canvas, if the mouse ever leaves the canvas, then the behavior of your system is unspecified.

1. (screensaver-3). The physics marketing department at a nearby university wants you to improve the screensaver you wrote for problem 2 of Problem Set 03 in these ways:

• The vx and vy components of a Goofball's velocity are still constrained to be integers, and are still expressed in cm per tick, but they are no longer constrained to have absolute value less than or equal to 1.
• Instead of having two goofballs on screen at all times, your improved screensaver will start with just one Goofball, initially centered at position (150,0) in Goofball coordinates with a southeastern velocity of (+2,+3).
• New goofballs can be created by pressing the "n" key. The first new goofball to be created will be centered at position (100,0). The next new goofball will be centered at position (50,0), the one after that at (0,0), and so on with each newly created goofball appearing 50 cm to the left of the initial position of the goofball created before it, until a new goofball has been created at position (-150,0). After that, the next seven new goofballs will be created at the same seven positions starting at (150,0) and going westward. This sequence of seven positions is to be repeated as many times as necessary to accomodate newly created goofballs.
• All newly created goofballs are initially unselected and have an initial velocity of (+2,+3) cm per tick, which is a little south of southeast.
• The "d" key deletes all selected goofballs and removes them from the screensaver.
• The "1" and "2" keys no longer do anything.
• The right arrow key (`"right"`) increases the vx component of all selected goofballs by 1 cm per tick. The left arrow key (`"left"`) decreases the vx component of all selected goofballs by 1 cm per tick. The down arrow key (`"down"`) increases the vy component of all selected goofballs by 1 cm per tick. The up arrow key (`"up"`) decreases the vy component of all selected goofballs by 1 cm per tick.
• Goofballs still rebound from the arena walls via a perfect bounce as defined in Problem Set 02.
Hint: One tick's worth of motion for a goofball moving at velocity (vx,vy) can be calculated by computing its horizontal movements independently of its vertical movements. That allows the net effect of a perfect bounce to be calculated using integer arithmetic.
• Goofballs remain selectable and draggable as in problem 2 of Problem Set 03 (screensaver-2).

The screensaver is paused at first, as in Problem Set 03. The situation shown in the following image was created by hitting the "n" key four times to create new goofballs, selecting the leftmost goofball and hitting the "left" and "up" arrow keys ten times each, and selecting the second goofball from the left while hitting the "right" and "down" arrow keys ten times each:

Unpausing the world shown above results in this motion:

You are to deliver a file named screensaver-3.rkt that provides the functions listed below. Note that `screensaver`, `initial-world`, `world-after-tick`, `world-after-key-event`, and `world-after-mouse-event` implicitly refer to the specification above, so they behave differently in screensaver-3 than in screensaver-2 even though their contracts and purpose statements read the same as before.

```;;; screensaver : PosReal -> WorldState
;;; GIVEN: the speed of the screensaver, in seconds per tick
;;;     (so larger numbers run slower)
;;; EFFECT: runs the screensaver, starting with the initial state as
;;;     specified above
;;; RETURNS: the final state of the world
;;; EXAMPLES:
;;;     (screensaver 1) runs the screensaver at normal speed
;;;     (screensaver 1/4) runs at a faster than normal speed

;;; initial-world : Any -> WorldState
;;; GIVEN: any value (ignored)
;;; RETURNS: the initial world specified for the screensaver
;;; EXAMPLE: (initial-world -174)

;;; world-after-tick : WorldState -> WorldState
;;; GIVEN: any WorldState that's possible for the screensaver
;;; RETURNS: the WorldState that should follow the given WorldState
;;;     after a tick

;;; world-after-key-event : WorldState KeyEvent -> WorldState
;;; GIVEN: a WorldState and a KeyEvent
;;; RETURNS: the WorldState that should follow the given WorldState
;;;     after the given KeyEvent

;;; world-after-mouse-event
;;;  : WorldState Int Int MouseEvent -> WorldState
;;;
;;; GIVEN: A World, the x- and y-coordinates of a mouse event, and the
;;;     mouse event
;;; RETURNS: the world that should follow the given world after the given
;;;     mouse event.

;;; world-goofballs : WorldState -> ListOfGoofball
;;; GIVEN: a WorldState
;;; RETURNS: a list of the goofballs present in the WorldState
;;;     (without goofballs that have been deleted using the "d" key)

;;; world-paused? : WorldState -> Boolean
;;; GIVEN: a WorldState
;;; RETURNS: true iff the WorldState is paused

;;; goofball-selected? : Goofball -> Boolean
;;; GIVEN: a Goofball
;;; RETURNS: true iff the given Goofball is selected

;;; goofball-radius : Goofball -> Integer
;;; GIVEN: a Goofball
;;; RETURNS: its radius, in cm

;;; goofball-x : Goofball -> Integer
;;; goofball-y : Goofball -> Integer
;;; GIVEN: a Goofball
;;; RETURNS: its corresponding x or y coordinate, in cm

;;; goofball-vx : Goofball -> Integer
;;; goofball-vy : Goofball -> Integer
;;; GIVEN: a Goofball
;;; RETURNS: its corresponding vx or vy velocity component, in cm/tick
```
2. (screensaver-4). The physics marketing department thinks your screensaver would be more exciting if goofballs occasionally blew up, and the physicists are afraid that's going to happen in their laboratories anyway, so your Screensaver 4.0 adds this new feature:

• If the centers of two or more Perpetual Goofballs ever coincide at the beginning or end of a tick, then all goofballs centered at that position blow up and are immediately removed from the WorldState.
• The screensaver should provide some suitably graphic indication of the site of the explosion, with the graphics limited to a 30cm radius around the center of the explosion, to persist no less than 10 ticks and no more than 100 ticks.

You are to deliver a file named screensaver-4.rkt that adds this behavior to the `screensaver` and `world-after-tick` functions. You should `provide` the same thirteen functions you provided for screensaver-3.