8.3

Homework 3

home work!

Programming Language BSL

Due Date Tuesday 9/28 at 9:00pm (Week 3)

Purpose To finish Cowabunga and integrate it with big-bang, and to practice designing programs and begin working with self-referential data.

Finger Exercises

Exercise 1 From HtDP, 97 98 99 100 123 128

Expectations
  • You should submit a single .rkt file containing your responses to all graded exercises via the Handin Server. We accept NO email submissions.

  • You are only allowed to use the language specified at the top of this page: failure to do so will result in a 0.

  • All elements of the design recipe are expected. Data definitions should have associated interpretations, examples, and templates. Functions should have signatures, purpose statements, tests, and code.

  • Note that there are few exceptions to these rules, such as main functions not being able to be tested.

  • Be sure to use helper functions when appropriate to keep code clear.

Graded Exercises

Cowabunga, Cowabunga!

Exercise 2 through 6 are a continuation of Cowabunga from Homework 2. You can reuse Cowabunga code from Homework 2. Please include it in your Homework 3 submission so we can run your game, but put it in a clearly marked section titled "FROM HOMEWORK 2" so we can ignore it when we grade.

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

Exercise 3 Design a function that handles key events (moving the UFO left / right, if it can move further left / right given its current position).

Exercise 4 Design a function that renders the World as an image. Feel free to use your own UFO and Cow images but try to keep them under 150x150 pixels. (The Handin server has a file-size limit of 5MB and large images in your file might put you over 5MB which means you won’t be able to submit.)

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

Exercise 6 Use the big-bang system to start the game. The game should stop when the UFO has either crashed or captured the cow. (You may want to use your solution to Exercise 20 on Homework 2, or a modified version of it.)

Note: You may slightly modify your code from last week to redo crashing or capturing calculations to get better visual rendering when you play the game—this might or might not be necessary depending on the size of your cow and UFO images.

Guess My Number

Exercise 7 Design a program which, when launched, randomly generates a number n from 0 to 9. On display should be the word "Guess!". Then, once a person guesses a number, it should display that number as well as either "Nope, higher.", "Nope, lower.", or "You've won!" based on whether n is higher, lower, or equal to the number guessed. The program should end when the correct guess is made and output the number of guesses it took to find the correct number.

Be sure to define your data and follow the design recipe all the way through!

Note: when a function input is ignored, it is called _ by convention. Also, its type in the signature is Any, since we accept any kind of data. Any will be formalized later on in the semester; don’t worry about defining it for now.

Hint: Sometimes, when building a program, it is easier to build a slightly simpler program that is easier to debug. One may want to initially design the program so that the number to be guessed is given to the function as opposed to being randomly generated. Then, once they are sure this version of the program works, modify it to randomly generate the number to be guessed.

You call that a Pizza?

; A Pizza is one of:
; - "red"
; - "no"
; - (make-topping String Pizza)
(define-struct topping [name more])
; and represents a collection of toppings on a pizza

Exercise 8 Define examples of and design the template for a Pizza.

Exercise 9 Design a function which describes a pizza. A pizza with just red sauce should be described as "This pizza has red sauce", and likewise a white pizza (i.e. no sauce) would be described as "This pizza has no sauce". A red-sauce pizza with olives (as the outermost topping) and cheese should be described as "This pizza has olives and cheese and red sauce".

The Tower Of Terror

; A Building is one of:
; - "ground"
; - (make-story Number PosInt String Building)
(define-struct story [height rooms color below])
; and represents either the ground story,
; or a story with a height, number of rooms, color, and
;   the rest of the building beneath it

Exercise 10 Define examples of and design the template for a Building.

Exercise 11 Design a function which counts the total number of rooms in a Building.

Exercise 12 Design a function which draws a building. The stories should be of a fixed width, be their own color, and stacked on top of each other. Each story should also consist of horizontally arranged, identically sized rooms, all of which are surrounded by a frame to differentiate between them. The ground story can be represented by the empty-image.

You may find the following data definition and template helpful, and keep in mind all positive integers are instances of natural numbers:

; A Nat is one of:
; - 0
; - (add1 Nat)
 
; nat-temp : Nat -> ?
(define (nat-temp n)
  (cond [(zero? n) ...]
        [(positive? n) (... (nat-temp (sub1 n)))]))