======================bi-bang + itemization=====================================
Program Description: This program displays a single traffic light. When the program
starts the light is green (on). When the spacebar is pressed, the traffic light colour
changes (from green to yellow, yellow to red, red to green, etc.) When the mouse is
clicked the light turns off. Once the light is off, it cannot come back on again.
;; ==========
;; Constants:
(define WIDTH 120)
(define HEIGHT 100)
(define LIGHT-RADIUS 20)
(define MTS (empty-scene WIDTH HEIGHT))
;; LightColour is one of:
;; - "red"
;; - "yellow"
;; - "green"
;; interp. the colour of a traffic light
;; <examples are redundant for enumerations>
(define (fn-for-light-colour lc)
(cond [(string=? "red" lc) (...)]
[(string=? "yellow" lc) (...)]
[(string=? "green" lc) (...)]))
;; Template rules used:
;; - one of: 3 cases
;; - atomic distinct: "red"
;; - atomic distinct: "yellow"
;; - atomic distinct: "green"
;; TrafficLight is one of:
;; - false
;; - LightColour
;; interp. a traffic light: false means the light is off, otherwise it is the given colour
(define TL1 false)
(define TL2 "red")
(define TL3 "green")
(define (fn-for-traffic-light tl)
(cond [(false? tl) (...)]
[else
(... (fn-for-light-colour tl))]))
;; Template rules used:
;; - one of: 2 cases
;; - atomic distinct: false
;; - reference: LightColour
;; =================
;; Functions:
;; TrafficLight -> TrafficLight
;; start the world with (main "green")
;; no tests for main functions
(define (main tl)
(big-bang tl ; TrafficLight
(to-draw render-tl) ; TrafficLight -> Image
(on-mouse handle-mouse) ; TrafficLight Integer Integer MouseEvent
; -> TrafficLight
(on-key handle-key))) ; TrafficLight KeyEvent -> TrafficLight
problem 1: draw arrows
problem 2: Complete the design of the render-tl function based on the wish-list entry below.
You must write examples, a note stating where you took the template from
and a complete function definition. You do not need to repeat the signature,
purpose and stub.
solution:
;; TrafficLight -> Image
;; place rendering of the traffic light in the middle of MTS
(check-expect (render-tl "red") (overlay (circle LIGHT-RADIUS "solid" "red")
MTS))
(check-expect (render-tl false) (overlay (text "light is off" 24 "black")
MTS))
;; Took template from TrafficLight
(define (render-tl tl)
(cond [(false? tl)(overlay (text "light is off" 24 "black")
MTS)]
[else
(overlay (render-light-colour tl)
MTS)]))
;; LightColour -> Image
;; render an image dislaying the light colour
(check-expect (render-light-colour "red") (circle LIGHT-RADIUS "solid" "red"))
(check-expect (render-light-colour "green") (circle LIGHT-RADIUS "solid" "green"))
(check-expect (render-light-colour "yellow") (circle LIGHT-RADIUS "solid" "yellow"))
;; Took template from LightColour
(define (render-light-colour s)
(cond [(string=? "red" s) (circle LIGHT-RADIUS "solid" "red")]
[(string=? "yellow" s) (circle LIGHT-RADIUS "solid" "yellow")]
[(string=? "green" s) (circle LIGHT-RADIUS "solid" "green")]))
problem 3: Complete the design of the handle-mouse function based on the wish-list entry below.
You must write examples, a note stating where you took the template from
and a complete function definition. You do not need to repeat the signature, purpose
and stub.
solution:
;; TrafficLight Integer Integer MouseEvent -> TrafficLight
;; turns the light off when the mouse is clicked
(check-expect (handle-mouse "red" 93 23 "drag") "red")
(check-expect (handle-mouse "green" 22 11 "button-down") false)
;(define (handle-mouse tl x y me) tl)
;; Took template from MouseEvent
(define (handle-mouse tl x y me)
(cond [(mouse=? me "button-down") false]
[else
tl]))
; YOU DO NOT NEED TO COMPLETE THE HANDLE-KEY FUNCTION DESIGN. THE WISH-LIST ENTRY IS PROVIDED
; FOR COMPLETENESS OF THE PROGRAM ONLY.
;; TrafficLight KeyEvent -> TrafficLight
;; when the spacebar is pressed, if the light is on it changes colour (red > green > yellow > red, etc.)
;; !!!
(define (handle-key tl ke) tl)