{"id":3753,"date":"2019-09-21T00:01:10","date_gmt":"2019-09-21T07:01:10","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3753"},"modified":"2019-09-28T08:07:36","modified_gmt":"2019-09-28T15:07:36","slug":"drawing-a-hand-of-cards-poker-iii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3753","title":{"rendered":"Drawing A Hand of Cards (Poker III)"},"content":{"rendered":"<p>Improving upon <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3746\">last week&#8217;s Lesson<\/a>, this update to the playing card simulator offers one minor change: Multiple cards are pulled from the <em>draw()<\/em> function.<br \/>\n<!--more--><br \/>\nAs each card is drawn, it&#8217;s stored in a <code>hand[]<\/code> array. Here&#8217;s the update to the <em>main()<\/em> function, which adds a <em>for<\/em> loop to last week&#8217;s code:<\/p>\n<pre class=\"screen\">\r\nint main()\r\n{\r\n    int deck[CARDS];\r\n    struct playing_card hand[HAND_SIZE];\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand((unsigned)time(NULL));\r\n    <span class=\"comments\">\/* activate wide characters *\/<\/span>\r\n    setlocale(LC_ALL,\"en_US.UTF-8\");\r\n\r\n    <span class=\"comments\">\/* initialize the deck *\/<\/span>\r\n        <span class=\"comments\">\/* 0 = card not drawn\r\n           1 = card drawn *\/<\/span>\r\n    for( x=0; x&lt;CARDS; x++)\r\n        deck[x] = 0;\r\n\r\n    <span class=\"comments\">\/* draw a hand of HAND_SIZE cards *\/<\/span>\r\n    printf(\"Your hand:\");\r\n    for( x=0; x&lt;HAND_SIZE; x++)\r\n    {\r\n        hand[x] = draw(deck);\r\n        wprintf(L\" %s%lc\",\r\n                hand[x].face,\r\n                hand[x].suit\r\n              );\r\n    }\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2019_09_21-Lesson-a.c\" rel=\"noopener noreferrer\" target=\"_blank\">Click here<\/a> to view the full code on Github.<\/p>\n<p>The <em>for<\/em> loop fills array <code>hand[]<\/code> with five cards from the <em>draw()<\/em> function. The rest of the code remains unchanged. Here&#8217;s a sample run:<\/p>\n<p><code>Your hand: 2&#9829; 10&#9830; K&#9827; 3&#9824; 2&#9824;<\/code><\/p>\n<p>I&#8217;m pleased with the results. To play a card game, however, the code must do what humans do when dealt a hand: sort the cards.<\/p>\n<p>To organize the output, I added a bubble sort to an update of the <em>main()<\/em> function:<\/p>\n<pre class=\"screen\">\r\nint main()\r\n{\r\n    int deck[CARDS];\r\n    struct playing_card hand[HAND_SIZE],temp;\r\n    int x,a,b;\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand((unsigned)time(NULL));\r\n    <span class=\"comments\">\/* activate wide characters *\/<\/span>\r\n    setlocale(LC_ALL,\"en_US.UTF-8\");\r\n\r\n    <span class=\"comments\">\/* initialize the deck *\/<\/span>\r\n        <span class=\"comments\">\/* 0 = card not drawn\r\n           1 = card drawn *\/<\/span>\r\n    for( x=0; x&lt;CARDS; x++)\r\n        deck[x] = 0;\r\n\r\n    <span class=\"comments\">\/* draw a hand of HAND_SIZE cards *\/<\/span>\r\n    for( x=0; x&lt;HAND_SIZE; x++)\r\n        hand[x] = draw(deck);\r\n    <span class=\"comments\">\/* sort the hand by value *\/<\/span>\r\n    for( a=0; a&lt;HAND_SIZE-1; a++)\r\n    {\r\n        for( b=a+1; b&lt;HAND_SIZE; b++ )\r\n        {\r\n            if( hand[a].value &gt; hand[b].value )\r\n            {\r\n                temp = hand[a];\r\n                hand[a] = hand[b];\r\n                hand[b] = temp;\r\n            }\r\n        }\r\n    }\r\n\r\n    printf(\"Your hand:\");\r\n    for( x=0; x&lt;HAND_SIZE; x++ )\r\n    {\r\n        wprintf(L\" %s%lc\",\r\n                hand[x].face,\r\n                hand[x].suit\r\n              );\r\n    }\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p>(<a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2019_09_21-Lesson-b.c\" rel=\"noopener noreferrer\" target=\"_blank\">Github<\/a>)<\/p>\n<p>The bubble sort uses nested <em>for<\/em> loops and variables <code>a<\/code> and <code>b<\/code> to order the structures in <code>hand[]<\/code> by card value. This ordering makes it easier for a future update of the code to find things such as pairs or a straight.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<p><code>Your hand: 3&#9827; 3&#9829; 4&#9827; J&#9829; J&#9830;<\/code><\/p>\n<p>This output lets you quickly see two pairs, threes and jacks.<\/p>\n<p>I could add a second sort to organize the hand by suit as well. Still, with only four suits and 13 different card values, sorting by value makes the most sense.<\/p>\n<p>The next step in this program&#8217;s evolution is to run tests on the hand based on the game played. I&#8217;m choosing Poker, so the tests will determine the various hand rankings:<\/p>\n<p>High card<br \/>\nPair<br \/>\nTwo pair<br \/>\nThree-of-a-kind<br \/>\nStraight<br \/>\nFlush<br \/>\nFull-house<br \/>\nFour-of-a-kind<br \/>\nStraight flush<\/p>\n<p>Dealing with these rankings in a logical manner is discussed in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3766\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The code can generate a random card from a deck and store a given number of them in a &#8220;hand.&#8221; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3753\">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-3753","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\/3753","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=3753"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3753\/revisions"}],"predecessor-version":[{"id":3783,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3753\/revisions\/3783"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}