;; 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-player2) (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")) (define deck (list "Jh" "Ad" "9s" "5c")) ;; 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) ;;----------------------------------------------------------------------------- ;;--- ;; append the second list to the end of the first one ;; append [Listof X] [Listof X] -> [Listof X] ;(define (append list1 list2) ; (cond ; [(empty? list1) list2] ; [else (cons (first list1) (append (rest list1) list2))])) ;; test append: (check-expect (append (list 3 4 5) (list 6 7)) (list 3 4 5 6 7)) ;;--- ;; receive a message: if 'done - stop the world ;; if you have no cards and you received none - stop the world ;; else append the card you won to the end of your list ;; and play the first available card ;; 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 "Jh" "Ad" "9s" "5c" "4s" "Jc")) ;;--- ;; on key event (space bar) show the next card ;; 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 "Ad" "9s" "5c") "Jh")) ;; 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))