;; 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 |problem set 10 blackjack|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))))) ;; Problem Set 10 ;; Patrick Uphoff ;; Tony Castaneda ;; calling "get-choice" to get the user's choice, adding the choice to ;; the list of weapons. weapon-list is the new 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 3) (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 5) (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: =============================== ;; This is an example of one round of the accummulation stage of a game. ;; Your game will have multiple rounds. ;; the first list of choices: ;(define weapon-choices (list "frying pan" "light saber")) ;; calling "get-choice" to get the user's choice, adding the choice to ;; the list of weapons. weapon-list is the new list ;(define weapon-list (cons ; (get-choice "Choose your weapon" weapon-choices) ; empty)) ;(sleep-for-a-while 2) ;; calling a get-answer function to let the user type in their name ;; 5 is the number of seconds you expect the user to spend on typing ;; the answer (from the fisrt character to the last one). ;; Too short times may result in the program getting a partial answer ;; Feel free to adjust this time as needed ;(define character-name (get-answer "What's your name?" 5)) (start 400 400) ; Defining rectangle structure for drawing cards. ;; 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 (draw-rectangle x y width height) (and (draw-solid-line (make-posn x y) (make-posn (+ x width) y)) (draw-solid-line (make-posn (+ x width) y) (make-posn (+ x width) (+ y height))) (draw-solid-line (make-posn (+ x width) (+ y height)) (make-posn x (+ y height))) (draw-solid-line (make-posn x (+ y height)) (make-posn x y)) ) ) ;; Defining the Jack, Queen, and King as 10, since that is their value in the game of Blackjack (define J 10) (define Q 10) (define K 10) ;; Ace is a special case, and as such, will be defined later. (define A 11) ;; Deck covers all 52 cards in a deck. The suit of a card is not important. The list is used so nth can pull a random card from the deck. (define deck (list A A A A 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10 10 J J J J Q Q Q Q K K K K)) ;; Selector for a random card from a deck. (define (nth a-list n) (cond [(= n 0) (first a-list)] [else (nth (rest a-list) (- n 1))] ) ) ;; Draw-card is a key function, because it is used for both dealing out the player's hand, and for adding new cards on "Hit". (define (draw-card) (nth deck (random 52))) ;These are the two functions use for the GUI interface (define choices (list "Hit" "Stay")) (define (action) (get-choice "Hit or Stay?" choices)) ;This determines the starting hand for both the player and the dealer (define hand-player (list (draw-card) (draw-card))) (define hand-dealer (list (draw-card))) ;contract: (Player-string a-list x y) => a list of numbers posn-x posn-y => Drawing ;Purpose: used to convert the hand into a string, which can be printed on the canvas to show the player's hands. (define (player-string a-list x y) (cond [(empty? a-list) ""] [else (begin (draw-solid-string (make-posn x y) (number->string (first a-list))) (player-string (rest a-list) (+ 25 x) y))])) ;This is the main function of the Blackjack game. All of the other functions run from here. ;Contract: player-hit-or-stay alon1 alon2 => list of x, list of x => String ;Purpose: this reacts to the hit or stay option selected, also leads into dealer options. Also Draws the start of the game. (define (player-hit-or-stay alon1 alon2) (begin (player-string alon1 25 300) (draw-solid-string (make-posn 25 250) "PLAYER") (draw-rectangle 20 230 75 25) (player-string alon2 200 100) (draw-solid-string (make-posn 200 75) "DEALER") (draw-rectangle 195 55 75 25) (cond [(< 21 (foldr + 0 alon1)) (draw-solid-string (make-posn 155 220)"Bust! You Lose!")] [else (local ((define thechoice (action))) (cond [(string=? "Stay" thechoice) (dealer hand-dealer)] [(string=? "Hit" thechoice) (begin (set! hand-player (cons (draw-card) alon1)) (clear-solid-rect (make-posn 20 280) 300 20) (player-hit-or-stay hand-player hand-dealer))] ) )] ))) ;Contract: compare alon1 alon2 => a list of x, a list of x => String ;Purpose: To compare the player (alon1) and the dealer (alon2) to determine who wins. (define (compare alon1 alon2) (cond [(= (foldr + 0 alon1) (foldr + 0 alon2))(draw-solid-string (make-posn 155 220) "Push. You tied")] [(< (foldr + 0 alon1) (foldr + 0 alon2))(draw-solid-string (make-posn 155 220)"Dealer Wins!")] [else (draw-solid-string (make-posn 155 220)"You Win!")] ) ) ;Contract: dealer alon2 => A list of x => String ;This controls the dealers commands automatically. He stops on 17 and higher, busts above 21. (define (dealer alon2) (begin (player-string alon2 200 100) (cond [(> (foldr + 0 alon2) 21) (draw-solid-string (make-posn 155 220)"Dealer Busts! You win!")] [(and (<= 17 (foldr + 0 alon2)) (>= 21 (foldr + 0 alon2))) (compare hand-player hand-dealer)] [else (begin (clear-solid-rect (make-posn 180 85) 300 40) (set! hand-dealer (cons (draw-card) alon2)) (dealer hand-dealer))] ) ) ) (draw-rectangle 150 200 150 25) (draw-solid-string (make-posn 200 200) "RESULT") (player-hit-or-stay hand-player hand-dealer)