summaryrefslogtreecommitdiffstats
path: root/net/ricketyspace
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2018-03-20 02:36:31 +0000
committerrsiddharth <s@ricketyspace.net>2018-03-20 02:36:31 +0000
commit06ebf0f46fa4ec5c55da20f88c6afa89814e6e2e (patch)
treea46e031303ee4c3991e4f119261aee6d58c1f6ef /net/ricketyspace
parent6dddc7840c0dc469a08e9dfa334e9235f66014a0 (diff)
Add net/ricketyspace/ror/five/guess.rkt
Diffstat (limited to 'net/ricketyspace')
-rw-r--r--net/ricketyspace/ror/five/guess.rkt63
1 files changed, 63 insertions, 0 deletions
diff --git a/net/ricketyspace/ror/five/guess.rkt b/net/ricketyspace/ror/five/guess.rkt
new file mode 100644
index 0000000..66f9fbb
--- /dev/null
+++ b/net/ricketyspace/ror/five/guess.rkt
@@ -0,0 +1,63 @@
+#lang racket
+(require 2htdp/universe 2htdp/image)
+
+(struct interval (small big))
+
+;;; constants
+(define TEXT-SIZE 12)
+(define HELP-TEXT
+ (text "↑ for larger numbers, ↓ for smaller ones"
+ TEXT-SIZE
+ "blue"))
+(define HELP-TEXT2
+ (text "Press = when your number is guessed; q to quit."
+ TEXT-SIZE
+ "blue"))
+(define COLOR "red")
+(define WIDTH (+ (image-width HELP-TEXT2) 10))
+(define HEIGHT 150)
+(define SIZE 72)
+(define TEXT-X 3)
+(define TEXT-UPPER-Y 10)
+(define TEXT-LOWER-Y 135)
+(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))))
+
+;; main
+(define (start lower upper)
+ (big-bang (interval lower upper)
+ (on-key deal-with-guess)
+ (to-draw render)
+ (stop-when single? render-last-scene)))
+
+;; key events
+(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 (smaller w)
+ (interval (interval-small w)
+ (max (interval-small w) (sub1 (guess w)))))
+
+(define (bigger w)
+ (interval (min (interval-big w) (add1 (guess w)))
+ (interval-big 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 "End" SIZE COLOR) MT-SC))
+
+(define (single? w)
+ (= (interval-small w) (interval-big w)))