;; 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 EricJustinAndrew) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))))) ;; PROJECT ONE ;; JUSTIN DAVIS, ERIC LASKA, ANDREW LEXVOLD ;; UNITE FOR THE GLORY OF THE FATHERLAND! ;Purpose: to remove the element at index from a los (list of stuff) ;Contract: list,int->list (define (remove-index los index) (cond [(and (empty? los) (> index 0)) (error 'remove-index "index out of range")] [(empty? los) empty] [(= index 0) (remove-index (rest los) (- index 1))] [else (cons (first los) (remove-index (rest los) (- index 1)))] ) ) ;Purpose: to find the index of the first occurance in los (list of stuff) ;Contract: list,any,integer 0->integer or error (define (find-index-occurance los occurance index) {cond [(empty? los) (error 'find-index-occurance "no such occurance exists")] [(= (first los) occurance) index] [else (find-index-occurance (rest los) occurance (+ index 1))] } ) ;Purpose: to get the element at index of a list ;Contract: list,int->any (define (get-index los index) (cond [(empty? los) (error 'get-index "index out of range")] [(> index 0) (get-index (rest los) (- index 1))] [else (first los)] ) ) ;Purpose: to find the character at index of a string ;Contract: string,int->string (define (index-of-string strg index) (get-index (explode strg) index);runs the find-index function on the string exploded into a list ) ;; ============== 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 (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)]) ]) )) ;; ========================================================================= ;; ===================== HELPER FUNCTIONS CAUSE WE'RE LAZY ================= ;; ========================================================================= (define (pause x) ; x is in milliseconds (sleep-for-a-while (/ x 1000)) ) (define (draw-sword upper-left-of-blade) ; must be (make-posn x y) (and (draw-solid-rect upper-left-of-blade 25 200 'black) ;blade (draw-solid-rect (make-posn (- (posn-x upper-left-of-blade) 50) (+ 140 (posn-y upper-left-of-blade))) 130 20 'black) ;hilt ) ) (define (clear-sword upper-left-of-blade) (and (clear-solid-rect upper-left-of-blade 25 200) ;blade (clear-solid-rect (make-posn (- (posn-x upper-left-of-blade) 50) (+ 140 (posn-y upper-left-of-blade))) 130 20) ;hilt ) ) (define (draw-staff upper-left-of-staff) (and (draw-solid-rect upper-left-of-staff 15 200 'black) ; staff (draw-solid-disk (make-posn (+ (posn-x upper-left-of-staff) 7.5) (posn-y upper-left-of-staff)) 20 'red) ) ) (define (clear-staff upper-left-of-staff) (and (clear-solid-rect upper-left-of-staff 15 200 'black) ; staff (clear-solid-disk (make-posn (+ (posn-x upper-left-of-staff) 7.5) (posn-y upper-left-of-staff)) 20) ) ) (define (draw-potion upper-left-of-bottle) (and (draw-solid-rect upper-left-of-bottle 50 150 'black) ; outline! (draw-solid-disk (make-posn (+ (posn-x upper-left-of-bottle) 25) (+ (posn-y upper-left-of-bottle) 150)) 50 'black) ; bottom outline (draw-solid-rect (make-posn (+ (posn-x upper-left-of-bottle) 5) (+ (posn-y upper-left-of-bottle) 5)) 40 140 'red) ; potion! (draw-solid-disk (make-posn (+ (posn-x upper-left-of-bottle) 25) (+ (posn-y upper-left-of-bottle) 150)) 45 'red) ; potion! ) ) (define (clear-potion upper-left-of-bottle) (and (clear-solid-rect upper-left-of-bottle 50 150 'black) ; outline! (clear-solid-rect (make-posn (+ (posn-x upper-left-of-bottle) 5) (+ (posn-y upper-left-of-bottle) 5)) 40 140 'red) ; potion! (clear-solid-disk (make-posn (+ (posn-x upper-left-of-bottle) 25) (+ (posn-y upper-left-of-bottle) 150)) 50 'black) ; bottom outline (clear-solid-disk (make-posn (+ (posn-x upper-left-of-bottle) 25) (+ (posn-y upper-left-of-bottle) 150)) 45 'red) ; potion! ) ) ;; ============================= STORYLINE ================================= ; sadjfhjasdf ;; ======== CHAPTER 1 ======== (define character-name (get-answer "What's yo' name?" 4)) ;contract: list,string->boolean ;purpose: to determine if los contains thing (define (find-first-occurance los thingdown thingup) (cond [(empty? los) false] [(not (string? (first los))) false] [(or (string=? (first los) thingup) (string=? (first los) thingdown)) true] [else (find-first-occurance (rest los) thingdown thingup)] ) ) ;; pick your first weapon! (define weapon-choices1 (list "sword" "staff")) (define weapon-list1 (list (get-choice "Choose your weapon, fool." weapon-choices1))) ;(pause 4000) (start 640 480) ; there's our canvas ; sorry, decided to use the same variable name. why not? (define message (string-append "You now have a " (first weapon-list1))) ; check to see what we need to draw, and draw the text (cond [(string=? (first weapon-list1) "sword") (and (draw-sword (make-posn 175 50)) (draw-solid-string (make-posn 20 20) message) (pause 3000) (clear-sword (make-posn 175 50)) (clear-solid-string (make-posn 20 20) message))] [(string=? (first weapon-list1) "staff") (and (draw-staff (make-posn 250 50)) (draw-solid-string (make-posn 20 20) message) (pause 3000) (clear-staff (make-posn 250 50)) (clear-solid-string (make-posn 20 20) message))] [else false] ) ;; ======== CHAPTER 2 ======== (draw-solid-string (make-posn 20 20) "You encounter a bear in the wilderness.") (pause 4000) (draw-solid-string (make-posn 20 32) "What do you want to do?") (define decision1-choices (list "Fight" "Run")) (define decision1-list (list (get-choice "Whatcha gonna do?" decision1-choices))) (clear-solid-string (make-posn 20 20) "You encounter a bear in the wilderness.") (clear-solid-string (make-posn 20 32) "What do you want to do?") (cond [(string=? (first decision1-list) "Fight") (and (draw-solid-string (make-posn 20 20) "You n00b. You died.") (pause 1000) (draw-solid-string (make-posn 20 32) "You didn't really think you could kill a bear, did you?") (pause 1500) (draw-solid-string (make-posn 20 44) "Just because you had a weapon doesn't mean you'll kill anything.") (draw-solid-string (make-posn 50 56) "GAME OVER!") (error 'death "You were killed by a bear") (stop) )] [(string=? (first decision1-list) "Run") (and (draw-solid-string (make-posn 20 20) "Good idea. You got away!") (pause 2000) (draw-solid-string (make-posn 20 32) "Sweet! On your way back, you found something.") )] ) (define chapter2-items (list (first weapon-list1) "healthpotion")) (clear-solid-string (make-posn 20 20) "Good idea. You got away!") (pause 2000) (clear-solid-string (make-posn 20 32) "Sweet! On your way back, you found something.") (pause 500) (define decision2-choices (list "View item" "I don't care")) (define decision2-list (list (get-choice "What shall we do now?" decision2-choices))) (draw-solid-rect (make-posn 0 0) 640 480 'white) (cond [(string=? (first decision2-list) "View item") (and (draw-solid-string (make-posn 20 20) "You got a healthpotion!") (draw-potion (make-posn 200 75)) (pause 4000) (clear-solid-string (make-posn 20 20) "You got a healthpotion!") (clear-potion (make-posn 200 75)) )] [(string=? (first decision2-list) "I don't care") (and (draw-solid-string (make-posn 20 20) "Fine, then!") (pause 2000) (draw-solid-string (make-posn 20 30) "By the way, you got a healthpotion!") (pause 1000) (draw-potion (make-posn 200 75)) (pause 2000) (clear-solid-string (make-posn 20 20) "Fine, then!") (clear-solid-string (make-posn 20 30) "By the way, you got a healthpotion!") (clear-potion (make-posn 200 75)) )] ) ;; ======== CHAPTER 3 ======== (define chapter3-items (list (first weapon-list1) "running boots")) (clear-solid-string (make-posn 20 20) "Find, then!") (clear-solid-string (make-posn 20 32) "By the way, you got a healthpotion!") (draw-solid-string (make-posn 10 10) "You found some running boots, what do you want to do with them?") (define decision3-choices (list "Put on" "Discard")) (define decision3-list (list (get-choice "What do you do?" decision3-choices))) (draw-solid-rect (make-posn 0 0) 640 480 'white) ;Contract:list->boolean ;Purpose: to draw the counterdown timer to the race on screen (define (draw-countdown time-left) (cond [(<= (first time-left) 0) true] [else (and (draw-solid-rect (make-posn 0 0) 640 480 'white) (draw-solid-string (make-posn 20 20) (number->string (first time-left))) (pause 1000) (draw-countdown (rest time-left)) ) ] ) ) (cond [(string=? (first decision3-list) "Put on") (and (draw-solid-string (make-posn 20 20) "You must now run from the quickly approaching bear!") (pause 3000) (draw-solid-string (make-posn 20 32) "Be prepared to move to the finish by using your arrow keys!!!") (pause 2500) (draw-countdown (list 5 4 3 2 1 0)) ) ];if put on [else (and (draw-solid-string (make-posn 20 20) "You couldn't run fast enough, the bear caught and mangled you") (error 'death "The bear caught up and ated you"))];if not put on ) ;; ======== CHAPTER 4 ======== ;Contracct: int,int,int,int->boolean ;Purpose: to move the object on screen (define (move px py bx by) (cond [(and (>= px 600) (<= px 640) (>= py 200) (<= py 220)) (cond [ (not (find-first-occurance (explode character-name) "z" "Z")) (error 'Death "You trip and the bear caught and killt you. You lost") ] [else (and (draw-solid-rect (make-posn 0 0) 640 480 'white) (draw-solid-string (make-posn 20 20) "You WON by ESCAPING the BEAR!!1!one") (draw-solid-string (make-posn 20 32) "Now you must finish winning:") false ) ] ) ] [(<= (sqrt (+ (expt (- px bx) 2) (expt (- py by) 2))) 20) (error 'Death "You could not escape the bear and died") ] [else (and (draw-solid-rect (make-posn 0 0) 640 480 'white) (draw-solid-rect (make-posn 600 200) 40 20 'black) (draw-solid-rect (make-posn px py) 10 10 'black) (draw-solid-rect (make-posn bx by) 20 20 'red) ) ] ) ) (define posnx 200) (define posny 200) (draw-solid-rect (make-posn posnx posny) 10 10 'black) (define (get-component px py bx by) (* 0.2 (sqrt (+ (expt (- px bx) 2) (expt (- py by) 2)))) ) ;Contract: int,int,int,int->boolean ;Purpose: to handle key input then call the move function to move the object on screen (define (get-key px py pdx pdy bx by) (local ( (define key (get-key-event)) (define bdx (/ (- px bx) (get-component px py bx by))) (define bdy (/ (- py by) (get-component px py bx by))) ) (cond [(eq? key false) (and (move (+ px pdx) (+ py pdy) (+ bx bdx) (+ by bdy)) (pause (/ 1000 60)) (get-key (+ px pdx) (+ py pdy) pdx pdy (+ bx bdx) (+ by bdy)))] [(eq? key 'release) (get-key px py 0 0 bx by)] [(eq? key 'left) (get-key px py -4 pdy bx by)] [(eq? key 'right) (get-key px py 4 pdy bx by)] [(eq? key 'up) (get-key px py pdx -4 bx by)] [(eq? key 'down) (get-key px py pdx 4 bx by)] [else (and (pause (/ 1000 60)) (get-key px py 0 0 bx by))] ) ) ) (get-key posnx posny 0 0 0 0) ;; ======== CHAPTER 5 ======== (define chapter5-items (list (first weapon-list1) "running boots")) (define decision5-choices (list "Keep everything" "Discard your weapon" "Discard your health potion" "Discard your boots")) (define decision5-list (list (get-choice "What do you want to remove?" decision5-choices))) (draw-solid-rect (make-posn 0 0) 640 480 'white) (cond [(string=? (first decision5-list) "Discard your weapon") (and (draw-solid-string (make-posn 20 20) "You discared your weapon") (pause 3000) (draw-solid-rect (make-posn 0 0) 640 480 'white) (pause 1000) ) ] [(string=? (first decision5-list) "Discard your health potion") (and (draw-solid-string (make-posn 20 20) "You discared your health potion") (pause 3000) (draw-solid-rect (make-posn 0 0) 640 480 'white) (pause 1000) ) ] [(string=? (first decision5-list) "Discard your boots") (and (draw-solid-string (make-posn 20 20) "You discared your boots") (pause 3000) (draw-solid-rect (make-posn 0 0) 640 480 'white) (pause 1000) ) ] [(string=? (first decision5-list) "Keep everything") (and (draw-solid-string (make-posn 20 20) "You kept everything, have fun carrying it all!") (pause 3000) (draw-solid-rect (make-posn 0 0) 640 480 'white) (pause 1000) ) ] ) (define decision6-choices (list "Yes" "No, I'm a n00b")) (define decision6-list (list (get-choice "Do you want to win?" decision6-choices))) (cond [(string=? (first decision6-list) "Yes") (and (draw-solid-string (make-posn 20 20) "You have now finally won. Good job") (pause 3000) (draw-solid-rect (make-posn 0 0) 640 480 'white) (error 'Victory "You have finally won, I promise") ) ] [(string=? (first decision6-list) "No, I'm a n00b") (and (draw-solid-string (make-posn 20 20) "You REALLY are a n00b for chosing that answer. The bear bites off your head") (error 'Failure "You chose to lose? You N00B!") ) ] )