### Problem Set 3h

This is the honors version of Problem Set 3; if you’re looking for the normal version, click here!

Programming Language BSL

Purpose The purpose of this problem set is to internalize design skills. Topically, the problem set focuses on functions that process structures and arbitrary unions of data.

Finger Exercises HtDP/2e: 65, 67, 68, 69, 70, 71, 77, 78, 79, 86, 94, 102, 104

Problem 1 Consider the following structure definitions:
 (define-struct lecture-hall (number capacity)) (define-struct automobile (year make model)) (define-struct football-player (name position number)) (define-struct shirt (material size color))

1. What are the names of the constructors and the selectors that each of the structures adds to Racket? Draw box representations for each of these structures.

2. Provide data definitions for the structure definitions above. Make appropriate assumptions about what data goes with which field.

3. Develop templates for functions that consume the structures above.

Problem 2 Here is a data definition for measuring time:
 (define-struct time (hours minutes)) ; A Time is a structure: ;    (make-time Number Number) ; interpretation: (make-time h m) is the time ; expressed in hours, and minutes ; Constraints: ; – hours is always between 0 and 11 ; – minutes is always between 0 and 59

1. Design the function tock, which adds one minute to the given time.

2. Design the function time->text, which converts a time to a text. The text should look like the display of a common alarm clock, i.e., it should separate the minutes from the hours with a colon. Hint: a text is an image, not a string, but you will need a string version of the time, too. See the HelpDesk for more on the text function.

3. After you have developed these functions, add a main function, which launches a big-bang program.

Problem 3 You have been hired by the local psychology department to assist with the software for a perception experiment. The lab director would like you to design a world program that records a specified number of key strokes with string representation of length 1. That is, the experimenter launches the program on some natural number n and when the “subject”Psychologists use the word “subject” for the victims that participate in their experiments. has struck n keys (with good representations), the program returns the string that is made up of these keys.

The program should draw the current string as a red text with a 40-point font size on a 200 x 50 gray background. For example, if the subject has hit "h", "a", and "t", the program shows

Challenge You can’t trust psychologists with computers. Design a checked version of the program that rejects all inputs other than natural numbers.

Problem 4 : Cowabunga!

In many parts of the world, UFOs have been abducting cows. This should not surprise you, as you are a UFO pilot who has been sent on just such a mission. As you have probably been warned, however, the base of your UFO is made of a special alloy that cannot be allowed to come in contact with the ground. Your task, therefore, is to land your UFO atop a cow without crashing on Earth... which, as you may have heard, can turn you into a participant in certain "experiments".

How the game works:

A cow is placed at an initial position on the bottom of the screen. The cow moves left at a fixed speed until hitting the left side of the screen at which point it changes direction and moves to the right until hitting the right side, and so on.

A UFO is placed at an initial position at the top of the screen. The UFO moves downward at a fixed rate. The UFO can be moved left/right by pressing the left/right arrow keys.

The game is over when the UFO hits the ground or the cow. The game should indicate in some way whether the game was won (UFO hits cow) or lost (UFO hits ground).

Cows and UFOs

• Design a data definition for UFOs and Cows.

• A UFO has an X and a Y coordinate.

• A Cow has an X coordinate and is either going left or right.

• Design a function that moves a UFO down by some fixed amount.

• Design a function that moves a UFO either left or right by some fixed amount.

• Design a function that moves a Cow in the direction it is going by some fixed amount.

• Design a function that determines if a Cow is at the edge of the screen.

• Design a function that flips the direction of a Cow.

• Design a function that moves a Cow in the direction it is going, unless it at the edge, in which case, the Cow should flip directions, and then move.

• Design a function that determines if a UFO has captured a Cow.

• Design a function that determines if a UFO has crashed on the ground.

• Design a function that determines if the game is over.

Animation system

• Design a data definition for Worlds. A World has a UFO and a Cow in it.

• Design a function that handles key events (maybe moving the UFO left / right).

• Design a function that renders the World as a Scene.

• Design a function that "ticks" the world (moves the UFO down and moves the Cow).

• Use the big-bang system to start the game.

Enjoy!