On this page:
New Partners!
Understanding Abstractions
All Hands On Duck
Before You Go...
Stretch Goals
6.6

Lab 6 Using Abstractions

lab!

Purpose: This lab will give you practice using existing list abstractions.

Textbook references: Chapter 16: Using Abstractions

New Partners!

Goals: Get a new lab partner. Make new friends, but keep the old.

As you walk into the lab you should see a list on the board of partnerships. Please find your assigned partner and sit with them. This person is your partner for the next 2 weeks. Say hello! Ask them their favorite color! Tell them something wonderful that happened to you this week!

Exercise 1 Exchange contact information with your partner. You will need to get together a lot so you are going to need to be able to talk to each other. Please give them a method of communicating with you that you will check regularly (as in, don’t just give them your email if you never check your email).

Exercise 2 Arrange a time to start working on homework 7 with your partner. It’s due on Friday so you are going to have to do it soon. It would be a good idea to establish a time to work on future homeworks as well, if you can.

Exercise 3 Re-read the policy page to refresh your memory on what to do if you are having problems with your partner. It is very important that you tell us if you are struggling to work with your partner. Otherwise we can’t help you resolve the problem.

Understanding Abstractions

Goals: Understand the signatures and purposes of each pre-defined list abstraction.

Exercise 4 Determine the best pre-defined list abstraction to use for each of the following tasks. Have a staff member look over your answers before you continue.
  • Create a list of the string-lengths of every string in a list.

  • Determine whether any Posn in a list is within a certain area.

  • Given a list of strings, create a list of only the strings that contain only numbers.

  • Check whether every number in a list is within a certain range.

  • Given a list of Posns, create a single Posn whose x-coordinate is the sum of all the x-coordinates in the list and whose y-coordinate is the sum of all the y-coordinates in the list.

Exercise 5 Design the function accumulate-posn which, given a list of Posns, creates a single Posn whose x-coordinate is the sum of all the x-coordinates in the list, and whose y-coordinate is the sum of all the y-coordinates in the list (this is the last example from the previous exercise).

Exercise 6 Determine the signature of the following function:
(define (f x y z)
  (or (andmap string-numeric? (map x (filter positive? (foldr append empty y)))) z))

All Hands On Duck

Goals: Use list abstractions to design a fun world program!

When one of your friends was finished playing with her extensive collection of rubber duckies, she pulled out the bathtub drain and watched as each duck swirled closer to the drain and eventually disappeared down it. "I could make big-bang do that!" she thought excitedly. But she needs your help to write the program.

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 7 Define some constants to represent what stays the same in the world. For example you may want to find images online of a cute rubber duck and a menacing whirlpool as those are both images that you will need to use to draw your program. What other constants can we define?

Step 2: What changes?

Exercise 8 Develop a data definition for your program. You will need to keep track of an arbitrary number of ducks, each with an x and y position on the screen.

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

Exercise 10 Design the function that draws the ducks on the whirlpool scene. Remember to use pre-defined list abstractions whenever you can!

The function that moves the ducks as the clock ticks is a bit more complicated, so we will break it down into several smaller functions in the following exercises.

Your friend remembered enough about fluid dynamics to produce the following functions:

; go-with-the-flow : Posn -> Posn
; produces a new posn that is the next point on a
; roundabout path to the CENTER
(check-expect
 (posn-within? (go-with-the-flow (make-posn 100 200)) (make-posn 101.96 199.61) 0.01)
 true)
(check-expect
 (posn-within? (go-with-the-flow (make-posn 321 123)) (make-posn 322.75 123.97) 0.01)
 true)
(define (go-with-the-flow p)
  (if (and (= (posn-x p) (posn-x CENTER))
           (= (posn-y p) (posn-y CENTER)))
      CENTER
      (make-posn (+ (posn-x p) (* SPEED (cos (find-angle (- (posn-x p) (posn-x CENTER))
                                                         (- (posn-y p) (posn-y CENTER))))))
                 (+ (posn-y p) (* SPEED (sin (find-angle (- (posn-x p) (posn-x CENTER))
                                                         (- (posn-y p) (posn-y CENTER)))))))))
 
; posn-within? : Posn Posn Number -> Boolean
; Are the coordinates of these positions within delta of each other?
(check-expect (posn-within? (make-posn 1 2) (make-posn 2 1) 3) true)
(check-expect (posn-within? (make-posn 100 0) (make-posn 100 0.5) 0.1) false)
(define (posn-within? p1 p2 delta)
  (and (<= (abs (- (posn-x p1) (posn-x p2))) delta)
       (<= (abs (- (posn-y p1) (posn-y p2))) delta)))
 
; find-angle : Number Number -> Number
; Find the angle to travel at when moving from one position to another
(check-within (find-angle 50 2) 2.3962 0.001)
(check-within (find-angle 1 100) -2.3662 0.001)
(define (find-angle dx dy)
  (atan (- dx dy) (- 0 dx dy)))

As you can see, your friend is using several constants which you will have to define in order to get the program to work correctly.

Exercise 11 Design the function move-all-ducks which takes in your worldstate and produces a new worldstate where the ducks have moved one step closer to the drain. The go-with-the-flow function will be useful to you here. Which list abstraction does the same thing to every element of a list?

Exercise 12 Design the function safe-from-drain which takes in your worldstate and produces a new worldstate with only the ducks that are not at the same position as the drain. Which list abstraction can remove elements from a list?

Exercise 13 Design the function replenish-ducks that takes in your worldstate and a NaturalNumber. If there are already at least that many ducks, the function does nothing, but if not it adds more ducks until there are the given number of ducks. These new ducks should be placed at a random position anywhere on the screen.

Exercise 14 Design the function update-ducks which uses move-all-ducks, safe-from-drain, and replenish-ducks to move the ducks, remove any ducks that have gone down the drain, and replenish the world to have the same number of ducks as it did originally. This function will be your on-tick handler.

Step 5: Put it all together!

Exercise 15 Design a function that uses big-bang to create your program, inserting the functions you defined above into the appropriate clauses. Your program should take as input the numbler of ducks to initially display on the screen.

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

In this section we will design a game of tic tac toe. In this game two players take turns placing their markers on a 3x3 board. A player wins if they get three markers in a row, either horizontally, vertically, or diagonally.

Step 1: What stays the same?

Exercise 16 Define some constants to represent what stays the same in your tic tac toe game.

Step 2: What changes?

Consider the following data definitions:
; A Tile is one of:
; - false (no one has played here yet)
; - "x" (player 1 has played here)
; - "o" (player 2 has played here)
 
(define-struct ttt [player1? all-tiles])
; A TTTGame is a (make-ttt Boolean [List-of [List-of Tile]])
; - where player1? is true if it is player 1's turn
; - and all-tiles is the list of tiles on the board so far

Exercise 17 Complete the steps of the data design recipe for the above data definitions.

Step 3: Which handlers do we need?

Exercise 18 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 19 Design the function that draws the tic tac toe game. Remember to use list abstractions whenever possible!

Exercise 20 Design the function that handles mouse events. When a player clicks you will need to check whether they have clicked an empty square. If they have, you should add their token to that square on the board. You may want to look at the documentation for list-ref, which will come in handy here. One thing to note is that you may need several list processing functions, but you may not necessarily be able to use list abstractions for this problem. If you’re not sure why that is, feel free to ask a staff member.

Exercise 21 Design the function which determines if the game is over. The game is over if one of the players has three tokens in a row either horizontally, vertically, or diagonally. The game is also over if all the tiles are filled up but nobody has won.

Exercise 22 Design the function which displays the name of the winner at the end of the game. If neither player won you should display text that says "It was a tie!"

Step 5: Put it all together!

Exercise 23 Design a function that uses big-bang to create your game, inserting the functions you defined above into the appropriate clauses.