{"id":3799,"date":"2019-10-19T00:01:18","date_gmt":"2019-10-19T07:01:18","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3799"},"modified":"2019-10-26T08:43:39","modified_gmt":"2019-10-26T15:43:39","slug":"four-of-a-kind-three-of-a-kind-and-a-full-house-poker-vii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3799","title":{"rendered":"Four-of-a-Kind, Three-of-a-kind, and a Full House (Poker VII)"},"content":{"rendered":"<p>To effectively evaluate poker hands, the next sequence after a straight, straight flush, and flush draw (covered in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3789\">last week&#8217;s Lesson<\/a>) is to evaluate four-of-a-kind, then three-of-a-kind, and (while you&#8217;re at it) a full house.<br \/>\n<!--more--><br \/>\nAfter the hand of playing cards is sorted by value, the four-of-a-kind test has only two possible solutions: the first four cards all have the same value or the last four cards all have the same value. Here&#8217;s the <em>fourkind()<\/em> function I wrote to test both conditions:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* only two possible hand patterns for\r\n   four-of-a-kind *\/<\/span>\r\nint fourkind(struct playing_card p[])\r\n{\r\n    <span class=\"comments\">\/* test the first four cards *\/<\/span>\r\n    if( p[0].value==p[1].value &amp;&amp; p[1].value==p[2].value &amp;&amp; p[2].value==p[3].value )\r\n        return(TRUE);\r\n    <span class=\"comments\">\/* test the last four cards *\/<\/span>\r\n    if( p[1].value==p[2].value &amp;&amp; p[2].value==p[3].value &amp;&amp; p[3].value==p[4].value )\r\n        return(TRUE);\r\n\r\n    return(FALSE);\r\n}<\/pre>\n<p>The <em>if<\/em> statements are logically complex but serve the task of identifying the two possible patterns for a four-of-a-kind draw. When all the card values compare positively, <code>TRUE<\/code> is returned.<\/p>\n<p>I used a similar approach to evaluate a three-of-a-kind draw. In this pattern, the first three cards, middle three cards, or last three cards must have the same values to generate a true condition. Here is my <em>threekind()<\/em> function:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* three possible hand patterns for\r\n   three-of-a-kind *\/<\/span>\r\nint threekind(struct playing_card p[])\r\n{\r\n    <span class=\"comments\">\/* test the first three cards *\/<\/span>\r\n    if( p[0].value==p[1].value &amp;&amp; p[1].value==p[2].value )\r\n        return(TRUE);\r\n    <span class=\"comments\">\/* test the next three cards *\/<\/span>\r\n    if( p[1].value==p[2].value &amp;&amp; p[2].value==p[3].value )\r\n        return(TRUE);\r\n    <span class=\"comments\">\/* test the last three cards *\/<\/span>\r\n    if( p[2].value==p[3].value &amp;&amp; p[3].value==p[4].value )\r\n        return(TRUE);\r\n\r\n    return(FALSE);\r\n}<\/pre>\n<p>After coding these two functions, I ran some tests. I updated the <em>playing_card<\/em> <code>hand[][]<\/code> array to add some positives for four-of-a-kind and three-of-a-kind draws, as well as keeping some of the positive matches for straights and flushes. You can view the full code for this draft on <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2019_10_19-Lesson-a.c\" rel=\"noopener noreferrer\" target=\"_blank\">Github<\/a>. Here&#8217;s sample output:<\/p>\n<p><code>Hand 1: 5&#x2666; 5&#x2660; 6&#x2666; 9&#x2666; 9&#x2665;<br \/>\nHand 2: 2&#x2660; 4&#x2660; 6&#x2660; 10&#x2660; K&#x2660; - Flush<br \/>\nHand 3: 6&#x2660; 7&#x2663; 7&#x2665; 9&#x2665; J&#x2666;<br \/>\nHand 4: 4&#x2665; 5&#x2665; 6&#x2665; 7&#x2665; 8&#x2665; - Straight Flush<br \/>\nHand 5: A&#x2660; A&#x2665; A&#x2666; A&#x2663; K&#x2665; - Four-of-a-kind<br \/>\nHand 6: A&#x2666; 4&#x2663; 4&#x2660; 4&#x2665; 4&#x2666; - Four-of-a-kind<br \/>\nHand 7: 5&#x2660; 5&#x2663; 5&#x2666; 9&#x2663; J&#x2665; - Three-of-a-kind<br \/>\nHand 8: A&#x2660; 9&#x2665; 9&#x2666; 9&#x2663; Q&#x2665; - Three-of-a-kind<br \/>\nHand 9: A&#x2660; A&#x2665; K&#x2666; K&#x2663; K&#x2665; - Three-of-a-kind<\/code><\/p>\n<p>The next logical hand to evaluate is a full house, which is a combination three-of-a-kind and a pair. After much sweat and gnashing of teeth, I determined that a separate <em>fullhouse()<\/em> function would be redundant to the <em>threekind()<\/em> function. So I modified the <em>threekind()<\/em> function to also detect a full house:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* three possible hand patterns for\r\n   three-of-a-kind  - and full house *\/<\/span>\r\nint threekind(struct playing_card p[])\r\n{\r\n    <span class=\"comments\">\/* test the first three cards *\/<\/span>\r\n    if( p[0].value==p[1].value &amp;&amp; p[1].value==p[2].value )\r\n    {\r\n        <span class=\"comments\">\/* also test for full house *\/<\/span>\r\n        if( p[3].value==p[4].value )\r\n            return(2);\r\n        else\r\n            return(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* test the next three cards *\/<\/span>\r\n    if( p[1].value==p[2].value &amp;&amp; p[2].value==p[3].value )\r\n        return(TRUE);\r\n    \r\n    <span class=\"comments\">\/* test the last three cards *\/<\/span>\r\n    if( p[2].value==p[3].value &amp;&amp; p[3].value==p[4].value )\r\n    {\r\n        <span class=\"comments\">\/* also test for a full house *\/<\/span>\r\n        if( p[0].value==p[1].value )\r\n            return(2);\r\n        else\r\n            return(1);\r\n    }\r\n\r\n    return(FALSE);\r\n}<\/pre>\n<p>With the cards in the hand sorted, only two arrangements are possible for a full house: a pair and three-of-a-kind or three-of-a-kind and a pair. The first and last <em>if<\/em> tests in the <em>threekind()<\/em> function check for both conditions.<\/p>\n<p>I also modified the return values from this function: <code>FALSE<\/code>, or zero, means no pattern was found; 1 (<code>TRUE<\/code>) means the function found a three-of-a-kind match; 2 is returned for a full house. Modifications were also made to the <em>main()<\/em> function both to show sample full house draws in the <code>hand[][]<\/code> array.<\/p>\n<p><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2019_10_19-Lesson-b.c\" rel=\"noopener noreferrer\" target=\"_blank\">Click here<\/a> to view the full code on Github. Here&#8217;s a sample run:<\/p>\n<p><code>Hand 1: 5&#x2666; 5&#x2660; 6&#x2666; 9&#x2666; 9&#x2665;<br \/>\nHand 2: 2&#x2660; 4&#x2660; 6&#x2660; 10&#x2660; K&#x2660; - Flush<br \/>\nHand 3: 6&#x2660; 7&#x2663; 7&#x2665; 9&#x2665; J&#x2666;<br \/>\nHand 4: 4&#x2665; 5&#x2665; 6&#x2665; 7&#x2665; 8&#x2665; - Straight Flush<br \/>\nHand 5: A&#x2660; A&#x2665; A&#x2666; A&#x2663; K&#x2665; - Four-of-a-kind<br \/>\nHand 6: A&#x2666; 4&#x2663; 4&#x2660; 4&#x2665; 4&#x2666; - Four-of-a-kind<br \/>\nHand 7: 5&#x2660; 5&#x2663; 5&#x2666; 9&#x2663; 9&#x2665; - Full House<br \/>\nHand 8: A&#x2660; 9&#x2665; 9&#x2666; 9&#x2663; Q&#x2665; - Three-of-a-kind<br \/>\nHand 9: A&#x2660; A&#x2665; K&#x2666; K&#x2663; K&#x2665; - Full House<\/code><\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3810\">next week&#8217;s Lesson<\/a> I add the final function, <em>pairs()<\/em>, which checks for one or two pairs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The tests for my poker program proceed, identifying a few high-ranking hands. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3799\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3799","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3799"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3799\/revisions"}],"predecessor-version":[{"id":3825,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3799\/revisions\/3825"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}