{"id":3810,"date":"2019-10-26T00:01:46","date_gmt":"2019-10-26T07:01:46","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3810"},"modified":"2019-11-02T09:33:17","modified_gmt":"2019-11-02T16:33:17","slug":"one-or-two-pairs-poker-viii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3810","title":{"rendered":"One or Two Pairs (Poker VIII)"},"content":{"rendered":"<p>Matching two card values in a poker hand counts as a pair. It&#8217;s the lowest-ranking hand (above the non-ranked &#8220;high card&#8221;) and the most common. In my poker program, it&#8217;s also the last ranking hand tested.<br \/>\n<!--more--><br \/>\nBecause the winning hands four-of-a-kind, three-of-a-kind, and full house have already been eliminated in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3799\">last week&#8217;s Lesson<\/a>, the single pair hand isn&#8217;t that complex of a test. What I discovered when coding my <em>pairs()<\/em> function, however, was that it also makes sense to check for two pairs after the first pair is detected.<\/p>\n<p>The <em>playing_card<\/em> array <code>p[]<\/code> is sorted by card value. In this arrangement, only four possible positions exist for a pair:<\/p>\n<p><code>p[0].value==p[1].value<br \/>\np[1].value==p[2].value<br \/>\np[2].value==p[3].value<br \/>\np[3].value==p[4].value<\/code><\/p>\n<p>If checked in the above order, only three possibilities exist for two pairs:<\/p>\n<p><code>p[0].value==p[1].value && p[2].value==p[3].value<br \/>\np[0].value==p[1].value && p[3].value==p[4].value<br \/>\np[1].value==p[2].value &&p[3].value==p[4].value<\/code><\/p>\n<p>These combinations can be tested in sequence, which is what I did in my <em>pairs()<\/em> function:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* Check for pairs *\/<\/span>\r\nint pairs(struct playing_card p[])\r\n{\r\n    <span class=\"comments\">\/* test the first two cards *\/<\/span>\r\n    if( p[0].value==p[1].value )\r\n    {\r\n        <span class=\"comments\">\/* check for a second pair *\/<\/span>\r\n        if( p[2].value==p[3].value || p[3].value==p[4].value )\r\n            return(2);\r\n        else\r\n            return(TRUE);\r\n    }\r\n\r\n    <span class=\"comments\">\/* test the second two cards *\/<\/span>\r\n    if( p[1].value==p[2].value )\r\n    {\r\n        <span class=\"comments\">\/* check for a second pair *\/<\/span>\r\n        if( p[3].value==p[4].value )\r\n            return(2);\r\n        else\r\n            return(TRUE);\r\n    }\r\n\r\n    <span class=\"comments\">\/* test for a pair at the 3rd position *\/<\/span>\r\n    if( p[2].value==p[3].value )\r\n        return(TRUE);\r\n\r\n    <span class=\"comments\">\/* test for a pair at the final position *\/<\/span>\r\n    if( p[3].value==p[4].value )\r\n        return(TRUE);\r\n\r\n    return(FALSE);\r\n}<\/pre>\n<p>The first <em>if<\/em> test compares values for the first two cards. If true, the second pair (at two positions) is also tested.<\/p>\n<p>The second <em>if<\/em> test checks for a pair at the second position, as well as a second pair at the fourth position.<\/p>\n<p>The third <em>if<\/em> text checks for a pair at the third position, and the final <em>if<\/em> test checks for a pair at the fourth position.<\/p>\n<p>Like the <em>threekind()<\/em> function (covered in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3799\">last week&#8217;s Lesson<\/a>), this function returns values other than TRUE and FALSE: TRUE (1) for a pair, 2 for two pair, and FALSE (0) for no match.<\/p>\n<p>The <em>main()<\/em> function is updated to call and evaluate the <em>pairs()<\/em> function and evaluate its return value. I also added the final test, which returns the high-card value:<\/p>\n<p><code>wprintf(L\" - High card %s%lc\\n\",hand[x][a].face,hand[x][a].suit);<\/code><\/p>\n<p><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2019_10_26-Lesson.c\" rel=\"noopener noreferrer\" target=\"_blank\">Click here<\/a> to view the full code on GitHub.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Hand 1: 5&#x2666; 5&#x2660; 6&#x2666; 9&#x2666; 9&#x2665; - Two Pairs<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; - One Pair<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=3816\">next week&#8217;s Lesson<\/a>, I wrap up the program. The original randomizer and sorter are restored as the code simulates drawing a poker hand and reporting its value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After eliminating all other possibilities, the final poker hand tests are for one or two pairs. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3810\">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-3810","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\/3810","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=3810"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3810\/revisions"}],"predecessor-version":[{"id":3841,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3810\/revisions\/3841"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}