;; 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) (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 100 120)] [else (place-image (text (first alist) 50 'black) 10 10 (empty-scene 100 120))])) (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 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 send it to the server ;; the next card will now be seen by us ;; ignore all other keys, or when our deck is empty ;; on-key-event: World KeyEvent -> (Package: World String) | World (define (on-key-event alist ke) (cond [(string=? ke " ") (cond [(empty? alist) 'stop-the-world] [else (make-package (rest alist) (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 " ") (make-package (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) (register "127.0.0.1") (on-draw show-card))