# [racket] left boundary problem

 From: Matthias Felleisen (matthias at ccs.neu.edu) Date: Thu May 10 09:33:37 EDT 2012 Previous message: [racket] left boundary problem Next message: [racket] Apache Thrift bindings for Racket? Messages sorted by: [date] [thread] [subject] [author]

```I have reformatted your code for readability. Please check whether tests are really complementary.

Exercise 1: eliminate all constants from function definition.

Exercise 2: clearly separate all constants that are freely definable (image of kat) from those that depend on these (size of kat and surrounding box). Then change the former to 'strange' values and see whether the program still makes sense. That's called refactoring for single points of control, and if you can do it you're well on your way to real software engineering.

(require 2htdp/universe)
(require 2htdp/image)

(define kat (circle 39 'solid 'red))
(define workspace (empty-scene 1000 200))
(define lengte (image-width workspace))
(define lengte2 (/ (image-width kat)2))
(define ondergrens (- 0 lengte2))
(define bovengrens (+ lengte lengte2))
(define gauge-omtrek (rectangle 1000 20 "outline" "black"))

(define-struct Vcat (Xcat Hcat Richting))
; Vcat = (make-editor Number Number)
; interp. (make-editor x h) where x is the x-coordinate of the cat and h is the happiness of the cat.
; make-editor Number Number -> Vcat
; Vcat-Xcat Editor -> Number
; Vcat-Hcat Editor -> Number
; Vcat-Richting -> String
; Vcat? Editor Any -> Boolean

; Vcat -> Vcat
; Function which change the world on clock ticks.
; On every tick the X-coordinate changes 3 pixels and the happiness decrease with 0.1.

(check-expect (tock (make-Vcat -39 100 "right")) (make-Vcat -39 99.9 "left")) ;
(check-expect (tock (make-Vcat -39 100 "left")) (make-Vcat -36 99.9 "left")) ;
(check-expect(tock (make-Vcat 20 100 "right")) (make-Vcat 17 99.9 "right"));
(check-expect (tock (make-Vcat 20 100 "left")) (make-Vcat 23 99.9 "left")) ;
(check-expect (tock (make-Vcat 1040 100 "right")) (make-Vcat 1037 99.9 "right"))
(check-expect (tock (make-Vcat 1040 100 "left")) (make-Vcat 1040 99.9 "right"))

(define (tock Vcat)
(cond
[(string=? (Vcat-Richting Vcat) "right")
(move-right-or-switch-direction (Vcat-Xcat Vcat) (Vcat-Hcat Vcat))]
[else
(move-left-or-switch-direction (Vcat-Xcat Vcat) (Vcat-Hcat Vcat))]))

;; Nat Nat -> Vcat
(define (move-right-or-switch-direction x h)
(cond
[(<= x ondergrens) (make-Vcat x (- h 0.1) "left")]
[(>= x ondergrens) (make-Vcat (- x 3) (- h 0.1) "right")]))

;; Nat Nat -> Vcat
(define (move-left-or-switch-direction x h)
(cond
[(and (>= x -39) (< x bovengrens)) (make-Vcat (+ x 3) (- h 0.1) "left")]
[(>= x bovengrens) (make-Vcat x (- h 0.1) "right")]))

; Vcat -> Image
; Function who display the state of the world into a image.
(define gauge-for-100
(overlay/xy gauge-omtrek 0 0 (rectangle (* 10 100) 20 "solid" "red")))
(define kat-at-90
(place-image kat 90 140 workspace))
(define kat-at-0
(place-image kat 0 140 workspace))

(check-expect (render (make-Vcat 90 100 "left"))
(place-image gauge-for-100 500 10 kat-at-90))
(check-expect (render (make-Vcat 0 100 "right"))
(place-image gauge-for-100 500 10 kat-at-0))

(define (render Vcat)
(place-image (render-gauge Vcat)
500 10
(place-image kat
(Vcat-Xcat Vcat) 140
workspace)))

;; Vcat -> Image
(define (render-gauge Vcat)
(overlay/xy gauge-omtrek
0 0
(rectangle (* 10 (Vcat-Hcat Vcat)) 20 "solid" "red")))

(define (stop Vcat)
(= (Vcat-Hcat Vcat) 0))

(define (main Vcat)
(big-bang Vcat (on-tick tock) (on-draw render) (stop-when stop )))

(define state0 (make-Vcat 12 10 "right")))

;; run (main state0) #### AFTER the tests are all completed

```

 Posted on the users mailing list. Previous message: [racket] left boundary problem Next message: [racket] Apache Thrift bindings for Racket? Messages sorted by: [date] [thread] [subject] [author]