Created
August 25, 2012 14:25
-
-
Save wmeister-old/3466437 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(module main racket) | |
(define spade #\♠) | |
(define heart #\♥) | |
(define diamond #\♦) | |
(define club #\♣) | |
(define suits (list spade heart diamond club)) | |
(define (card->value card) | |
(let ([id (car card)]) | |
(case id | |
[(1 2 3 4 5 6 7 8 9 10) id] | |
[(#\J #\Q #\K) 10] | |
[(#\A) 11]))) | |
(define (hand->values hand) | |
(map | |
(lambda (card) (card->value card)) | |
hand)) | |
(define (make-suit sym) | |
(map (lambda (v) (cons v sym)) | |
(append (range 1 10) '(#\J #\Q #\K #\A)))) | |
(define (make-deck) | |
(apply append | |
(for/list ([suit suits]) | |
(make-suit suit)))) | |
(define (make-and-shuffle-deck) | |
(shuffle (make-deck))) | |
(define (reduce-aces values) | |
(let ([sum (apply + values)]) | |
(if (<= sum 21) | |
values | |
(if (member 11 values) | |
(reduce-aces (append (remove 11 values) '(1))) | |
values)))) | |
(define (sum-hand hand) | |
(apply + (reduce-aces (hand->values hand)))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment