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-intermediate-reader.ss" "lang")((modname |23.1|) (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 #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp")))))
4 12687dd9 2023-08-04 jrmu ;;Model
5 12687dd9 2023-08-04 jrmu
6 12687dd9 2023-08-04 jrmu (define NUM-OF-DIGITS 3)
7 12687dd9 2023-08-04 jrmu
8 12687dd9 2023-08-04 jrmu ;;Chosen is a random number between 0 and 10^(NUM-OF-DIGITS)
9 12687dd9 2023-08-04 jrmu (define chosen (random (expt 10 NUM-OF-DIGITS)))
10 12687dd9 2023-08-04 jrmu
11 12687dd9 2023-08-04 jrmu
12 12687dd9 2023-08-04 jrmu ;build-number : (listof number) -> number
13 12687dd9 2023-08-04 jrmu ;Given alon which represent the digits of a number, with the first digit representing the most significant digit, return the number alon represents.
14 12687dd9 2023-08-04 jrmu
15 12687dd9 2023-08-04 jrmu ;x10add : number number -> number
16 12687dd9 2023-08-04 jrmu ;Given n1 and n2, multiply n2 by 10 (ie, x10) then add to n1 (ie, add).
17 12687dd9 2023-08-04 jrmu
18 12687dd9 2023-08-04 jrmu (define (build-number alon)
19 12687dd9 2023-08-04 jrmu (local ((define (x10add n1 n2)
20 12687dd9 2023-08-04 jrmu (+ (* n2 10) n1)))
21 12687dd9 2023-08-04 jrmu (foldl x10add 0 alon)))
22 12687dd9 2023-08-04 jrmu
23 12687dd9 2023-08-04 jrmu ;;View
24 12687dd9 2023-08-04 jrmu
25 12687dd9 2023-08-04 jrmu (define a-msg (make-message "Make a guess!"))
26 12687dd9 2023-08-04 jrmu
27 12687dd9 2023-08-04 jrmu ;;DIGITS is a list of gui-items [choice]
28 12687dd9 2023-08-04 jrmu (define DIGITS (local ((define (create-choice-gui colnum)
29 12687dd9 2023-08-04 jrmu (make-choice (build-list 10 number->string))))
30 12687dd9 2023-08-04 jrmu (build-list NUM-OF-DIGITS create-choice-gui)))
31 12687dd9 2023-08-04 jrmu
32 12687dd9 2023-08-04 jrmu ;;list-of-digit-choices : number -> gui-item [choice]
33 12687dd9 2023-08-04 jrmu
34 12687dd9 2023-08-04 jrmu
35 12687dd9 2023-08-04 jrmu ;;Controller
36 12687dd9 2023-08-04 jrmu
37 12687dd9 2023-08-04 jrmu ;;check-digits : event -> true
38 12687dd9 2023-08-04 jrmu ;;Given event, return true. check-digits checks to see if the guess matches the chosen number.
39 12687dd9 2023-08-04 jrmu (define (check-digits event)
40 12687dd9 2023-08-04 jrmu (local ((define guess (build-number (map choice-index DIGITS))))
41 12687dd9 2023-08-04 jrmu (cond
42 12687dd9 2023-08-04 jrmu [(> guess chosen) (draw-message a-msg "Too High")]
43 12687dd9 2023-08-04 jrmu [(< guess chosen) (draw-message a-msg "Too Low")]
44 12687dd9 2023-08-04 jrmu [(= guess chosen) (draw-message a-msg "You've Won!")])))
45 12687dd9 2023-08-04 jrmu
46 12687dd9 2023-08-04 jrmu (create-window (list DIGITS
47 12687dd9 2023-08-04 jrmu (list a-msg)
48 12687dd9 2023-08-04 jrmu (list (make-button "Check Guess" check-digits))))