{"id":3777,"date":"2019-10-05T00:01:34","date_gmt":"2019-10-05T07:01:34","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3777"},"modified":"2019-10-12T08:29:36","modified_gmt":"2019-10-12T15:29:36","slug":"straight-draw-poker-v","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3777","title":{"rendered":"Straight Draw (Poker V)"},"content":{"rendered":"<p>The first test for a hand of five cards is the straight, specifically an Ace-high straight followed by a standard straight. To perform this test, I&#8217;ve concocted a special version of the program, one that has a set of pre-drawn poker hands.<br \/>\n<!--more--><br \/>\nThe reason for presetting the poker hands is that random draws would take too long to test the code. It&#8217;s possible, of course, but it&#8217;s far more efficient to manually create an array of poker hands with positives and negatives and run test functions on those.<\/p>\n<p>You can view the full source code for this week&#8217;s Lesson on Github: <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2019_10_05-Lesson.c\" rel=\"noopener noreferrer\" target=\"_blank\">click here<\/a>.<\/p>\n<p>From <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3766\">last week&#8217;s Lesson<\/a>, I&#8217;ve updated the <em>main()<\/em> function to contain an array of 6 (set by the defined constant <code>HANDS<\/code>) preset <em>playing_card<\/em> structures:<\/p>\n<pre class=\"screen\">\r\nstruct playing_card hand[HANDS][HAND_SIZE] = {\r\n    { { DIAMONDS, \"5\", 5 }, { SPADES, \"5\", 5 }, { DIAMONDS, \"6\", 6 }, { DIAMONDS, \"9\", 9 }, { HEARTS, \"9\", 9 } },\r\n    { { SPADES, \"4\", 4 }, { HEARTS, \"6\", 6 }, { CLUBS, \"6\", 6 }, { HEARTS, \"7\", 7 }, { DIAMONDS, \"10\", 10 } },\r\n    { { SPADES, \"6\", 6 }, { CLUBS, \"7\", 7 }, { HEARTS, \"7\", 7 }, { HEARTS, \"9\", 9 }, { DIAMONDS, \"J\", 11 } },\r\n    { { HEARTS, \"4\", 4 }, { CLUBS, \"5\", 5 }, { DIAMONDS, \"6\", 6}, { CLUBS, \"7\", 7 }, { SPADES, \"8\", 8 } },\r\n    { { HEARTS, \"A\", 1 }, { HEARTS, \"3\", 3 }, { DIAMONDS, \"7\", 7 }, { CLUBS, \"K\", 13 }, { HEARTS, \"K\", 13 } },\r\n    { { DIAMONDS, \"A\", 1 }, { CLUBS, \"10\", 10 }, { CLUBS, \"J\", 11 }, { HEARTS, \"Q\", 12 }, { CLUBS, \"K\", 13 } }\r\n};<\/pre>\n<p>The cards in the <code>hand[]<\/code> array are pre-sorted, so the code need not sort them. Instead, the <em>main()<\/em> function outputs each hand, then calls the <em>straight()<\/em> function:<\/p>\n<p><code>r = straight(hand[x]);<\/code><\/p>\n<p>The function returns <code>TRUE<\/code> or <code>FALSE<\/code> (defined in the code) based on its evaluation. Here is the <em>straight()<\/em> function:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* Determine a straight draw *\/<\/span>\r\n    <span class=\"comments\">\/* This function assumes the HAND_SIZE is fixed at 5 *\/<\/span>\r\nint straight(struct playing_card p[])\r\n{\r\n    <span class=\"comments\">\/* straight, ace high *\/<\/span>\r\n    if( p[0].value==1 &amp;&amp; p[1].value==10 &amp;&amp; p[2].value==11 &amp;&amp; p[3].value==12 &amp;&amp; p[4].value==13 )\r\n        return(TRUE);\r\n\r\n    <span class=\"comments\">\/* other straight *\/<\/span>\r\n    if( p[0].value != p[1].value-1 )\r\n        return(FALSE);\r\n    if( p[1].value != p[2].value-1 )\r\n        return(FALSE);\r\n    if( p[2].value != p[3].value-1 )\r\n        return(FALSE);\r\n    if( p[3].value != p[4].value-1 )\r\n        return(FALSE);\r\n\r\n    return(TRUE);\r\n}<\/pre>\n<p>The first <em>if<\/em> test is a complex logical construction that checks for the condition: Ace, 10, Jack, Queen, King &mdash; an Ace-high straight. Remember, in my <em>playing_card<\/em> structure, the Ace is the lowest valued card, 1.<\/p>\n<p>The second clutch of <em>if<\/em> tests determine the sequential value for each card. Remember: They&#8217;re sorted. So if the first card, <code>p[0]<\/code>, is one less than the second card, <code>p[1]<\/code>, the testing continues. Each successive card is tested until the fourth, <code>p[3]<\/code>, is compared with the fifth, <code>p[4]<\/code>. When all the tests pass, the hand is a straight.<\/p>\n<p>Two straights are preset in the <code>hand[]<\/code> array to test the function&#8217;s accuracy. Here&#8217;s a sample run:<\/p>\n<p><code>Hand 1: 5&#x2665; 5&#x2660; 6&#x2665; 9&#x2665; 9&#x2663;<br \/>\nHand 2: 4&#x2660; 6&#x2663; 6&#x2666; 7&#x2663; 10&#x2665;<br \/>\nHand 3: 6&#x2660; 7&#x2666; 7&#x2663; 9&#x2663; J&#x2665;<br \/>\nHand 4: 4&#x2663; 5&#x2666; 6&#x2665; 7&#x2666; 8&#x2660; - Straight<br \/>\nHand 5: A&#x2663; 3&#x2663; 7&#x2665; K&#x2666; K&#x2663;<br \/>\nHand 6: A&#x2665; 10&#x2666; J&#x2666; Q&#x2663; K&#x2666; - Straight<\/code><\/p>\n<p>Hand 6 is a straight, but it looks awkward because the Ace is shown low. A correction could be made later in the code, or the code could present the hands as randomly drawn and internally sort itself. This change is made later in the program&#8217;s development.<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3789\">next week&#8217;s Lesson<\/a>, the code is updated to check for a flush and straight flush.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Test the <em>straight()<\/em> function to see whether it can detect a straight draw in a hand of cards. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3777\">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-3777","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\/3777","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=3777"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3777\/revisions"}],"predecessor-version":[{"id":3808,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3777\/revisions\/3808"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}