Accidentally skipped over this project earlier and it was mentioned in the poker hands project so I am doubling back to do it. Full description at link. For this post I’m just quoting the descriptions of the subprocedures and tests.
Card-val
Write a procedure
card-val
that takes a single card as its argument and returns the value of that card.
1 2 3 4 5 6 7 8 9 10 |
> (card-val 'cq) 2 > (card-val 's7) 0 > (card-val 'ha) 4 |
My answer:
1 2 3 4 5 6 7 |
(define (card-val card) (cond ((equal? (bf card) 'a) 4) ((equal? (bf card) 'k) 3) ((equal? (bf card) 'q) 2) ((equal? (bf card) 'j) 1) (else 0))) |
High-card-points
Write a procedure
high-card-points
that takes a hand as its argument and returns the total number of points from high cards in the hand. (This procedure does not count distribution points.)
1 2 3 4 5 6 7 |
> (high-card-points '(sa s10 hq ck c4)) 9 > (high-card-points '(sa s10 s7 s6 s2 hq hj h9 ck c4 dk d9 d3)) 13 |
My answer:
1 2 3 |
(define (high-card-points sent) (accumulate + (every card-val sent))) |
Count-suit
Write a procedure
count-suit
that takes a suit and a hand as arguments and returns the number of cards in the hand with the given suit.
1 2 3 4 5 6 7 8 9 10 |
> (count-suit 's '(sa s10 hq ck c4)) 2 > (count-suit 'c '(sa s10 s7 s6 s2 hq hj h9 ck c4 dk d9 d3)) 2 > (count-suit 'd '(h3 d7 sk s3 c10 dq d8 s9 s4 d10 c7 d4 s2)) 5 |
My answer:
1 2 3 |
(define (count-suit suit sent) (appearances suit (accumulate word sent))) |
Suit-counts
Write a procedure
suit-counts
that takes a hand as its argument and returns a sentence containing the number of spades, the number of hearts, the number of clubs, and the number of diamonds in the hand.
1 2 3 4 5 6 7 8 9 10 |
> (suit-counts '(sa s10 hq ck c4)) (2 1 2 0) > (suit-counts '(sa s10 s7 s6 s2 hq hj h9 ck c4 dk d9 d3)) (5 3 2 3) > (suit-counts '(h3 d7 sk s3 c10 dq d8 s9 s4 d10 c7 d4 s2)) (5 1 2 5) |
My answer:
1 2 3 4 5 6 |
(define (suit-counts sent) (se (count-suit 's sent) (count-suit 'h sent) (count-suit 'c sent) (count-suit 'd sent))) |
Suit-dist-points
Write
suit-dist-points
that takes a number as its argument, interpreting it as the number of cards in a suit. The procedure should return the number of distribution points your hand gets for having that number of cards in a particular suit.
1 2 3 4 5 6 7 8 9 10 |
> (suit-dist-points 2) 1 > (suit-dist-points 7) 0 > (suit-dist-points 0) 3 |
My answer:
1 2 3 4 5 6 7 |
(define (suit-dist-points cards_in_suit) (cond ((equal? cards_in_suit 2) 1) ((equal? cards_in_suit 1) 2) ((equal? cards_in_suit 0) 3) (else 0))) |
Hand-dist-points
Write
hand-dist-points
, which takes a hand as its argument and returns the number of distribution points the hand is worth.
1 2 3 4 5 6 7 |
> (hand-dist-points '(sa s10 s7 s6 s2 hq hj h9 ck c4 dk d9 d3)) 1 > (hand-dist-points '(h3 d7 sk s3 c10 dq d8 s9 s4 d10 c7 d4 s2)) 3 |
My answer:
1 2 3 |
(define (hand-dist-points hand) (accumulate + (every suit-dist-points (suit-counts hand)))) |
Bridge-val
Write a procedure
bridge-val
that takes a hand as its argument and returns the total number of points that the hand is worth.
1 2 3 4 5 6 7 |
> (bridge-val '(sa s10 s7 s6 s2 hq hj h9 ck c4 dk d9 d3)) 14 > (bridge-val '(h3 d7 sk s3 c10 dq d8 s9 s4 d10 c7 d4 s2)) 8 |
My answer:
1 2 3 4 |
(define (bridge-val hand) (+ (hand-dist-points hand) (high-card-points hand))) |
Full Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
(define (card-val card) (cond ((equal? (bf card) 'a) 4) ((equal? (bf card) 'k) 3) ((equal? (bf card) 'q) 2) ((equal? (bf card) 'j) 1) (else 0))) (define (high-card-points hand) (accumulate + (every card-val hand))) (define (count-suit suit sent) (appearances suit (accumulate word sent))) (define (suit-counts sent) (se (count-suit 's sent) (count-suit 'h sent) (count-suit 'c sent) (count-suit 'd sent))) (define (suit-dist-points cards_in_suit) (cond ((equal? cards_in_suit 2) 1) ((equal? cards_in_suit 1) 2) ((equal? cards_in_suit 0) 3) (else 0))) (define (hand-dist-points hand) (accumulate + (every suit-dist-points (suit-counts hand)))) (define (bridge-val hand) (+ (hand-dist-points hand) (high-card-points hand))) |
Other People’s Solutions
I liked AnneB’s suit-counts
:
1 2 3 |
(define (suit-counts hand) (every (lambda (suit) (count-suit suit hand)) ‘(s h c d)) ) |