;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-advanced-reader.ss" "lang")((modname gamepictionaryfinal) (read-case-sensitive #t) (teachpacks ((lib "master.ss" "teachpack" "htdp") (lib "draw.ss" "teachpack" "htdp") (lib "hangman.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "master.ss" "teachpack" "htdp") (lib "draw.ss" "teachpack" "htdp") (lib "hangman.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))))) ;; Matt Cannon ;; Problem Set 10 ;; Pictionary Game ;; ============== These are the two predefined functions ==================== ;; You don't need to understand their code, only the contract and the purpose ;; =========================================================================== ;; ===================== You may skip to the sample code below ================ ;; get-choice: string, list of strings -> integer ;; the function displays a simple menu form with the given message ;; and a drop-down menu with the list of choices. The menu ;; initially displays "Make a choice". Once a different item is chosen, ;; that item is returned. ;; When a "Close" button is pressed, the form disappears. (define (get-choice message list-of-choices) (local ( (define the-choices (make-choice (cons "Make a choice" list-of-choices))) (define w (create-window (list (list (make-message message)) (list the-choices) (list (make-button "Close" (lambda (e) (hide-window w))))))) (define (process-result n) (cond [(> n 200) (hide-window w)] ;; timeout after 200 sec [(= (choice-index the-choices) 0) (and (sleep-for-a-while 1) (process-result (+ n 1)))] [else true]) ) ) (cond [(process-result 0) (list-ref list-of-choices (- (choice-index the-choices) 1))]))) ;; get-answer: string number -> string ;; The function displays a simple text input with a question ;; and returns the answer typed in by the user ;; The second parameter (time-delay) is the number of seconds ;; the function waits after the typing of the first character ;; before it returns the answer. If the user takes longer ;; than that, a partial answer may be returned ;; 5 sec delay is reasonable for a one-word answer (define (get-answer question time-delay) (local ((define the-answer (make-text question)) (define w (create-window (list (list the-answer) (list (make-button "Close" (lambda (e) (hide-window w))))))) (define (process-result n) (cond [(> n 200) (hide-window w)] ;; timeout after 200 sec [(string=? (text-contents the-answer) "") (and (sleep-for-a-while 1) (process-result (+ n 1)))] [else true] ;; the user starts typing ))) (cond [(process-result 0) (cond [(sleep-for-a-while time-delay) (text-contents the-answer)]) ]) )) ;; =========================== do not change anything above this line ============= ;; NOTE: The language is Advanced Student. Teachpacks: gui.ss, draw.ss ;; =========================== Your work goes here: =============================== ;; Previously used defintions from drawing lab: ;; The structure represents a circle ;; center is the position (posn structure) of the center ;; radius is its radius (a non-negative number), and ;; color is a symbol representing a color (define-struct circle (center radius color)) ;; draw-a-circle: circle -> true ;; the function draws a circle structure as a disk ;; and returns true (define (draw-a-circle a-circle) (draw-solid-disk (circle-center a-circle) (circle-radius a-circle) (circle-color a-circle))) ;; The structure represents a rectangle ;; left-upper is the position (posn structure) of its ;; left upper corner, height and width are its height and ;; width (non-negative numbers), and ;; color is a symbol representing a color (define-struct rectangle (left-upper height width color)) ;; draw-a-rectangle: rectangle -> true ;; the function draws a rectangle structure as a solid rectangle ;; and returns true (define (draw-a-rectangle a-rectangle) (draw-solid-rect (rectangle-left-upper a-rectangle) (rectangle-height a-rectangle) (rectangle-width a-rectangle) (rectangle-color a-rectangle))) ;; draw-shapes: a list of shapes -> boolean ;; the function takes a list of shapes (circles, rectangles) ;; and draws them on the canvas in order (define (draw-shapes alosh) (cond [(empty? alosh) true] [(circle? (first alosh)) (and (draw-a-circle (first alosh)) (draw-shapes (rest alosh)))] [(rectangle? (first alosh)) (and (draw-a-rectangle (first alosh)) (draw-shapes (rest alosh)))])) ;===================== PICTIONARY ===================== ;;The Pictures: ;;Road - draws a road (define road (list (make-rectangle(make-posn 0 200) 300 100 'Black) (make-rectangle(make-posn 0 190) 300 10 'Green) (make-rectangle (make-posn 5 243) 40 6 'Yellow) (make-rectangle (make-posn 55 243) 40 6 'Yellow) (make-rectangle (make-posn 105 243) 40 6 'Yellow) (make-rectangle (make-posn 155 243) 40 6 'Yellow) (make-rectangle (make-posn 205 243) 40 6 'Yellow) (make-rectangle (make-posn 255 243) 40 6 'Yellow))) ;;Car - draws a car (define carr (list (make-rectangle (make-posn 75 150) 150 75 'Brown) (make-circle (make-posn 110 225) 25 'Black) (make-circle (make-posn 190 225) 25 'Black) (make-rectangle (make-posn 100 122) 105 30 'Brown) (make-rectangle (make-posn 105 125) 45 28 'Blue) (make-rectangle (make-posn 155 125) 45 28 'Blue))) ;;Tree - draws a tree (define tree (list (make-rectangle (make-posn 125 125) 18 260 'Brown) (make-circle (make-posn 125 145) 25 'Green) (make-circle (make-posn 150 150) 20 'Green) (make-circle (make-posn 175 120) 30 'Green) (make-circle (make-posn 110 110) 45 'Green) (make-circle (make-posn 155 90) 40 'Green))) ;House - draws a house (define house (list (make-rectangle (make-posn 0 250) 300 50 'Green) (make-rectangle (make-posn 75 150) 150 100 'Brown) (make-rectangle (make-posn 90 165) 40 40 'Blue) (make-rectangle (make-posn 173 165) 40 40 'Blue) (make-rectangle (make-posn 135 170) 33 80 'Red) (make-circle (make-posn 160 208) 4 'Yellow))) ;;8ball - draws an 8ball (define 8ball (list (make-circle (make-posn 150 150) 100 'Black) (make-circle (make-posn 150 150) 40 'White) (make-circle (make-posn 150 135) 15 'Black) (make-circle (make-posn 150 165) 15 'Black) (make-circle (make-posn 150 135) 10 'White) (make-circle (make-posn 150 165) 10 'White))) ;;defines the picture structure in terms of it's name (in form of a string) and it's list of shapes (define-struct picture (name los)) ;;randpic: number -> picture structure ;;provides a picture to a given number ;;(randpic 1) would yield (make-picture "8ball" 8ball) (define (randpic n) (cond [(= n 0) (make-picture "road" road)] [(= n 1) (make-picture "8ball" 8ball)] [(= n 2) (make-picture "car" carr)] [(= n 3) (make-picture "tree" tree)] [(= n 4) (make-picture "house" house)])) ;;gamepic function chooses a random picture from a given set to be the picture for the pictionary game (define gamepic (randpic (random 5))) ;;list of pictures to choose from (define pictures (list "road" "car" "house" "boat" "bowling ball" "8ball" "tree" "bank" "lockbox")) ;;listminus: string list-of-strings -> list-of -strings ;;consumes a string and list-of-strings, then produces a list-of-strings without the consumed string (define (listminus a los) (cond [(empty? los) empty] [(not (string=? a (first los))) (cons (first los) (listminus a (rest los)))] [(string=? a (first los)) (listminus a (rest los))])) ;;a message indicating that you've lost (define message1 (string-append "It is a " (picture-name gamepic) ", you lose.")) ;;a message indicating that you've won (define message2 (string-append "It is a " (picture-name gamepic) ", you win!")) ;;cond-draw: list-of-shapes -> shape ;;draws the first shape from a list-of-shapes (define (cond-draw los) (cond [(empty? los) (and (draw-solid-string (make-posn 10 20) message1) exit)] [(not (empty? los)) (draw-shapes (list (first los)))])) ;;response: string -> string/exit message ;;consumes a string and forms a response, the function is used to indicate correct/incorrect guesses in the game (define (response a) (cond [(string=? a (picture-name gamepic)) (and (draw-solid-string (make-posn 10 20) message2) (draw-shapes (picture-los gamepic)) exit)] [(not (string=? a (picture-name gamepic))) (draw-solid-string (make-posn 10 20) "Incorrect, try again.")])) ;=== Game Start === (start 300 300) ;=== Title === (draw-solid-string (make-posn 75 150) "Welcome to Pictionary") (sleep-for-a-while 4) (clear-solid-string (make-posn 75 150) "Welcome to Pictionary") (sleep-for-a-while 2) ;=== End Title === ;===Game Start=== (cond-draw (picture-los gamepic)) ;;defines the first choice from a list of choices (define choice (list (get-choice "What's the picture?" pictures))) ;;defines a list of possible pictures to choose from, minus first choice (set! pictures (listminus (first choice) pictures)) ;;reset function is recursively defined to reset the guessing process (define (reset shape choices pic) (and (response (first choices)) (sleep-for-a-while 8) (clear-solid-string (make-posn 10 20) "Incorrect, try again.") (cond-draw (rest shape)) (begin (set! choice (list (get-choice "What's the picture?" pictures))) (set! pictures (listminus (first choice) pictures)) (reset (rest shape) choice (rest pic)) ))) (reset (picture-los gamepic) choice pictures)