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"
(define HELP-TEXT2
(text "press = when your number is guessed; q to quit it."
(define COLOR "red")

(define MT-SC
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)
[(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)




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

25 Must-Visit Killer Websites For Developers

How to get your PSPO and PSM certifications in two months

How to increase/decrease FileSystem under LVM

Convert Emojis to Text Using This Python Library

Why We Use Django At Keleno

Free, Secure and Strong — 2FA for Mikrotik and VPN

Demystifying ICMP Types and Codes

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
Anil Kumar B

Anil Kumar B

More from Medium

Java Virtual Machine — 01

Signed and Unsigned Arithmetic Operations using Java

Test better using MockK’s slot and Mockito’s argument captor

Single & Multidimentional Array