;; This plays a simplified game of Twenty-One. ;; (strategy customer-total-so-far dealer-up-card) should return #t or #f ;; indicating whether the customer should hit. ;; The twenty-one function then returns 1, 0, or -1 according to ;; whether the customer won, tied, or lost. (define (twenty-one strategy) ;; The result-of-dealer-play function assumes customer-total <= 21, ;; and that the customer wants no more cards. ;; It then plays the dealer's hand and returns the result of the game. (define (result-of-dealer-play customer-total dealer-total-so-far) (cond ((> dealer-total-so-far 21) 1) ((< dealer-total-so-far 17) (result-of-dealer-play customer-total (+ dealer-total-so-far (random-card)) ) ) ((< customer-total dealer-total-so-far) -1) ((= customer-total dealer-total-so-far) 0) (else 1) ) ) ;; The result-of-play function plays the customer's hand and then ;; the dealer's hand if necessary, and returns the result of the game. (define (result-of-play customer-total-so-far dealer-up-card) (cond ((> customer-total-so-far 21) -1) ((strategy customer-total-so-far dealer-up-card) (result-of-play (+ customer-total-so-far (random-card)) dealer-up-card)) (else ; The dealer should start with two cards, not one, but she'll ; always hit at least once if we give her only one card. (result-of-dealer-play customer-total-so-far dealer-up-card) ) ) ) (result-of-play (+ (random-card) (random-card)) (random-card) ) ) ;; The random-card function returns a random card value between 1 and 10, ;; inclusive. ;; The value 10 is returned four times more often than any other value. (define (random-card) (min (+ 1 (random 13)) 10) )