CoreDump: A Guessing Game.

(define (guess w)
(quotient ( + (interval-small w) (interval-big w)) 2))
(require 2htdp/universe 2htdp/image)

(define WIDTH 600)
(define HEIGHT 600)
(define TEXT-SIZE 10)
(define TEXT-X 20)
(define TEXT-UPPER-Y 50)
(define TEXT-LOWER-Y 50)
(define SIZE 36)

(define X-CENTER (quotient WIDTH 2))
(define Y-CENTER (quotient HEIGHT 2))

(struct interval (small big count))

(define HELP-TEXT (text " UP for larger numbers, DOWN for smaller ones"
TEXT-SIZE
"Blue"))
(define HELP-TEXT2
(text "press = when your number is guessed; q to quit it."
TEXT-SIZE
"blue"))
(define COLOR "red")

(define MT-SC
(place-image/align
HELP-TEXT TEXT-X TEXT-UPPER-Y "left" "top"
(place-image/align
HELP-TEXT2 TEXT-X TEXT-LOWER-Y "left" "bottom"
(empty-scene WIDTH HEIGHT))))

(define (start lower upper)
(big-bang (interval lower upper count)
(on-key deal-with-guess)
(to-draw render )
(stop-when single? render-last-scene)))

(define (deal-with-guess w key)
(cond
[(key=? key "up") (bigger w)]
[(key=? key "down") (smaller w)]
[(key=? key "q") (stop-with w)]
[(key=? key "=") (stop-with w)]
[else w]))

(define (count w)
(interval (interval-count w)))


(define (smaller w)

(interval (interval-small w)

(max (interval-small w) (sub1 (guess w)))

(add1 (interval-count w))))


(define (bigger w)

(interval (min (interval-big w) (add1 (guess w)))

(add1 (interval-count w))))


(define (guess w)
(quotient ( + (interval-small w) (interval-big w)) 2))

(define (render w)
(overlay (text (number->string (guess w)) SIZE COLOR) MT-SC))


(define (render-last-scene w)
(overlay (text (number->string (count w)) SIZE COLOR) MT-SC))


(define (single? w)
(= (interval-small w) (interval-big w)))
(define (count w)
(interval (interval-count w)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store