Lab 3 Structure types; World programs with structures
Purpose: to review structure type definitions, and to practice their use, especially in the context of World programs.
Posns
Consider this data design for a Position:
; A Position is a (make-posn Number Number) ; Int.: the x and y coordinates of a point in 2d space (define pos-0 (make-posn 0 0)) (define pos-1 (make-posn 1 -3)) (define pos-2 (make-posn 1.5 pi)) (define (position-templ p) (... (posn-x p) ... (posn-y p) ...))
Exercise 1 Can you explain the difference between a Position and a (make-posn Number Number)?
Exercise 2 Design the function pos-adder which takes two Positions and produces a single Position whose x-coordinate is the sum of the two x-coordinates and whose y-coordinate is the sum of the two y-coordinates. Be sure to follow all the steps of the design recipe for functions.
Exercise 3 Below is a data definition for a book. What is the problem with this data definition?
Custom Structures
Exercise 4 The Franklin Park Zoo wants to keep better track of their animals but they need your help! Each animal at the zoo has a name, a species, an indicator of whether they are kept outside or inside, and a diet which can be either herbivorous, carnivorous, or omnivorous. Design appropriate data definitions to describe an animal at the zoo. Be sure to follow all the steps of the design recipe for data.
Consider the following data definitions:
(define-struct address [num st city us-state zip]) ; An Address is a (make-address Nat String String String Nat) where ; - num is the number of the building on the street ; - st is the name of the street ; - city is the city the building is in ; - us-state is the state the city is in, and ; - zip is the zipcode of the building (define-struct student [first last nuid local perm]) ; An NEUStudent is a ; (make-student String String PositiveNumber Address Address) where ; - first is the student's first name ; - last is the student's last name ; - nuid is the student's NUID # ; - local is the student's local address, and ; - perm is the student's permanent address
Exercise 5 Complete the data design recipe for the above data definitions.
Exercise 6 Design the function student-email which takes an NEUStudent and produces a string representing that student’s email address. For simplicity we will say that a student’s email address has the form,
"<lastname>.<first-initial>@husky.neu.edu"
The last name and the first-name initial in the email address are all lowercase.
Exercise 7 Design the function new-zipcode which takes an NEUStudent and a number, representing the new zip code of the person after a move, and updates their permanent address to have that zip code. Be sure to follow the template!
Cookie Clicker
In the remaining time we will work on designing a small program which allows you to play an advanced version of cookie clicker. The program should display a black background with a cookie at some given initial location and then when the user clicks, the cookie should appear at the mouse’s location. If a user clicks within the area of a cookie, then the number of cookies they have earned increments. You can see an example of a simpler cookie clicker here. To do this, we will follow the same steps for designing programs as we did in the previous lab.
Step 1: What stays the same?
Exercise 8 Define some constants to describe what stays the same throughout the game. What images will you need in order to draw the program?
Step 2: What changes?
The only things that change over time in this program are the location of the cookie and the number virtual cookies earned. How can we best represent this? Think about the types of data you know and choose one that represents a location in 2d space. This will be your world state.
Step 3: Which handlers do we need?
Exercise 9 Write the signature and purpose statements for the handler functions you will need. Recall that big-bang always requires a to-draw clause. Which other clauses will you need based on the program description? If you are having trouble take a look at the documentation, and if you are still confused, ask a tutor or TA for assistance.
Step 4: Main Function
Now that we’ve decided on our handler functions, we can put together our main function which will call big-bang. Recall that we write this function first because in this course we follow a top-down programming approach. This means that, if function f uses function g, we design function f first, then function g. The BSL language allows that; it is only at the end of reading the whole file that all functions being used must be defined.
Exercise 10 Design the main function cookie-clicker which, given an initial position for the cookie, initialize our cookie count to 0, then starts up the big-bang program.
Step 5: Design your handlers
Exercise 11 Design the handlers you decided on in step 3. Hint: To help you design the function that changes your world over time, you might need a function which determines if a click is within the cookie. You might find the following function which calculates the distance between two posns, and function header for such a function helpful.
; posn-within? : Posn Posn Number -> Boolean ; Is the first posn within the radius of the second? (check-expect (posn-within? (make-posn 0 0) (make-posn 1 1) 1) #false) (check-expect (posn-within? (make-posn 0 0) (make-posn 1 1) 2) #true) (define (posn-within? p1 p2 r) ...) ; distance : Posn Posn -> Number ; Produces the distance between the two positions (check-expect (distance (make-posn 0 0) (make-posn 8 6)) 10) (check-expect (distance (make-posn 1 2) (make-posn 1 2)) 0) (define (distance p1 p2) (inexact->exact (sqrt (+ (sqr (- (posn-x p1) (posn-x p2))) (sqr (- (posn-y p1) (posn-y p2)))))))
Give your program a try! Recall that the dot should only change positions when the mouse is clicked, and not every time the mouse moves. Good luck!
Exercise 12 What if we want to grow the size of cookie over time? What needs to change in the world? Make this change to your world data definition and update your handlers.