Blame


1 12687dd9 2023-08-04 jrmu ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 12687dd9 2023-08-04 jrmu ;; about the language level of this file in a form that our tools can easily process.
3 12687dd9 2023-08-04 jrmu #reader(lib "htdp-advanced-reader.ss" "lang")((modname |37.4|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp")))))
4 12687dd9 2023-08-04 jrmu (define WIDTH 100)
5 12687dd9 2023-08-04 jrmu (define HEIGHT 340)
6 12687dd9 2023-08-04 jrmu (define RADIUS 40)
7 12687dd9 2023-08-04 jrmu (define INTERDIST 20)
8 12687dd9 2023-08-04 jrmu (define X-BULB (/ WIDTH 2))
9 12687dd9 2023-08-04 jrmu (define Y-RED (+ INTERDIST RADIUS))
10 12687dd9 2023-08-04 jrmu (define Y-YELLOW (+ Y-RED (* 2 RADIUS) INTERDIST))
11 12687dd9 2023-08-04 jrmu (define Y-GREEN (+ Y-YELLOW (* 2 RADIUS) INTERDIST))
12 12687dd9 2023-08-04 jrmu
13 12687dd9 2023-08-04 jrmu ;A traffic-light color (TL-color) is either
14 12687dd9 2023-08-04 jrmu ;1. 'red,
15 12687dd9 2023-08-04 jrmu ;2. 'yellow, or
16 12687dd9 2023-08-04 jrmu ;3. 'green.
17 12687dd9 2023-08-04 jrmu
18 12687dd9 2023-08-04 jrmu ;;State Variable:
19 12687dd9 2023-08-04 jrmu ;;current-color : TL-color
20 12687dd9 2023-08-04 jrmu (define current-color 'red)
21 12687dd9 2023-08-04 jrmu
22 12687dd9 2023-08-04 jrmu ;next : -> true
23 12687dd9 2023-08-04 jrmu ;Effect: Changes current-color from 'red to 'green, 'green to 'yellow, or 'yellow to 'red depending on what the current-color is. Returns true once evaluation completes.
24 12687dd9 2023-08-04 jrmu
25 12687dd9 2023-08-04 jrmu (define (next)
26 12687dd9 2023-08-04 jrmu (local ((define previous-color current-color))
27 12687dd9 2023-08-04 jrmu (begin (set! current-color (next-color current-color))
28 12687dd9 2023-08-04 jrmu (switch current-color previous-color))))
29 12687dd9 2023-08-04 jrmu
30 12687dd9 2023-08-04 jrmu ;next-color : TL-color -> TL-color
31 12687dd9 2023-08-04 jrmu ;Given acolor, returns the next logical color.
32 12687dd9 2023-08-04 jrmu
33 12687dd9 2023-08-04 jrmu (define (next-color acolor)
34 12687dd9 2023-08-04 jrmu (cond
35 12687dd9 2023-08-04 jrmu [(symbol=? acolor 'red) 'green]
36 12687dd9 2023-08-04 jrmu [(symbol=? acolor 'yellow) 'red]
37 12687dd9 2023-08-04 jrmu [(symbol=? acolor 'green) 'yellow]))
38 12687dd9 2023-08-04 jrmu
39 12687dd9 2023-08-04 jrmu ;;init-current-color : -> true
40 12687dd9 2023-08-04 jrmu ;Opens the canvas and draws the outline of the 3 traffic light bulbs as well as lighting up the red lightbulb.
41 12687dd9 2023-08-04 jrmu
42 12687dd9 2023-08-04 jrmu (define (init-current-color)
43 12687dd9 2023-08-04 jrmu (begin (start WIDTH HEIGHT)
44 12687dd9 2023-08-04 jrmu (draw-circle (make-posn X-BULB Y-RED) (+ RADIUS 1) 'black)
45 12687dd9 2023-08-04 jrmu (draw-circle (make-posn X-BULB Y-YELLOW) (+ RADIUS 1) 'black)
46 12687dd9 2023-08-04 jrmu (draw-circle (make-posn X-BULB Y-GREEN) (+ RADIUS 1) 'black)
47 12687dd9 2023-08-04 jrmu (set! current-color 'red)
48 12687dd9 2023-08-04 jrmu (fill-bulb current-color)))
49 12687dd9 2023-08-04 jrmu
50 12687dd9 2023-08-04 jrmu ;; fill-bulb : symbol -> boolean
51 12687dd9 2023-08-04 jrmu ;; Fills in a given bulb based on color; returns true if the function evaluates properly, false otherwise.
52 12687dd9 2023-08-04 jrmu
53 12687dd9 2023-08-04 jrmu (define (fill-bulb color)
54 12687dd9 2023-08-04 jrmu (cond
55 12687dd9 2023-08-04 jrmu [(symbol=? color 'red)
56 12687dd9 2023-08-04 jrmu (draw-solid-disk (make-posn X-BULB Y-RED) RADIUS 'red)]
57 12687dd9 2023-08-04 jrmu [(symbol=? color 'yellow)
58 12687dd9 2023-08-04 jrmu (draw-solid-disk (make-posn X-BULB Y-YELLOW) RADIUS 'yellow)]
59 12687dd9 2023-08-04 jrmu [(symbol=? color 'green)
60 12687dd9 2023-08-04 jrmu (draw-solid-disk (make-posn X-BULB Y-GREEN) RADIUS 'green)]))
61 12687dd9 2023-08-04 jrmu
62 12687dd9 2023-08-04 jrmu ;; clear-bulb : symbol -> boolean
63 12687dd9 2023-08-04 jrmu ;; Clears a bulb given color; returns true if evaluation completes, false otherwise.
64 12687dd9 2023-08-04 jrmu
65 12687dd9 2023-08-04 jrmu (define (clear-bulb color)
66 12687dd9 2023-08-04 jrmu (cond
67 12687dd9 2023-08-04 jrmu [(symbol=? color 'red)
68 12687dd9 2023-08-04 jrmu (clear-solid-disk (make-posn X-BULB Y-RED) RADIUS 'red)]
69 12687dd9 2023-08-04 jrmu [(symbol=? color 'yellow)
70 12687dd9 2023-08-04 jrmu (clear-solid-disk (make-posn X-BULB Y-YELLOW) RADIUS 'yellow)]
71 12687dd9 2023-08-04 jrmu [(symbol=? color 'green)
72 12687dd9 2023-08-04 jrmu (clear-solid-disk (make-posn X-BULB Y-GREEN) RADIUS 'green)]))
73 12687dd9 2023-08-04 jrmu
74 12687dd9 2023-08-04 jrmu ;; switch : symbol symbol -> boolean
75 12687dd9 2023-08-04 jrmu ;; Fills in bulb based on fill and clears another based on clear to switch colors. Returns true if evaluation completes, false otherwise.
76 12687dd9 2023-08-04 jrmu
77 12687dd9 2023-08-04 jrmu (define (switch fill clear)
78 12687dd9 2023-08-04 jrmu (and
79 12687dd9 2023-08-04 jrmu (fill-bulb fill)
80 12687dd9 2023-08-04 jrmu (clear-bulb clear)))
81 12687dd9 2023-08-04 jrmu
82 12687dd9 2023-08-04 jrmu (init-current-color)