;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-reader.ss" "lang")((modname war-player-simple) (read-case-sensitive #t) (teachpacks ((lib "testing.ss" "teachpack" "htdp") (lib "universe.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "testing.ss" "teachpack" "htdp") (lib "universe.ss" "teachpack" "2htdp"))))) ;; a World is a list of strings: card value and suit: ;; a sample World -- a deck of cards: (define deck (list "Kh" "Qd" "3s" "8c")) ;; draw the world as simple text in a 40 x 60 scene ;; show-card: String -> (define (show-card alist) (cond [(empty? alist) (empty-scene 40 60)] [else (place-image (text (first alist) 10 'black) 10 10 (empty-scene 40 60))])) (show-card deck) (show-card empty) ;;----------------------------------------------------------------------------- ;;--- ;; receive a message: if 'done - stop the world ;; else append the card you won to the end of your list ;; receive-world: (World [Listof String] -> World (define (receive-world w msg) (cond [(symbol? msg) 'stop-the-world] [else (append w msg)])) ;; test receive-world: (check-expect (receive-world deck 'done) 'stop-the-world) (check-expect (receive-world deck empty) deck) (check-expect (receive-world deck (list "4s" "Jc")) (list "Kh" "Qd" "3s" "8c" "4s" "Jc")) ;;--- ;; on key event (space bar) ;; if space bar is pressed ;; remove the top card from the deck and put it on the bottom ;; the next card will now be seen by us ;; ignore all other keys, or when our deck is empty ;; on-key-event: World KeyEvent -> World (define (on-key-event alist ke) (cond [(string=? ke " ") (cond [(empty? alist) 'stop-the-world] [else (append (rest alist) (list (first alist)))])] [else alist])) ;; test on-key-event: (check-expect (on-key-event deck "right") deck) (check-expect (on-key-event empty " ") 'stop-the-world) (check-expect (on-key-event deck " ") (list "Qd" "3s" "8c" "Kh")) ;; define the condition for ending the world ;; end-the-world?: World -> Boolean (define (end-the-world? w) (empty? w)) ;; test end-the-world (check-expect (end-the-world? deck) false) (check-expect (end-the-world? empty) true) ;; run the world (big-bang deck (stop-when end-the-world?) (on-receive receive-world) (on-key on-key-event) (on-draw show-card))