;; 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-intermediate-lambda-reader.ss" "lang")((modname AdamSethAlex) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "master.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "master.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))))) ;Extended Exercise ;Adam Hystead, Seth Sorensen, Alex Longerbone ;; 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 (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 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 delay) (text-contents the-answer)]) ]) )) (define (JamesBond x) (and (and (draw-solid-disk (make-posn 100 250) 15 'pink)(draw-solid-rect (make-posn 82 245) 5 6 'pink)(draw-solid-rect (make-posn 114 245) 5 6 'pink)) ;head/ears (and(draw-solid-disk (make-posn 96 245) 2 'white)(draw-solid-disk (make-posn 104 245) 2 'white) (draw-solid-disk (make-posn 96.5 245) 1 'blue) (draw-solid-disk (make-posn 104.5 245) 1 'blue)) (and (draw-solid-line (make-posn 102 250) (make-posn 102 252) 'black) (draw-solid-line (make-posn 100 252) (make-posn 102 252)));nose (draw-solid-line (make-posn 98 256) (make-posn 103 256) 'black);mouth (and (draw-solid-rect (make-posn 80 270) 40 50 'black)(draw-solid-line (make-posn 90 270)(make-posn 104 280) 'white)(draw-solid-line (make-posn 110 270)(make-posn 104 280) 'white)(draw-solid-line (make-posn 104 280)(make-posn 104 320)'white)(draw-solid-rect (make-posn 112 276) 5 5 'red));body (and (draw-solid-rect (make-posn 120 270) 20 10 'black) (draw-solid-rect (make-posn 130 250) 10 20 'black) (draw-solid-rect (make-posn 75 272) 10 30 'black)(draw-solid-rect (make-posn 75 292) 20 10 'black)(draw-solid-line (make-posn 85 275)(make-posn 85 292) 'silver)(draw-solid-line (make-posn 85 292)(make-posn 94 292) 'silver)(draw-solid-line (make-posn 94 292)(make-posn 94 301) 'silver)(draw-solid-line (make-posn 94 301)(make-posn 80 301) 'silver)) ;arms (and (draw-solid-rect (make-posn 82 320) 36 60 'black) (draw-solid-line (make-posn 100 379) (make-posn 100 340) 'white)) ;legs (and (draw-solid-rect (make-posn 80 382) 40 10 'black)(draw-solid-line (make-posn 100 382) (make-posn 100 391) 'white)) ;feet (and (draw-solid-rect (make-posn 90 235) 22 5 'black) (draw-solid-rect (make-posn 98 240) 5 2 'black) (draw-solid-rect (make-posn 85 235) 5 12 'black)(draw-solid-rect (make-posn 112 235) 5 12 'black)) ;hair (and (draw-solid-rect (make-posn 95 291) 10 10 'pink) (draw-solid-rect (make-posn 130 240) 10 10 'pink));hands (draw-solid-rect (make-posn 125 230) 5 20 'silver) ;gun )) (define (Stickman x) (and(draw-circle (make-posn 200 200) 50 'black);head (draw-solid-line (make-posn 200 250)(make-posn 200 350) 'black);body (draw-solid-line (make-posn 200 280)(make-posn 250 275) 'black);arm (draw-solid-line (make-posn 150 275)(make-posn 200 280) 'black);arm (draw-solid-line (make-posn 200 350)(make-posn 230 410) 'black);leg (draw-solid-line (make-posn 170 410)(make-posn 200 350) 'black);leg (draw-circle (make-posn 185 185) 10 'black);eye (draw-circle (make-posn 215 185) 10 'black);eye (draw-solid-disk (make-posn 185 185) 5 'black);pupil (draw-solid-disk (make-posn 215 185) 5 'black);pupil (draw-solid-line (make-posn 185 210)(make-posn 200 220) 'black);smile (draw-solid-line (make-posn 200 220)(make-posn 215 210) 'black);smile) (draw-solid-line (make-posn 230 255)(make-posn 260 285) 'black)));stick (weapon))) (define (MrClean x) (and (and (draw-solid-disk (make-posn 100 250) 15 'pink)(draw-solid-rect (make-posn 82 245) 5 6 'pink)(draw-solid-rect (make-posn 114 245) 5 6 'pink));head (and(draw-solid-disk (make-posn 96 245) 2 'white)(draw-solid-disk (make-posn 104 245) 2 'white) (draw-solid-disk (make-posn 96.5 245) 1 'blue) (draw-solid-disk (make-posn 104.5 245) 1 'blue));eyes (and (draw-solid-line (make-posn 102 250) (make-posn 102 252) 'black) (draw-solid-line (make-posn 100 252) (make-posn 102 252)));nose (and (draw-solid-line (make-posn 98 256) (make-posn 95 254) 'black)(draw-solid-line (make-posn 103 256) (make-posn 106 254) 'black)(draw-solid-line (make-posn 98 256) (make-posn 103 256) 'black));mouth (and (draw-solid-rect (make-posn 71 271) 58 14 'white) (draw-solid-rect (make-posn 81 285) 38 35 'white) (draw-solid-line (make-posn 70 270)(make-posn 130 270) 'black)(draw-solid-line (make-posn 80 280)(make-posn 80 320) 'black)(draw-solid-line (make-posn 120 280)(make-posn 120 320)'black)(draw-solid-line (make-posn 70 270)(make-posn 70 285)'black)(draw-solid-line (make-posn 70 285)(make-posn 80 285)'black)(draw-solid-line (make-posn 130 270)(make-posn 130 285)'black)(draw-solid-line (make-posn 130 285)(make-posn 120 285)'black));body (and (draw-solid-rect (make-posn 82 320) 36 60 'darkblue) (draw-solid-line (make-posn 100 379) (make-posn 100 340) 'white));legs (and (draw-solid-line (make-posn 80 382)(make-posn 99 382) 'black)(draw-solid-line (make-posn 80 382)(make-posn 80 392) 'black)(draw-solid-line (make-posn 80 392)(make-posn 99 392) 'black)(draw-solid-line (make-posn 99 382)(make-posn 99 392) 'black));right foot (and (draw-solid-line (make-posn 101 382)(make-posn 120 382) 'black)(draw-solid-line (make-posn 101 382)(make-posn 101 392) 'black)(draw-solid-line (make-posn 101 392)(make-posn 120 392) 'black)(draw-solid-line (make-posn 120 382)(make-posn 120 392) 'black));left foot (and(draw-solid-rect (make-posn 70 285) 15 15 'pink)(draw-solid-rect (make-posn 115 285) 15 15 'pink)(draw-solid-rect (make-posn 70 287) 60 15 'pink)(draw-solid-line (make-posn 85 287)(make-posn 115 293) 'black)(draw-solid-line (make-posn 85 287)(make-posn 73 290)'black)(draw-solid-line (make-posn 70 285)(make-posn 70 302)'black)(draw-solid-line (make-posn 70 302)(make-posn 105 302)'black)(draw-solid-rect (make-posn 105 302) 10 5 'pink)(draw-solid-line (make-posn 105 302)(make-posn 105 307)'black)(draw-solid-line (make-posn 105 307)(make-posn 115 307)'black)(draw-solid-line (make-posn 130 285)(make-posn 130 302) 'black)(draw-solid-line (make-posn 130 302) (make-posn 115 302)'black)(draw-solid-line (make-posn 115 307)(make-posn 115 293)'black)(draw-solid-line (make-posn 85 287) (make-posn 120 287)'black)(draw-solid-line (make-posn 120 287)(make-posn 124 292)'black));arms (draw-solid-rect (make-posn 76 316) 6 12 'lightgreen);soap )) (define (jungle x) (and (draw-solid-rect (make-posn 0 0) 1000 500 'darkgreen) (draw-solid-rect (make-posn 50 200) 90 425 'brown) (draw-solid-rect (make-posn 189 125) 69 600 'brown) (draw-solid-rect (make-posn 498 200) 50 300 'brown) (draw-solid-rect (make-posn 701 0) 100 500 'brown) (draw-solid-rect (make-posn 0 170) 200 30 'lightgreen) (draw-solid-rect (make-posn 0 165) 200 15 'green) (draw-solid-rect (make-posn 150 125) 190 20 'lightgreen) (draw-solid-rect (make-posn 155 115) 180 15 'green) (draw-solid-rect (make-posn 468 200) 110 23 'lightgreen) (draw-solid-rect (make-posn 473 195) 100 15 'green) ;BUGS (draw-solid-disk (make-posn 100 400) 10 'black) (draw-solid-disk (make-posn 97 399) 2 'red) (draw-solid-disk (make-posn 102 399) 2 'red) (draw-solid-disk (make-posn 120 450) 10 'black) (draw-solid-disk (make-posn 117 449) 2 'red) (draw-solid-disk (make-posn 123 449) 2 'red) (draw-solid-disk (make-posn 210 400) 10 'black) (draw-solid-disk (make-posn 207 397) 2 'red) (draw-solid-disk (make-posn 213 397) 2 'red) (draw-solid-disk (make-posn 240 215) 10 'black) (draw-solid-disk (make-posn 237 214) 2 'red) (draw-solid-disk (make-posn 243 214) 2 'red) (draw-solid-disk (make-posn 190 290) 10 'black) (draw-solid-disk (make-posn 187 291) 2 'red) (draw-solid-disk (make-posn 193 291) 2 'red) (draw-solid-disk (make-posn 390 287) 10 'black) (draw-solid-disk (make-posn 387 286) 2 'red) (draw-solid-disk (make-posn 393 286) 2 'red) (draw-solid-disk (make-posn 420 310) 10 'black) (draw-solid-disk (make-posn 417 309) 2 'red) (draw-solid-disk (make-posn 423 309) 2 'red) (draw-solid-disk (make-posn 340 200) 10 'black) (draw-solid-disk (make-posn 337 199) 2 'red) (draw-solid-disk (make-posn 343 199) 2 'red) (draw-solid-disk (make-posn 490 210) 10 'black) (draw-solid-disk (make-posn 487 209) 2 'red) (draw-solid-disk (make-posn 493 209) 2 'red) (draw-solid-disk (make-posn 310 430) 10 'black) (draw-solid-disk (make-posn 307 429) 2 'red) (draw-solid-disk (make-posn 313 429) 2 'red))) (define (lasers x) (and (draw-solid-line (make-posn 495 148) (make-posn 100 400) 'red) (draw-solid-line (make-posn 495 148) (make-posn 120 450) 'red) (draw-solid-line (make-posn 495 148) (make-posn 210 400) 'red) (draw-solid-line (make-posn 495 148) (make-posn 240 215) 'red) (draw-solid-line (make-posn 495 148) (make-posn 190 290) 'red) (draw-solid-line (make-posn 495 148) (make-posn 390 287) 'red) (draw-solid-line (make-posn 495 148) (make-posn 420 310) 'red) (draw-solid-line (make-posn 495 148) (make-posn 340 200) 'red) (draw-solid-line (make-posn 495 148) (make-posn 310 430) 'red))) (define (robot x) (and (draw-solid-rect (make-posn 475 130) 50 50 'silver) ;head (draw-solid-rect (make-posn 465 185) 70 50 'silver) ;body (draw-solid-rect (make-posn 485 145) 30 8 'red) ;eye (draw-solid-rect (make-posn 435 200) 130 10 'silver) ;arms (draw-solid-rect (make-posn 435 175) 10 25 'silver) ;left forearm (draw-solid-rect (make-posn 555 175) 10 25 'silver) ;right forearm (draw-solid-rect (make-posn 455 240) 90 20 'silver))) ;feet (define (cave x) (and (draw-solid-rect (make-posn 0 0) 1000 500 'brown) (draw-solid-disk (make-posn 500 300) 200 'black) (draw-solid-rect (make-posn 300 300) 400 200 'brown))) ;;Contract: string character -> character ;;Purpose: Adds an item to a character's profile (define (give-item item a-char) (cond [(empty? (character-slot1 a-char)) (make-character (character-charname a-char) item empty empty empty empty)] [(empty? (character-slot2 a-char)) (make-character (character-charname a-char) (character-slot1 a-char) item empty empty empty)] [(empty? (character-slot3 a-char)) (make-character (character-charname a-char) (character-slot1 a-char) (character-slot2 a-char) item empty empty)] [(empty? (character-slot4 a-char)) (make-character (character-charname a-char) (character-slot1 a-char) (character-slot2 a-char) (character-slot3 a-char) item empty)] [(empty? (character-slot5 a-char)) (make-character (character-charname a-char) (character-slot1 a-char) (character-slot2 a-char) (character-slot3 a-char) (character-slot4 a-char) item)] [else "You Have No Room Left!"])) (define (clear-canvas x) (draw-solid-rect (make-posn 0 0) 1000 500 'white)) ;;Purpose: defines a character and inventory of the character (define-struct character (charname slot1 slot2 slot3 slot4 slot5)) (start 1000 500) ;Contract: string -> character ;Purpose: lets the user choose a character (define (draw-char string) (cond [(string=? string "James Bond") (JamesBond 1)] [(string=? string "Stickman") (Stickman 1)] [(string=? string "Mr. Clean") (MrClean 1)] [else true])) ;Purpose: lets's the user pick the character so it can be called again to add something to the inventory (define char1 (make-character (get-choice "Choose your character" (list "James Bond" "Stickman" "Mr. Clean")) empty empty empty empty empty)) (draw-char (character-charname char1)) (draw-solid-string (make-posn 20 20) "You woke up in the middle of the jungle, all disorientated and such, but there is a cave nearby.") (sleep-for-a-while 3) ;Contract: choice -> advancement through the scene ;Purpose: to let the user choose where they want to go next (define (jungle-sequence1 x) (and (clear-canvas 1) (jungle 1) (draw-char (character-charname char2)) (draw-solid-string (make-posn 100 20) "You're walking through the jungle and you hear some buzzing.") (draw-solid-string (make-posn 100 40) "Suddenly a swarm of angry flesh-eating bugs descend upon you.") (draw-solid-string (make-posn 100 60) "You search around and see some items on the ground.") (cond [(empty? (character-slot3 x)) (jungle-sequence2 (give-item (get-choice "What would you like to use?" (list "Rock" "Stick" "Banana")) char2))] [(string=? (character-slot3 x) "Robot") (and (robot 1) (jungle-sequence2 (give-item (get-choice "What would you like to use?" (list "Rock" "Stick" "Banana")) x)))]))) ;Contract: choice -> advancement through the scene ;Purpose: to let the user choose where they want to go next (define (jungle-sequence2 struct) (and (clear-canvas 1) (jungle 1) (draw-char (character-charname char2)) (cond [(and (string=? (character-charname struct) "Stickman") (string=? (character-slot2 struct) "Stick")) (and(draw-solid-line (make-posn 150 250) (make-posn 150 290) 'black) (draw-solid-line (make-posn 150 250) (make-posn 230 255) 'silver) (draw-solid-string (make-posn 300 50) "You smash all the bugs with your nunchucks and now you see an exit from the jungle.") (draw-solid-string (make-posn 300 70) "You escape from the jungle and never look back, however you're still perplexed by") (draw-solid-string (make-posn 300 90) "the mystery of how you ended up there in the first place. To be continued..."))] [(string=? (character-slot3 struct) "Robot") (and (robot 1) (draw-solid-string (make-posn 100 20) "You and your robot buddy smoke the bugs and") (draw-solid-string (make-posn 100 40) "walk out of the jungle hand in hand and live happily ever after.") (lasers 1))] [else (draw-solid-string (make-posn 300 50) "Your weapon you chose is no match for tiny little insects, and you are now deceased, Game Over!")]))) ;Contract: choice -> advancement through the scene ;Purpose: to let the user choose where they want to go next (define (cave-sequence1 x) (and (clear-canvas 1) (cave 1) (draw-solid-string (make-posn 20 20) "On your way into the cave you stumble over a flashlight. You pick it up, and add it to your inventory.") (draw-char (character-charname char2)) (draw-solid-string (make-posn 20 40) "You walk into the cave. It's dark, you turn on your flashlight and search around. You find a sword on the ground, it is in rough shape.") (cond [(string=? (get-choice "Do you keep the sword?" (list "Yes" "No")) "Yes") (cave-sequence2 (give-item "Sword" char2))] [else (cave-sequence2 (give-item "No Sword" char2))]))) ;Contract: choice -> advancement through the scene ;Purpose: to let the user choose where they want to go next (define (cave-sequence2 struct) (and (clear-canvas 1) (draw-solid-rect (make-posn 0 60) 1000 500 'black) (draw-solid-disk (make-posn 500 250) 200 'yellow) (draw-solid-string (make-posn 20 20) "You walk deeper into the cave and see a lonely robot sitting in the corner") (robot 1) (draw-solid-string (make-posn 20 40) "You walk up to the robot, and he looks up at you innocently.") (cond [(string=? (get-choice "What would you like to do?" (list "Befriend the robot." "Attack him.")) "Attack him") (cave-sequence3 1)] [else (cave-sequence4 (give-item "Robot" struct))]))) ;Contract: choice -> advancement through the scene ;Purpose: to let the user choose where they want to go next (define (cave-sequence3 x) (and (clear-canvas) (draw-solid-string (make-posn 400 250) "The robot killed you with his laser. GAME OVER!!"))) ;Contract: choice -> advancement through the scene ;Purpose: to let the user choose where they want to go next (define (cave-sequence4 x) (and (clear-canvas 1) (draw-solid-string (make-posn 20 40) "You and the robot team up and search the cave, and find it's a dead end, so you leave the cave.") (draw-char (character-charname char1)) (robot 1) (sleep-for-a-while 5) (jungle-sequence1 x))) (define (choice1 string) (cond [(string=? string "Go into the cave.") (cave-sequence1 1)] [else (and (draw-solid-string (make-posn 20 20) "On your way into the jungle you stumble over a flashlight. You pick it up, and add it to your inventory.") (jungle-sequence1 char2))])) (define char2 (give-item "Flashlight" char1)) (choice1 (get-choice "What would you like to do?" (list "Go into the cave." "Venture deeper into the jungle." )))