Guest User

Untitled

a guest
Jan 4th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.76 KB | None | 0 0
  1. (define-struct card (suit rank))
  2.  
  3.  
  4. ;; bestsuit: card card - > string
  5. ;;Helping function that distinguishes the better suit of two cards
  6.  
  7. (define (bestsuit a b)
  8.   (if (equal? (card-suit a) 'spades)
  9.       'spades
  10.       (if (or (equal? (card-suit a) 'hearts) (equal? (card-suit b) 'hearts))
  11.           'hearts
  12.           (if (or (equal? (card-suit a) 'diamonds)(equal? (card-suit b) 'diamonds))
  13.               'diamonds
  14.               'clubs))))
  15.  
  16.  
  17.  
  18. ;;bestrank nat nat - > nat
  19. ;;Helping function that distinguises the highest rank of two cards.
  20.  
  21. (define (bestrank a b)
  22.   (cond
  23.     [(< (card-rank a) (card-rank b)) (card-rank b)]
  24.     [(> (card-rank a) (card-rank b)) (card-rank a)]
  25.     [(equal? (card-rank a) (card-rank b)) (card-rank a)]))
  26.  
  27. ;;better-card card card - > card
  28. ;;This function will intake two cards and then produce
  29. ;;the card with the highest suit. If both suits are equal,
  30. ;;the function will produce the card with highest rank.
  31.  
  32. (define (better-card a b)
  33.   (if (equal? (card-suit a)(card-suit b))
  34.       (make-card (card-suit a) (bestrank a b))
  35.       (if (equal? (bestsuit a b) a)
  36.           (make-card (card-suit a)(card-rank a))
  37.           (make-card (card-suit b)(card-rank b)))))
  38.  
  39.  
  40. ;;straight: card card card - > bool
  41. ;;Helping function which will consume three card and, if they are of the same suit,
  42. ;;will produce either true if each card has a distance of one from another, or
  43. ;;false if this is not the case.
  44.  
  45. (define (straight a b c)
  46.   (if (equal? (card-suit a)(card-suit b)(card-suit c))
  47.       (if (and (or
  48.                 (equal? (- (bestrank (bestrank a b) c) 1)(card-rank a))
  49.                 (equal? (- (bestrank (bestrank a b) c) 1)(card-rank b))
  50.                 (equal? (- (bestrank (bestrank a b) c) 1)(card-rank c)))
  51.                (equal? (- (bestrank (bestrank a b) c) 2)(card-rank a))
  52.                (equal? (- (bestrank (bestrank a b) c) 2)(card-rank b))
  53.                (equal? (- (bestrank (bestrank a b) c) 2)(card-rank c)))
  54.           true
  55.           false)
  56.       false))
  57.  
  58. ;;hand-value: card card card - > string
  59. ;;This function will consume three cards and produce
  60. ;;a string corresponding to the rules at the beginning
  61. ;;of this assigment.
  62.  
  63. (define (hand-value a b c)
  64.   (if (equal? (card-suit a)(card-suit b)(card-suit c))
  65.       (if (straight a b c)
  66.           'straightflush
  67.           'flush)
  68.       (if (straight a b c)
  69.           'straight
  70.           (if (and (equal? (card-rank a)(card-rank b))
  71.                    (equal? (card-rank b)(card-rank c)))
  72.               'three-of-a-kind
  73.               (if (or (equal? (card-rank a)(card-rank b))
  74.                       (equal? (card-rank b)(card-rank c))
  75.                       (equal? (card-rank c)(card-rank a)))
  76.                   'pair
  77.                   'high-card)))))
Add Comment
Please, Sign In to add comment