;;; banana.scm ;;; ;;; A world with gorillas throwing explosive bananas ;;; ;;; Changes: ;;; * corrected test case outputs -- Sat Feb 7 00:54:11 EST 2004 ;;; * fixed typo in comment -- Sun Feb 8 21:56:12 EST 2004 ;;; ;; Some utility definitions provided to you (define pi (acos -1)) (define degree2radian (lambda (deg) (/ (* deg pi) 180.))) ;; Problem 1 ; YOUR DOCUMENTATION HERE... ; (define position (lambda (a v u t) YOUR-CODE-HERE)) ; Test cases ;(position 0 0 0 0) ; -> 0 ;(position 0 0 10 0) ; -> 10 ;(position 0 5 5 5) ; -> 30 ;(position 1 1 1 1) ; -> 2.5 ;(position 10 10 10 10) ; -> 610 ; YOUR TEST CASES HERE... ;; Problem 2 ; Finds first root of az^2 + bz + c = 0 ; (define root1 (lambda (a b c) YOUR-CODE-HERE)) ; YOUR DOCUMENTATION HERE... ; (define root2 (lambda (a b c) YOUR-CODE-HERE)) ; YOUR DOCUMENTATION HERE... ; (define larger-root (lambda (a b c) YOUR-CODE-HERE)) ; Test cases ;(larger-root 1 0 -4) ; -> 2 ; YOUR TEST CASES HERE... ;; Problem 3 ; YOUR DOCUMENTATION HERE... ; (define flight-time (lambda (vertical-velocity elevation) YOUR-CODE-HERE)) ; Test cases ;(flight-time 0 0) ; -> 0 ;(flight-time 0 100) ; -> approx 4.5 ;(flight-time 100 0) ; -> approx 20.4 ;(flight-time 100 100) ; -> ; YOUR TEST CASES HERE... ;; Problem 4 ; YOUR DOCUMENTATION HERE... ; (define distance (lambda (angle velocity elevation) YOUR-CODE-HERE)) ; Test cases -- (distance angle velocity elevation) ;(distance 45 0 100) ; -> 0 ;(distance 0 10 0) ; -> 0 ;(distance 90 10 0) ; -> 0 ;(distance 45 10 0) ; -> approx 10.2 ;(distance 30 10 0) ; -> approx 8.8 ;(distance 90 10 100) ; -> 0 ;(distance 45 10 100) ;(distance 30 10 100) ; YOUR TEST CASES HERE... ;; Problem 5 ; YOUR DOCUMENTATION HERE... ; (define hit? (lambda (angle velocity elevation burst-radius target-distance) YOUR-CODE-HERE)) ; Tests ;(distance 45 10 0) ; -> approx 10.2 ;(hit? 45 10 0 1 10) ; -> #t ;(hit? 45 10 0 1 50) ; -> #f ; YOUR TEST CASES HERE... ; game ; ; With your procedures above, we have enough to play the game! ; ; Note that this procedure uses some gory Scheme internally ; that you are not yet expected to understand. Treat this as ; a black box and don't worry about the internal details. ; (define play (lambda (separation burst-radius height1 height2) (display "G1 (") (display height1) (display "m high)") (display " --- ") (display separation) (display "m --- G2 (") (display height2) (display "m high)" ) (newline) (let* ((angle1 (prompt-for-command-expression "Player 1 Angle: ")) (velocity1 (prompt-for-command-expression "Player 1 Velocity: ")) (angle2 (prompt-for-command-expression "Player 2 Angle: ")) (velocity2 (prompt-for-command-expression "Player 2 Velocity: "))) (cond ((hit? angle1 velocity1 height1 burst-radius separation) (display "Player 2 destroyed!") (newline)) ((hit? angle2 velocity2 height2 burst-radius separation) (display "Player 1 destroyed!") (newline)) (else (newline) ; Feedback to player 1 (display "Player 1's banana went ") (display (distance angle1 velocity1 height1)) (display "m") (newline) ; Feedback to player 2 (display "Player 2's banana went ") (display (distance angle2 velocity2 height2)) (display "m") (newline) ; Loop (play separation burst-radius height1 height2)))))) ; Try it out by evaluating the following: (play 10 1 3 2) ; Enter guesses (in *scheme* buffer) followed by ctrl-x ctrl-e ; Use ctrl-c ctrl-c to terminate. ;; Problem 6 ; YOUR DOCUMENTATION HERE... ; (define find-angle (lambda (velocity elevation burst-radius target-distance) (find-angle-helper 0 velocity elevation burst-radius target-distance))) ; YOUR DOCUMENTATION HERE... ; (define find-angle-helper (lambda (angle velocity elevation burst-radius target-distance) YOUR-CODE-HERE)) ; Test cases -- (find-angle velocity elevation burst-radius target-distance) ; fixed Sun Feb 8 21:56:12 EST 2004 (define default-radius 1) (define default-target 20) ;(find-angle 10 100 default-radius default-target) ; -> 68 ; Verify that this angle gives right distance... ;(distance 68 10 100) ; -> approx 20.8 ; ;(find-angle 11 100 default-radius default-target) ; -> 71 ;(find-angle 10 50 default-radius default-target) ;(find-angle 10 50 default-radius 40) ; YOUR TEST CASES HERE... ;; Problem 7 ; Some scheme stuff to communicate with gorilla. Don't ; worry about the details, just the interface ; (define tell-gorilla (lambda (angle velocity) (if (or (not angle) (not velocity)) (display (list "Sorry, no angle and velocity works")) (display (list "Throw at" angle "degrees and" velocity "meters/sec"))) (list angle velocity))) ; Tests ;(tell-gorilla 30 15) ;(tell-gorilla #f 15) ;(tell-gorilla 30 #f) ; A bogus find-throw procedure that ; simply refuses to find a correct solution, but ; instead just tells the gorilla anything (not a ; smart thing for a pet programmer to do!) ; (define find-throw (lambda (elevation burst-radius target-distance) (tell-gorilla 45 10))) ;(find-throw 10 default-radius default-target) ; YOUR DOCUMENTATION HERE... ; (define find-throw (lambda (elevation burst-radius target-distance) (find-throw-helper 0 elevation burst-radius target-distance))) ; YOUR DOCUMENTATION HERE... ; (define find-throw-helper (lambda (velocity elevation burst-radius target-distance) YOUR-CODE-HERE)) ; Test cases ; ; A previous test, with fixed velocity ;(find-angle 10 10 1 20) ; -> #f ; ; Now find throw angle and velocity ;(find-throw 10 1 20) ; -> (18 11) - other values may work ; Verify that the suggested angle and velocity goes the distance: ;(distance 18 11 10) ; ; YOUR TEST CASES HERE... ;; Problem 8 ; How many bananas in a bundle survive a bounce ; (define num-survive (lambda (num) (if (even? num) (/ num 2) (/ (- num 1) 2)))) ; Tests ;(num-survive 5) ; -> 2 ;(num-survive 2) ; -> 1 ;(num-survive 1) ; -> 0 ; YOUR DOCUMENTATION HERE... ; (define bundle-bounces (lambda (num) YOUR-CODE-HERE)) ; Test cases ;(bundle-bounces 5) ; -> 2 ;(bundle-bounces 20) ; -> 4 ; YOUR TEST CASES HERE... ;; Problem 9 ; YOUR DOCUMENTATION HERE... ; (define bundle-distance (lambda (num angle velocity elevation) YOUR-CODE-HERE)) ; Test cases ;(distance 0 10 50) ; -> approx 31.9 ;(bundle-distance 1 0 10 50) ; -> approx 31.9 (one base case) ;(bundle-distance 0 0 10 50) ; -> 0 (another base case) ;(bundle-distance 5 0 10 50) ; -> approx 31.9 ;(bundle-distance 5 90 10 50) ; -> 0 ; ;(bundle-distance 2 45 10 50) ;(bundle-distance 20 45 10 50) ; farther ; YOUR TEST CASES HERE... ;; Problem 10 ; YOUR DOCUMENTATION HERE... ; (define find-bundle-throw (lambda (num elevation burst-radius target-distance) YOUR-CODE-HERE)) ; Test cases ; Some previous single-banana throws... ;(find-throw 10 default-radius default-target) ;(find-bundle-throw 1 10 default-radius default-target) ; ; Test - when single banana fails, can a bundle succeed? ;(find-throw 50 default-radius 400) ; -> (#f #f) ;(find-bundle-throw 10 50 default-radius 400) ; -> (73 9) ; ; YOUR TEST CASES HERE... ;; Problem 11 ; YOUR DOCUMENTATION HERE... ; (define find-throw (lambda (elevation burst-radius target-distance) YOUR-CODE-HERE))