Blob


1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; about the language level of this file in a form that our tools can easily process.
3 #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 ;;Model
6 (define NUM-OF-DIGITS 3)
8 ;;Chosen is a random number between 0 and 10^(NUM-OF-DIGITS)
9 (define chosen (random (expt 10 NUM-OF-DIGITS)))
12 ;build-number : (listof number) -> number
13 ;Given alon which represent the digits of a number, with the first digit representing the most significant digit, return the number alon represents.
15 ;x10add : number number -> number
16 ;Given n1 and n2, multiply n2 by 10 (ie, x10) then add to n1 (ie, add).
18 (define (build-number alon)
19 (local ((define (x10add n1 n2)
20 (+ (* n2 10) n1)))
21 (foldl x10add 0 alon)))
23 ;;View
25 (define a-msg (make-message "Make a guess!"))
27 ;;DIGITS is a list of gui-items [choice]
28 (define DIGITS (local ((define (create-choice-gui colnum)
29 (make-choice (build-list 10 number->string))))
30 (build-list NUM-OF-DIGITS create-choice-gui)))
32 ;;list-of-digit-choices : number -> gui-item [choice]
35 ;;Controller
37 ;;check-digits : event -> true
38 ;;Given event, return true. check-digits checks to see if the guess matches the chosen number.
39 (define (check-digits event)
40 (local ((define guess (build-number (map choice-index DIGITS))))
41 (cond
42 [(> guess chosen) (draw-message a-msg "Too High")]
43 [(< guess chosen) (draw-message a-msg "Too Low")]
44 [(= guess chosen) (draw-message a-msg "You've Won!")])))
46 (create-window (list DIGITS
47 (list a-msg)
48 (list (make-button "Check Guess" check-digits))))