;;; =========================== ;;; CMPU-2013, Spring 2013 ;;; Lab 8 Solutions ;;; April 15, 2013 ;;; =========================== (load "lab8-helper.txt") (problem "1: Using GEN-AND-TEST with FULL-HOUSE") (define has-full-house? (lambda (cards) (let ((sorted-ranks (sort (map rank cards) <=))) ;; Pattern is either: (X X X Y Y) or (X X Y Y Y) (or (and (= (first sorted-ranks) (third sorted-ranks)) (= (fourth sorted-ranks) (fifth sorted-ranks))) (and (= (first sorted-ranks) (second sorted-ranks)) (= (third sorted-ranks) (fifth sorted-ranks))))))) (tester '(gen-and-test 100000 gen-poker-hand has-full-house?)) (tester '*prob-full-house*) (problem "2: Using GEN-AND-TEST with TWO-PAIR") (define has-two-pair? (lambda (cards) (let ((sorted-ranks (sort (map rank cards) <=))) ;; Pattern is one of: (X X Y Y Z), (X X Y Z Z) or (X Y Y Z Z) (or (and (= (first sorted-ranks) (second sorted-ranks)) (not (= (second sorted-ranks) (third sorted-ranks))) (or (and (= (third sorted-ranks) (fourth sorted-ranks)) (not (= (fourth sorted-ranks) (fifth sorted-ranks)))) (and (not (= (third sorted-ranks) (fourth sorted-ranks))) (= (fourth sorted-ranks) (fifth sorted-ranks))))) (and (not (= (first sorted-ranks) (second sorted-ranks))) (= (second sorted-ranks) (third sorted-ranks)) (not (= (third sorted-ranks) (fourth sorted-ranks))) (= (fourth sorted-ranks) (fifth sorted-ranks))))))) (tester '(gen-and-test 10000 gen-poker-hand has-two-pair?)) (tester '(gen-and-test 10000 gen-poker-hand has-two-pair?)) (tester '(gen-and-test 10000 gen-poker-hand has-two-pair?)) (tester '*prob-two-pair*) (problem "3: Using GEN-AND-TEST with a STRAIGHT") ;;; Don't care if has straight flush (define has-straight? (lambda (cards) (let ((sorted-ranks (sort (map rank cards) <=))) (or (equal? sorted-ranks '(1 10 11 12 13)) (and (all-diff? sorted-ranks) (= (+ 4 (first sorted-ranks)) (fifth sorted-ranks))))))) (define all-diff? (lambda (listy) (or (null? listy) (null? (rest listy)) (and (not (= (first listy) (second listy))) (all-diff? (rest listy)))))) (define *num-straights* (* 10 ;; 10 kinds of straights (depends only on lowest card 4 ;; pick a suit for first card 4 ;; pick a suit for second card 4 ;; etc. 4 4)) (define *prob-straight* (/ *num-straights* *num-5-card-hands* 1.0)) (tester '*prob-straight*) (tester '(gen-and-test 10000 gen-poker-hand has-straight?)) (tester '(gen-and-test 10000 gen-poker-hand has-straight?)) (tester '(gen-and-test 10000 gen-poker-hand has-straight?))