On this page:
Spot the Difference
Raining Apples
Before You Go...
Stretch Goals
6.6

Lab 5 Lists and Abstraction

lab!

Purpose: The purpose of this lab is to you some hands-on experience with lists of structures, structures of lists, and abstracting similarities.

Textbook references: Chapter 14: Similarities Everywhere, Chapter 15: Designing Abstractions

Spot the Difference

Exercise 1 Consider the following two function definitions:
; matching-x-posn : [List-of Posn] Number Posn -> Posn
; Find the first Posn in the list with the given x-coordinate or return the given Posn
; if no such position can be found
(check-expect (matching-x-posn empty 10 (make-posn 0 0)) (make-posn 0 0))
(check-expect
 (matching-x-posn
  (cons (make-posn 1 2) (cons (make-posn 3 4) empty)) 3 (make-posn 5 6))
 (make-posn 3 4))
(define (matching-x-posn lop desired-x default)
  (cond [(empty? lop) default]
        [(cons? lop)
         (if (= (posn-x (first lop)) desired-x)
             (first lop)
             (matching-x-posn (rest lop) desired-x default))]))
 
; string-with-length : [List-of String] Nat -> String
; Returns the first String in the given list with the given length or "no such string" if no
; such string can be found
(check-expect (string-with-length empty 10) "no such string")
(check-expect (string-with-length (cons "hi" (cons "hello" (cons "aloha" empty))) 5) "hello")
(define (string-with-length los desired-length)
  (cond [(empty? los) "no such string"]
        [(cons? los)
         (if (= (string-length (first los)) desired-length)
             (first los)
             (string-with-length (rest los) desired-length))]))
Design the function find-first-match which abstracts these two functions. Be sure to redefine matching-x-posn and string-with-length using your abstraction.

Exercise 2 Consider the following two data definitions:
; A MaybeString is one of:
; - #false
; - String
 
; A MaybePosn is one of:
; - #false
; - Posn
Design a data definition which abstracts these two definitions. Redefine MaybeString and MaybePosn using your abstraction.

Raining Apples

Autumn is here and the apples are ready to eat! Johnny Appleseed needs your help collecting the apples, which are so ripe that they are falling off the trees! In this lab we will design a program where apples fall from the sky. Every tick there is a possibility that a new apple will be added at the top of the screen with a random x-coordinate and a random velocity. The apples then fall to the ground where the player tries to catch them in their basket. You can move the basket by moving the mouse around the screen. The x-coordinate of the basket should always be the same as the x-coordinate of the mouse. The player’s score will be the number of apples they have collected in the basket.

Recall that the steps we take to design world programs break with our usual convention of top down programming because the larger program is at the bottom instead of at the top. However, you should continue to design your individual functions using top down programming as much as possible.

Step 1: What stays the same?

Exercise 3 Define some constants to represent the things in the world that never change.

Step 2: What changes?

Exercise 4 Design a data definition for Apples. An apple has an x-coordinate, a y-coordinate, and a y-velocity. It does not move horizontally. Be sure to follow all the steps of the data design recipe!

Exercise 5 Design a data definition for a Game. A Game needs to keep track of the apples falling from the sky, as well as the x-coordinate of the player’s basket, and their score so far. Be sure to follow all the steps of the data design recipe!

Step 3: Which handlers do we need?

Exercise 6 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: Design your handlers

Exercise 7 Design the function that draws the game. Remember that you need to display the falling apples, the basket at the bottom of the screen, and the score somewhere on the screen (we recommend the top left corner, but it’s up to you).

Exercise 8 Design the function that changes the game when the clock ticks. Your function will need to make the following changes to the game:
  • Remove any apples that have fallen off the bottom of the screen, since clearly the player will not be able to catch them in the basket.

  • Remove any apples that have landed in the basket and add their count to the player’s score so far.

  • Move the remaining apples down the screen based on their velocity.

  • Randomly decide whether or not to add a new apple at a random x-coordinate at the top of the screen.

Exercise 9 Design the function that changes the game when the player moves the mouse. This function should simply update the x-coordinate of the player’s basket to be the same as the x-coordinate of the mouse.

Step 5: Put it all together!

Exercise 10 Design the function apple-game which, given the initial x-coordinate of the player’s basket, starts the apple game. The function should return the score of the player. Give it a try!

Before You Go...

If you had trouble finishing any of the exercises in the lab or homework, or just feel like you’re struggling with any of the class material, please feel free to come to office hours and talk to a TA or tutor for additional assistance.

Stretch Goals

Exercise 11 Add gravity to your game. The apples’ velocities should increase by 0.35 every time the clock ticks. (The number we are using here is smaller than gravitational acceleration because by default the clock ticks 28 times per second).

Exercise 12 Update your game so that it only runs for a certain amount of time, after which the game ends and the player’s score is displayed.

Exercise 13 Update your game so that there are two kinds of apples: good apples, and bad apples. Catching good apples increases your score, but catching bad apples decreases it.