This repository has been archived by the owner on Aug 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrafficLight.rkt
59 lines (52 loc) · 2.7 KB
/
TrafficLight.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-beginner-reader.ss" "lang")((modname TrafficLight) (read-case-sensitive #t) (teachpacks ((lib "image.rkt" "teachpack" "2htdp") (lib "batch-io.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.rkt" "teachpack" "2htdp") (lib "batch-io.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp")))))
; Graphic constants
(define MT (empty-scene 100 30))
(define PADDING (rectangle 5 10 "solid" "white"))
; TrafficLight -> TrafficLight
; simulates a traffic light that changes with each clock tick
(define (traffic-light-simulation initial-state)
(big-bang initial-state
[to-draw tl-render]
[on-tick tl-next 1]))
; TrafficLight -> TrafficLight
; determines the next state of the traffic light, given current-state
(check-expect (tl-next "green") "yellow")
(check-expect (tl-next "yellow") "red")
(check-expect (tl-next "red") "green")
(define (tl-next current-state)
(cond
[(string=? "green" current-state) "yellow"]
[(string=? "yellow" current-state) "red"]
[(string=? "red" current-state) "green"]))
; TrafficLight -> Image
; renders the current state of the traffic light as a image
(check-expect (tl-render "green")
(overlay (beside (circle 10 "outline" "red") PADDING
(circle 10 "outline" "yellow") PADDING
(circle 10 "solid" "green"))
MT))
(check-expect (tl-render "yellow")
(overlay (beside (circle 10 "outline" "red") PADDING
(circle 10 "solid" "yellow") PADDING
(circle 10 "outline" "green"))
MT))
(check-expect (tl-render "red")
(overlay (beside (circle 10 "solid" "red") PADDING
(circle 10 "outline" "yellow") PADDING
(circle 10 "outline" "green"))
MT))
(define (tl-render current-state)
(overlay (beside (circle 10
(if (string=? "red" current-state) "solid" "outline")
"red")
PADDING
(circle 10
(if (string=? "yellow" current-state) "solid" "outline")
"yellow")
PADDING
(circle 10
(if (string=? "green" current-state) "solid" "outline")
"green"))
MT))