Created
December 4, 2013 21:04
-
-
Save jbclements/7795513 to your computer and use it in GitHub Desktop.
code rewritten to use a list rather than a structure.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require 2htdp/universe) | |
(require 2htdp/image) | |
;; a 16-bools is a list of booleans of length 16 | |
;world -> world | |
;; a world is (make-world 16-bools) | |
;a world is (make-world boolean boolean boolean boolean) | |
(define-struct world (bools)) | |
;; return the 'idx'th bool from the world | |
;; world number -> bool | |
(define (world-bool w idx) | |
(list-ref (world-bools w) idx)) | |
;; replace the 'n'th element of a list with a new value | |
;; list number any -> list | |
(define (list-replace lob idx new-val) | |
(cond [(= idx 0) (cons new-val (rest lob))] | |
[else (cons (first lob) (list-replace (rest lob) (sub1 idx) new-val))])) | |
(check-expect (list-replace (list false false false false) 2 "apple") | |
(list false false "apple" false)) | |
;; toggle the 'n'th element of a list from false to true and vice versa | |
;; list number -> list | |
(define (list-toggle lob idx ) | |
(cond [(= idx 0) (cons (not (first lob)) (rest lob))] | |
[else (cons (first lob) (list-toggle (rest lob) (sub1 idx)))])) | |
(check-expect (list-toggle (list false false false false) 2) | |
(list false false true false)) | |
;; toggle the n-th bool in a world from true to false and vice versa | |
;; world number -> world | |
(define (toggle-bool w idx) | |
(make-world (list-toggle (world-bools w) idx))) | |
;; WRITE A PURPOSE STATEMENT AND SIGNATURE FOR YOUR FUNCTION! | |
(define (buttons solid? z) (rectangle 70 40 (if solid? "solid" "outline") z)) | |
;world -> image | |
;;> FIX THIS PURPOSE STATEMENT! | |
;circles takes a world and draws 4 circles over an empty-scene | |
(define (rect x) | |
;;> why do you need to pass "black" in everywhere? | |
(place-image (buttons (world-bool x 3) "black") 350 50 | |
(place-image (buttons (world-bool x 2) "black") 250 50 | |
(place-image (buttons (world-bool x 1) "black") 150 50 | |
(place-image (buttons (world-bool x 0) "black") 50 50 | |
(place-image (buttons (world-bool x 7) "black") 350 150 | |
(place-image (buttons (world-bool x 6) "black") 250 150 | |
(place-image (buttons (world-bool x 5) "black") 150 150 | |
(place-image (buttons (world-bool x 4) "black") 50 150 | |
(place-image (buttons (world-bool x 11) "black") 350 250 | |
(place-image (buttons (world-bool x 10) "black") 250 250 | |
(place-image (buttons (world-bool x 9) "black") 150 250 | |
(place-image (buttons (world-bool x 8) "black") 50 250 | |
(place-image (buttons (world-bool x 15) "black") 350 350 | |
(place-image (buttons (world-bool x 14) "black") 250 350 | |
(place-image (buttons (world-bool x 13) "black") 150 350 | |
(place-image (buttons (world-bool x 12) "black") 50 350(empty-scene 400 400)))))))))))))))))) | |
;world key -> world | |
;changes the state of whichever world-field-name corresponds to the key pressed to the opposite value | |
(check-expect (change (make-world (list false false false false false false false false false false false false false false false false)) "1") | |
(make-world (list true false false false false false false false false false false false false false false false))) | |
(check-expect (change (make-world (list false true false true false false false false false false false false false false false false)) "4") | |
(make-world (list false true false false false false false false false false false false false false false false))) | |
;;> PURPOSE STATEMENT AND SIGNATURE! | |
(define (change x y) | |
(cond | |
[(key=? y "1") (toggle-bool x 0)] | |
[(key=? y "2") (toggle-bool x 1)] | |
[(key=? y "3") (toggle-bool x 2)] | |
[(key=? y "4") (toggle-bool x 3)] | |
[(key=? y "q") (toggle-bool x 4)] | |
[(key=? y "w") (toggle-bool x 5)] | |
[(key=? y "e") (toggle-bool x 6)] | |
[(key=? y "r") (toggle-bool x 7)] | |
[(key=? y "a") (toggle-bool x 8)] | |
[(key=? y "s") (toggle-bool x 9)] | |
[(key=? y "d") (toggle-bool x 10)] | |
[(key=? y "f") (toggle-bool x 11)] | |
[(key=? y "z") (toggle-bool x 12)] | |
[(key=? y "x") (toggle-bool x 13)] | |
[(key=? y "c") (toggle-bool x 14)] | |
[(key=? y "v") (toggle-bool x 15)] | |
[else x])) | |
(big-bang (make-world (list false false false false false false false false false false false false false false false false)) (to-draw rect) (on-key change) (on-release change)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment