Due: Mar. 17, 2009 CS 480 Spring 2009 Dr. Duric Homework #3 (50p) In your third homework you will write functions to evaluate poker hands. You will write a function CompareHands which will take two poker hands of five cards each and determine which hand is better. The function should return 1 if the first hand is better, 0 if the hands are equal, and -1 if the second hand is better. Each hand is given as a list of five pairs (card suit), for example: (setf hand1 '((2 C) (8 C) (J C) (5 C) (K C))) (setf hand2 '((8 S) (7 S) (2 S) (J S) (K S))) (CompareHands hand1 hand2) should return -1. The rules that determine strengths of the hands are given at: http://boardgames.about.com/cs/poker/a/poker_hands.htm Notice that in case of ties you need to apply tie-breaking rules: http://www.pokerhands.com/poker_hand_tie_rules.html You can write as many helpe functions as you want to work with your CompareHands function. Here is an implementation that uses several functions: Please note that this is not a requirement. You can write as many functions as you want as long as they satisfy the requirements in the assignment. These are the functions in a possible implementation: ------------------------ ;; NumericalHand ;; replace A by 15, J by 12, Q by 13, K by 14 ;; replace A by 1 only if there is a Straight (1 2 3 4 5) (defun NumericalHand (hand) ( ) ;; returns a hand in which letter are replaced by numbers Examples: (NumericalHand '((2 C) (8 C) (J C) (5 C) (K C))) ((2 C) (5 C) (8 C) (12 C) (14 C)) (NumericalHand '((2 C) (A C) (3 H) (4 C) (5 D))) ((1 C) (2 C) (3 H) (4 C) (5 D)) ------------------------- ;; Sort hand by card value (defun SortHand (hand) (sort hand #'(lambda (x y) (< (first x) (first y))))) ------------------------- ;; EvaluateHand ;; Evaluate hand from strongest to weakest ;; Input: 5 card hand ;; Return value: ;; ((RoyalFlush Suit) ;; have royal flush give suit, otherwise nil ;; (StraightFlush HighCard Suit) ;; high card value & suit, otherwise nil ;; (FourOfAKind CardVal) ;; (FullHouse HighCardVal LowCardVal) ;; HighCardVal - three of a kind value, LowCard - pair value ;; (Flush HighCard Suit) ;; (Straight HighCard) ;; (ThreeOfAKind CardVal) ;; (TwoPair HighCardVal LowCardVal) ;; (Pair CardVal) ;; HighCardVal) (defun EvaluateHand (hand) () Examples: 260]> (EvaluateHand (NumericalHand '((2 C) (A C) (3 H) (4 C) (5 D)))) (NIL NIL NIL NIL NIL (STRAIGHT 5) NIL NIL NIL 15) [261]> (EvaluateHand (NumericalHand '((2 C) (10 C) (J C) (Q C) (K C)))) (NIL NIL NIL NIL (FLUSH 14 C) NIL NIL NIL NIL 14) [262]> (EvaluateHand (NumericalHand '((8 S) (10 S) (10 D) (10 H) (10 C)))) (NIL NIL (FOUROFAKIND 10) NIL NIL NIL NIL NIL NIL 10) [263]> (EvaluateHand (NumericalHand '((8 S) (10 S) (A D) (10 H) (10 C)))) (NIL NIL NIL NIL NIL NIL (THREEOFAKIND 10) NIL NIL 15) [264]> (EvaluateHand (NumericalHand '((8 S) (10 S) (A D) (6 H) (10 C)))) (NIL NIL NIL NIL NIL NIL NIL NIL (PAIR 10) 15) ----------------------- ;; function to break ties; compare two hands until one card is higher or done ;; return 1 if hand1 is better, -1 if hand2 is better and 0 otherwise ;; only card values are given in reverse order (from highest to lowest) (defun BreakTies (cvals1 cvals2) ;; (format t "~% cvals1 = ~A cvals2 = ~A ~%" cvals1 cvals2) (cond ((or (null cvals1) (null cvals2)) 0) ((> (first cvals1) (first cvals2)) 1) ((< (first cvals1) (first cvals2)) -1) (t (BreakTies (rest cvals1) (rest cvals2)))) ) ----------------------- ;; COMPAREHANDS ;; given two hands of five cards each determine if hand1 is stronger than hand2 ;; there are three possible return values ;; 1: hand1 is better ;; -1: hand2 is better ;; 0: hands are equal (defun CompareHands (hand1 hand2) () Examples: [265]> (setf hand1 '((2 C) (8 C) (J C) (5 C) (K C))) ((2 C) (8 C) (J C) (5 C) (K C)) [266]> (setf hand2 '((8 S) (7 S) (2 S) (J S) (K S))) ((8 S) (7 S) (2 S) (J S) (K S)) [267]> (CompareHands hand1 hand2) -1 Instructions for submission: ---------------------------- (1) Using script or dribble, you are to capture the output of a Lisp session in which you successfully load and execute your code, showing sufficient testing of your function(s). (2) Send a SINGLE email to zduric@cs.gmu.edu formatted in the following way: - the subject field of the email should read: CS480 HW#3 Duric - the content of the email should be : (a) a cover sheet containing: Your name CS480 Spring 2009 Dr. Duric Homework #3 (A short summary stating whether you were successful, describing any problems, etc.) (b) a commented version of the Lisp code for HW3. Comments at the top should include: Your name CS480 Spring 2009 Dr. Duric Homework #3 (c) a printout of a captured Lisp session involving the execution of the HW3 Lisp code As a safety precaution, always CC yourself when you submit homework this way and keep it around until it has been graded and returned.