;; code for ps4. ;; ;; update requires definitions of make-ship-state, height, velocity, fuel, ;; engine-strength, fuel-burn-rate, gravity, and dt ;; ;; you will write a new version for Problem 2. (define (update ship-state fuel-burn-rate) (make-ship-state (+ (height ship-state) (* (velocity ship-state) dt)) (+ (velocity ship-state) (* (- (* engine-strength fuel-burn-rate) gravity) dt)) (- (fuel ship-state) (* fuel-burn-rate dt)))) ;; lander-loop requires definitions of show-ship-state, landed?, end-game, ;; and get-burn-rate (define (lander-loop ship-state) (show-ship-state ship-state) (if (landed? ship-state) (end-game ship-state) (lander-loop (update ship-state (get-burn-rate))))) ;; show-ship-state (needed in lander-loop) (define (show-ship-state ship-state) (display (list "height" (height ship-state) "velocity" (velocity ship-state) "fuel" (fuel ship-state))) (newline)) ;; landed? (needed in lander-loop) (define (landed? ship-state) (<= (height ship-state) 0)) ;; end-game (needed in lander-loop) (define (end-game ship-state) (let ((final-velocity (velocity ship-state))) (display "final velocity ") (display final-velocity) (newline) (cond ((>= final-velocity safe-velocity) (display "good landing ") "game over") (else (display "you crashed! ") "game over")))) ;; get the burn rate from the user. Note this is _not_ asynchronous. ;; the user must press "Enter" after space (for full burn) or "Enter" ;; after some key other than space or Enter (for no burn). (define (get-burn-rate) (let ((c (read-char (current-input-port)))) (cond ((char=? c #\newline) (get-burn-rate)) ((char=? c burn-key) 1) (else 0)))) ;; (define (play) (lander-loop (initial-ship-state))) ;; (define (initial-ship-state) (make-ship-state 50 ;height 0 ;velocity 20)) ;fuel ;; some constants (define dt 1) (define gravity .5) (define safe-velocity -2) (define engine-strength 1) (define burn-key #\space)