7.0

Week 3 Set a

home work!

Programming Language BSL

Due Date Monday 9/17 at 9:00pm (Week 3)

Purpose To practice function design and prepare for the exam.

Finger Exercises

Exercise 1 From HtDP, 55 64 65 84

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

Everyday I’m Shuffling

; A CSG (CoinShuffleGame) is a (make-csg CoinOrFalse CoinOrFalse CoinOrFalse)
(define-struct csg [left middle right])
; and represents the three cups in a coin shuffle game, and what is under them
 
; A CoinOrFalse is one of:
; - #false
; - Number
; and represents either no coin or the coin's monetary value
 
; A Guess is one of:
; - "left"
; - "middle"
; - "right

Exercise 2 Provide examples and templates for the above data definitions. Recall that when data definitions refer to other complex data types, the template must reflect this by calling the appropriate template.

Exercise 3 Design a function, shuffle-right, which moves all cup values in a CSG to the right. The right cup’s contents should loop back to the left cup.

Exercise 4 Design a function, which given a CSG and a Guess, outputs the monetary value of the guessed cup. No coinage is worth 0.

Exercise 5 Design the function inflation, which given a CSG and a number, adds that monetary value to all of the coins in the cup, and leaves empty cups as is.

A Posn Aside

Recall the definition of a Posn.
; A Posn is a (make-posn Number Number)
; and represents a 2d coordinate
 
(define POSN-20 (make-posn 20 20))
 
; posn-temp : Posn -> ?
(define (posn-temp p)
  (... (posn-x p) ... (posn-y p)))

Exercise 6 Design a function which, given two Posns, makes a new Posn with their x and y values added to each other.

Drone Shoots

We are going to develop a game which simulates a drone shoot. In it, a stationary photographer begins the photo shoot on the ground, with some part of the ground as their goal to capture. They will then launch the drone and fly it. The shoot will end when the drone can capture the whole surface area of the goal... or it crashes!

In this assignment, we will begin working on some of the logic of the game, and will finish it and integrate it into big-bang next assignment.

; A DS (Drone Shoot) is one of:
; - (make-launch Number Interval)
(define-struct launch [photographer goal])
; - (make-flight Number Interval Posn)
(define-struct flight [photographer goal drone])
; Where:
;   - photographer represents a photographer's x-coordinate on the ground
;   - goal represents the range of the desired image to to be captured at the ground-level
;   - drone (if any) represents the drone's x/y position (with 0, 0 at the bottom left)
; All numbers are measured in pixels.
 
; An Interval is a (make-interval Number Number)
(define-struct interval [left right])
; and represents the leftmost and rightmost range of an interval in pixel coordinates (inclusive)

Exercise 7 Provide examples and templates for the above data definitions. Recall that when data definitions refer to other complex data types, the template must reflect this by calling the appropriate template.

Exercise 8 Design a function falling-drone which, given a DS, moves the drone (if any) down by 1 pixel.

Exercise 9 Design a function launch-drone, which given a DS without a drone in flight, puts the drone in flight at 20 pixels in the air and 15 pixels to the right of the photographer. If the drone is in flight, nothing should change.

Exercise 10 Design a function shoot-over?, which determines if the shoot is over. The shoot is over if the drone has crashed (height = 0), or if the drone has captured the goal. Assume the drone has a 53.14 degree camera, which essentially means that if the drone is y pixels above the ground, it can see the ground y/2 pixels to the left and right of its position. It does not matter if the drone captures more than the goal, so long as the whole goal is captured.