### Homework 4

Due Date Tuesday May 19, 10pm Eastern

Purpose To practice using union data and self-referential data.

You must submit a single .rkt file containing your responses to all exercises via the Handin Server. We accept no email submissions.

You must use the language specified at the top of this page.

Your code must conform to the guidelines outlined in the style guide on the course website. The style guide will be updated as the semester progresses, so revisit it before submitting each assignment.

Unless otherwise stated, for all programming problems you must provide (i) a signature, (ii) a purpose statement, (iii) sufficiently many check-expects (not for big-bang programs), and (iv) the actual code, in the language specified at the top of this page.

Be sure to look at the feedback for previous assignments before submitting, as we will be grading you more harshly on things we have warned you about before.

Failure to comply with these expectations will result in deductions and possibly a 0 score.

Exercise 1 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.

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).

You will need the following Data Definitions:

A UFO has an X and a Y coordinate.

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

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

Big-bang functions needed:

Design a main function that uses big-bang to start the game.

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).

Design a function that determines if the game is over.

Design a function that draws the final scene.

Helper Functions you may need:

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 is 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.

Exercise 2 Below is a data definition for a class of shapes.

; A Shape is one of: ; - Circle ; - Square ; - Rectangle (define-struct circl (x y r outline c)) ; A Circle is a (make-circl Number Number Number Boolean Symbol) ; interpretation: x and y determine the center of the circle, ; r the radius, outline whether it's outlined or solid, ; and c its color (define-struct squar (x y size outline c)) ; A Square is a (make-squar Number Number Number Boolean Symbol) ; interpretation: Supply a good interpretation of Square. (define-struct recta (x y width height outline c)) ; A Rectangle is a (make-recta Number Number Number Number Boolean Symbol) ; interpretation: Supply a good interpretation of Rectangle.

Add an interpretation for the Square and Rectangle definitions. Both represent shapes whose borders are parallel to the borders of a canvas (window).

Develop the template for functions that consume Shapes.

Use the template to design shape-shift-x. The function consumes a Shape, sh, and a number, delta. It produces a Shape that is like sh but shifted by delta pixels along the x-axis.

Use the template to design shape-draw. The function consumes a Shape, sh and an Image, bg and draws sh onto bg.

Exercise 3 A ShapeStack is a data definition for a collection of Shapes.

; A ShapeStack is one of: ; - Shape ; - (make-stack Shape ShapeStack) (define-struct stack (top foundation))

Design a function to shift all of the Shapes in a ShapeStack by a given delta along the x-axis.

Design a function to draw all of the Shapes in a ShapeStack stacked on top of each other. For instance, a ShapeStack containing a red circle, a blue square and a green rectangle might be rendered as shown below:

Exercise 4 Design a function eliminate, that takes a list of numbers lon and a number n, and constructs a new list of numbers with all the same values as lon except for any numbers equal to n. For instance,

(check-expect (eliminate (cons 5 (cons 4 (cons 5 (cons 2 '())))) 5) (cons 4 (cons 2 '()))) Note: this is essentially remove-all, simplified to lists of only numbers. We have not addressed how equal? works yet, so the full flexibility of remove-all doesn’t yet make sense. Also – do not use remove-all to implement this function!

Challenge (optional): Design a function no-dups, that takes a List of Numbers, and constructs a new list with any duplicate values removed (and keeping just a single one of the copies). For example,

Exercise 5 We know that cons allows us to add a number to the front of a list of numbers. Design its opposite, snoc, that allows us to add a number onto the end of a list of numbers.