Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (define ranks '(a k q j 10 9 8 7 6 5 4 3 2))
- (define suits '(s h d c))
- (define (numeric-rank rank)
- (cond ((equal? rank 'a) 14)
- ((equal? rank 'k) 13)
- ((equal? rank 'q) 12)
- ((equal? rank 'j) 11)
- (else rank)))
- (define (make-card suit rank)
- (word suit rank))
- (define (rank card) (bf card))
- (define (suit card) (first card))
- (define (sorting hand)
- ((repeated sort-once (- (count hand) 1)) hand))
- (define (sort-once hand)
- (cond ((empty? hand) hand)
- ((= (count hand) 1) hand)
- ((> (numeric-rank (bf (first hand)))
- (numeric-rank (bf (first (bf hand)))))
- (se (first hand) (sort-once (bf hand))))
- (else (se (first (bf hand))
- (sort-once (se (first hand) (bf (bf hand))))))))
- (define (poker-value hand)
- (let ((sortedhand (sorting hand)))
- (cond ((royal-flush? sortedhand) ??)
- ((straight-flush? sortedhand) ??)
- ((four-of-a-kind? sortedhand) ??)
- ((full-house? sortedhand) ??)
- ((flush? sortedhand) ??)
- ((straight? sortedhand) ??)
- ((three-of-a-kind? sortedhand) ??)
- ((two-pair? sortedhand) ??)
- ((pair? sortedhand) ??)
- (else ??))))
- (define (convert-to-suit x)
- (cond ((equal? x 'd) 'diamonds)
- ((equal? x 'h) 'hearts)
- ((equal? x 'c) 'clubs)
- ((equal? x 's) 'spades)
- (else 'rainbows)))
- (define (convert-to-num x)
- (cond ((equal? x 1) 'ones)
- ((equal? x 2) 'twos)
- ((equal? x 3) 'threes)
- ((equal? x 4) 'fours)
- ((equal? x 5) 'fives)
- ((equal? x 6) 'sixes)
- ((equal? x 7) 'sevens)
- ((equal? x 8) 'eights)
- ((equal? x 9) 'nines)
- ((equal? x 10) 'tens)
- ((equal? x 'j) 'jacks)
- ((equal? x 'q) 'queens)
- ((equal? x 'k) 'kings)
- ((equal? x 'a) 'aces)
- (else 'rainbows)))
- (define ranks '(a k q j 10 9 8 7 6 5 4 3 2))
- (define suits '(s h d c))
- (define (numeric-rank rank)
- (cond ((equal? rank 'a) 14)
- ((equal? rank 'k) 13)
- ((equal? rank 'q) 12)
- ((equal? rank 'j) 11)
- (else rank)))
- (define (make-card suit rank)
- (word suit rank))
- (define (rank card) (bf card))
- (define (suit card) (first card))
- ;sorts all the cards in the hand based on numeric value, high to low
- (define (sorting hand)
- ((repeated sort-once (- (count hand) 1)) hand))
- ;sorts first two cards of hand based on numeric value, high to low
- (define (sort-once hand)
- (cond ((empty? hand) hand)
- ((= (count hand) 1) hand)
- ((> (numeric-rank (bf (first hand)))
- (numeric-rank (bf (first (bf hand)))))
- (se (first hand) (sort-once (bf hand))))
- (else (se (first (bf hand))
- (sort-once (se (first hand) (bf (bf hand))))))))
- (define (royal-flush? hand)
- (let ((sortedhand (sorting hand)))
- (if (and (or (equal? (every first sortedhand) '(s s s s s)) (equal? (every first sortedhand) '(h h h h h)) (equal? (every first sortedhand) '(d d d d d)) (equal? (every first sortedhand) '(c c c c c)))
- (equal? (bf (first sortedhand)) 'a)
- (equal? (bf (first (bf sortedhand))) 'k)
- (equal? (bf (first (bf (bf sortedhand)))) 'q)
- (equal? (bf (first (bf (bf (bf sortedhand))))) 'j)
- (equal? (bf (first (bf (bf (bf (bf sortedhand)))))) '10)) #t #f)))
- (define (straight-flush? hand)
- (let ((sortedhand (sorting hand)))
- (if (or (and (or (equal? (every first sortedhand) '(s s s s s)) (equal? (every first sortedhand) '(h h h h h)) (equal? (every first sortedhand) '(d d d d d)) (equal? (every first sortedhand) '(c c c c c)))
- (equal? (- (numeric-rank (bf (first sortedhand))) 1) (numeric-rank (bf (first (bf sortedhand)))))
- (equal? (- (numeric-rank (bf (first (bf sortedhand)))) 1) (numeric-rank (bf (first (bf (bf sortedhand))))))
- (equal? (- (numeric-rank (bf (first (bf (bf sortedhand))))) 1) (numeric-rank (bf (first (bf (bf (bf sortedhand)))))))
- (equal? (- (numeric-rank (bf (first (bf (bf (bf sortedhand)))))) 1) (numeric-rank (bf (first (bf (bf (bf (bf sortedhand)))))))))
- (and (or (equal? (every first sortedhand) '(s s s s s)) (equal? (every first sortedhand) '(h h h h h)) (equal? (every first sortedhand) '(d d d d d)) ((equal? (every first sortedhand) '(c c c c c))))
- (equal? (bf (first sortedhand)) 'a)
- (equal? (bf (first (bf sortedhand))) 5)
- (equal? (bf (first (bf (bf sortedhand)))) 4)
- (equal? (bf (first (bf (bf (bf sortedhand))))) 3)
- (equal? (bf (last sortedhand)) 2)))
- (if (royal-flush? hand) #f #t) #f)))
- ;simplifying the code above by writing it recursively
- (define (straight-flush? hand)
- (let ((sortedhand (sorting hand)))
- (if (empty? hand)
- '#t
- (if (= (- (numeric-rank (bf (first sortedhand))) 1) (numeric-rank (bf (first (bf sortedhand))))) (straight-flush? (bf hand)) #f))))
- (define (ace? x)
- (if (equal? x 'a) #t #f))
- (define (2? x)
- (if (equal? x '2) #t #f))
- (define (3? x)
- (if (equal? x '3) #t #f))
- (define (4? x)
- (if (equal? x '4) #t #f))
- (define (5? x)
- (if (equal? x '5) #t #f))
- (define (6? x)
- (if (equal? x '6) #t #f))
- (define (7? x)
- (if (equal? x '7) #t #f))
- (define (8? x)
- (if (equal? x '8) #t #f))
- (define (9? x)
- (if (equal? x '9) #t #f))
- (define (10? x)
- (if (equal? x '10) #t #f))
- (define (jack? x)
- (if (equal? x 'j) #t #f))
- (define (queen? x)
- (if (equal? x 'q) #t #f))
- (define (king? x)
- (if (equal? x 'k) #t #f))
- (define (four-of-a-kind? hand)
- (if (or (= (count (keep ace? (every bf hand))) 4)
- (= (count (keep 2? (every bf hand))) 4)
- (= (count (keep 3? (every bf hand))) 4)
- (= (count (keep 4? (every bf hand))) 4)
- (= (count (keep 5? (every bf hand))) 4)
- (= (count (keep 6? (every bf hand))) 4)
- (= (count (keep 7? (every bf hand))) 4)
- (= (count (keep 8? (every bf hand))) 4)
- (= (count (keep 9? (every bf hand))) 4)
- (= (count (keep 10? (every bf hand))) 4)
- (= (count (keep jack? (every bf hand))) 4)
- (= (count (keep queen? (every bf hand))) 4)
- (= (count (keep king? (every bf hand))) 4)) #t #f))
- (define (full-house? hand)
- (if (and (or (= (count (keep ace? (every bf hand))) 3)
- (= (count (keep 2? (every bf hand))) 3)
- (= (count (keep 3? (every bf hand))) 3)
- (= (count (keep 4? (every bf hand))) 3)
- (= (count (keep 5? (every bf hand))) 3)
- (= (count (keep 6? (every bf hand))) 3)
- (= (count (keep 7? (every bf hand))) 3)
- (= (count (keep 8? (every bf hand))) 3)
- (= (count (keep 9? (every bf hand))) 3)
- (= (count (keep 10? (every bf hand))) 3)
- (= (count (keep jack? (every bf hand))) 3)
- (= (count (keep queen? (every bf hand))) 3)
- (= (count (keep king? (every bf hand))) 3))
- (or (= (count (keep ace? (every bf hand))) 2)
- (= (count (keep 2? (every bf hand))) 2)
- (= (count (keep 3? (every bf hand))) 2)
- (= (count (keep 4? (every bf hand))) 2)
- (= (count (keep 5? (every bf hand))) 2)
- (= (count (keep 6? (every bf hand))) 2)
- (= (count (keep 7? (every bf hand))) 2)
- (= (count (keep 8? (every bf hand))) 2)
- (= (count (keep 9? (every bf hand))) 2)
- (= (count (keep 10? (every bf hand))) 2)
- (= (count (keep jack? (every bf hand))) 2)
- (= (count (keep queen? (every bf hand))) 2)
- (= (count (keep king? (every bf hand))) 2))) #t #f))
- (define (flush? hand)
- (if (or (equal? (every first hand) '(s s s s s)) (equal? (every first hand) '(h h h h h)) (equal? (every first hand) '(d d d d d)) (equal? (every first hand) '(c c c c c))) (if (or (royal-flush? hand) (straight-flush? hand)) #f #t) #f))
- (define (straight? hand)
- (let ((sortedhand (sorting hand)))
- (if (or (and (not (or (equal? (every first sortedhand) '(s s s s s)) (equal? (every first sortedhand) '(h h h h h)) (equal? (every first sortedhand) '(d d d d d)) (equal? (every first sortedhand) '(c c c c c))))
- (equal? (- (numeric-rank (bf (first sortedhand))) 1) (numeric-rank (bf (first (bf sortedhand)))))
- (equal? (- (numeric-rank (bf (first (bf sortedhand)))) 1) (numeric-rank (bf (first (bf (bf sortedhand))))))
- (equal? (- (numeric-rank (bf (first (bf (bf sortedhand))))) 1) (numeric-rank (bf (first (bf (bf (bf sortedhand)))))))
- (equal? (- (numeric-rank (bf (first (bf (bf (bf sortedhand)))))) 1) (numeric-rank (bf (first (bf (bf (bf (bf sortedhand)))))))))
- (and (not (or (equal? (every first sortedhand) '(s s s s s)) (equal? (every first sortedhand) '(h h h h h)) (equal? (every first sortedhand) '(d d d d d)) (equal? (every first sortedhand) '(c c c c c))))
- (equal? (bf (first sortedhand)) 'a) (equal? (bf (first (bf sortedhand))) 5) (equal? (bf (first (bf (bf sortedhand)))) 4) (equal? (bf (first (bf (bf (bf sortedhand))))) 3) (equal? (bf (last sortedhand)) 2)))
- #t #f)))
- (define (three-of-a-kind? hand)
- (if (and (or (= (count (keep ace? (every bf hand))) 3)
- (= (count (keep 2? (every bf hand))) 3)
- (= (count (keep 3? (every bf hand))) 3)
- (= (count (keep 4? (every bf hand))) 3)
- (= (count (keep 5? (every bf hand))) 3)
- (= (count (keep 6? (every bf hand))) 3)
- (= (count (keep 7? (every bf hand))) 3)
- (= (count (keep 8? (every bf hand))) 3)
- (= (count (keep 9? (every bf hand))) 3)
- (= (count (keep 10? (every bf hand))) 3)
- (= (count (keep jack? (every bf hand))) 3)
- (= (count (keep queen? (every bf hand))) 3)
- (= (count (keep king? (every bf hand))) 3))
- (not (full-house? hand))) #t #f))
- (define (two-pair? hand))
- (if (and (or (= (count (keep ace? (every bf hand))) 2)
- (= (count (keep 2? (every bf hand))) 2)
- (= (count (keep 3? (every bf hand))) 2)
- (= (count (keep 4? (every bf hand))) 2)
- (= (count (keep 5? (every bf hand))) 2)
- (= (count (keep 6? (every bf hand))) 2)
- (= (count (keep 7? (every bf hand))) 2)
- (= (count (keep 8? (every bf hand))) 2)
- (= (count (keep 9? (every bf hand))) 2)
- (= (count (keep 10? (every bf hand))) 2)
- (= (count (keep jack? (every bf hand))) 2)
- (= (count (keep queen? (every bf hand))) 2)
- (= (count (keep king? (every bf hand))) 2))
- (not (or (full-house? hand) (two-pair? hand)))) #t #f))
- (define (pair hand)
- (if (and (or (= (count (keep ace? (every bf hand))) 2)
- (= (count (keep 2? (every bf hand))) 2)
- (= (count (keep 3? (every bf hand))) 2)
- (= (count (keep 4? (every bf hand))) 2)
- (= (count (keep 5? (every bf hand))) 2)
- (= (count (keep 6? (every bf hand))) 2)
- (= (count (keep 7? (every bf hand))) 2)
- (= (count (keep 8? (every bf hand))) 2)
- (= (count (keep 9? (every bf hand))) 2)
- (= (count (keep 10? (every bf hand))) 2)
- (= (count (keep jack? (every bf hand))) 2)
- (= (count (keep queen? (every bf hand))) 2)
- (= (count (keep king? (every bf hand))) 2))
- (not (or (full-house? hand) (two-pair? hand)))) #t #f))
- (define (poker-value hand)
- (let ((sortedhand (sorting hand))
- (cond ((royal-flush? sortedhand) (se '(royal flush) '(-) (convert-to-suit (first (first hand)))))
- ((straight-flush? sortedhand) (se '(straight flush) '(-) (convert-to-suit (first (first hand))))))
- ((four-of-a-kind? sortedhand) (se '(four) '(of) '(convert-to-num (bf (first hand))))
- ((full-house? sortedhand) (se '(full house) '(-) ')
- ((flush? sortedhand) "")
- ((straight? sortedhand) "")
- ((three-of-a-kind? sortedhand) "")
- ((two-pair? sortedhand) "")
- ((pair? sortedhand) "")
- (else "")))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement