CMPU-145, Spring 2013 Lab 8 April 15, 2013 ==> Note: For this lab, you are encouraged to work in pairs. However, each person should type up his/her own definitions file, which will be submitted electronically as usual. If you work in a pair, make sure your work gets checked out!! For this lab, you will use the helper file, lab8-helper.txt, that contains the code seen today in class, but with all of the tester expressions commented out. 1. Recall from class today that a FULL HOUSE is a 5-card hand that contains three cards of one rank, plus two cards of another rank. For example, a hand containing three kings and two sixes is a full house, as is a hand containing three fours and two jacks. Note that *suit* plays no role in a full house. Recall further that the fraction of all 5-card hands that have a full house is about .00144. Stated differently, the probability that a randomly generated 5-card poker hand has a full house is about .00144. For this problem, you will use the GEN-AND-TEST and GEN-POKER-HAND functions seen in class. You will need to define a new function, called HAS-FULL-HOUSE?, that takes a list of five "cards" (i.e., numbers from 0 to 51) and determines whether or not that list of cards contains a full house. Recall the "trick" of using an expression such as (sort (map rank cards) <=) to generate a sorted list of the ranks of a given list of cards. For a full house, the pattern of such a list should be either (X X X Y Y) or (X X Y Y Y), depending upon whether the rank of the triple is lower or higher than the rank of the pair. Consider the following interactions: > (define *cardies* (list 8 10 34 21 23)) > (sort (map rank *cardies*) <=) (9 9 9 11 11) > (has-full-house? *cardies*) #t > (define *cardz* (list 8 10 35 21 23)) > (sort (map rank *cardz*) <=) (9 9 10 11 11) > (has-full-house? *cardz*) #f After you define this function, compare the result obtained by evaluating an expression such as (gen-and-test 1000 gen-poker-hand has-full-house?) How big does the first input of GEN-AND-TEST have to be before your answer gets close to the "real answer"? NOTE: If your answer does get close the "real answer", then you probably have an error in your HAS-FULL-HOUSE? function. 2. Recall from class that a 5-card poker hand is said to have "two pair" if it contains two cards of one rank, two cards of another rank, and one last card of still another rank. For example, a hand containing two sixes, two eights, and a jack has "two pair". Note that a full house does not count as having "two pair"; neither does a hand with four of a kind. Recall further that the probability of a randomly generated 5-card hand having two-pair is approximately .047539. For this problem, you will use GEN-AND-TEST and GEN-POKER-HAND in a manner similar to what you did for problem 1. You will need to define a new function, called HAS-TWO-PAIR?, that determines whether a given list of five cards has exactly two pair (not a full house or four of a kind). Note that after sorting the ranks of the cards in a hand with two pair, the pattern will be one of the following: (X X Y Y Z) (X X Y Z Z) (X Y Y Z Z) depending on the ranks of the pairs and the non-matching card. Make sure that you explicitly rule out hands having a full house or four of a kind! When your function is working properly, then an expression such as (gen-and-test 10000 gen-poker-hand has-two-pair?) should generate an answer fairly close to the "correct answer". Consider the following sorts of interactions: > (define *cards* (gen-poker-hand)) > *cards* (32 40 13 22 36) > (define *ranks* (map rank *cards*)) (7 2 1 10 11) > (define *sorted-ranks* (sort *ranks* <=)) (1 2 7 10 11) > (has-two-pair? *cards*) #f > (define *cards2* (list 8 15 21 28 29)) > (sort (map rank *cards2*) <=) (3 3 4 9 9) > (has-two-pair? *cards2*) #t 3. A 5-card hand has a "straight" if the ranks of its cards, when sorted, form one of the following patterns: (1 2 3 4 5) (2 3 4 5 6) (3 4 5 6 7) (4 5 6 7 8) (5 6 7 8 9) ... (9 10 11 12 13) (10 11 12 13 1) <---- Notice that an "ace" counts as the top card in this straight!! Define a function, called HAS-STRAIGHT?, that returns #t if a given list of "cards" (i.e., numbers from 0 to 51, inclusive) contains a "straight". Here are some examples: > (sort (map rank (list 19 31 7 9 47)) <=) (6 7 8 9 10) > (has-straight? (list 19 31 7 9 47)) #t > (sort (map rank (list 19 32 7 9 47)) <=) (7 7 8 9 10) > (has-straight? (list 19 32 7 9 47)) #f > (sort (map rank (list 1 3 5 7 9)) <=) (2 4 6 8 10) > (has-straight? (list 1 3 5 7 9)) #f After your function is working, evaluate an expression such as: (gen-and-test 1000 gen-poker-hand has-straight?). The answer should come out to be about: 0.0039400. (You will compute this answer in your assignment.) ======================= When you are done, make sure that a coach checks your work. Then submit electronically, as usual.