{"id":3746,"date":"2019-09-14T00:01:05","date_gmt":"2019-09-14T07:01:05","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3746"},"modified":"2019-09-21T08:11:36","modified_gmt":"2019-09-21T15:11:36","slug":"the-playing-card-structure","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3746","title":{"rendered":"The Playing Card Structure (Poker II)"},"content":{"rendered":"<p>Drawing cards from a deck is an activity requiring more effort than flagging an element in an array, which was demonstrated in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3731\">last week&#8217;s Lesson<\/a>. Such code works, but it&#8217;s best to set details about the card when it&#8217;s drawn, noting its face value and suit, for example. Such complex information is best placed into a structure.<br \/>\n<!--more--><br \/>\nHere&#8217;s the <em>playing_card<\/em> structure I devised:<\/p>\n<p><code>struct playing_card {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;wchar_t suit;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;char face[3];<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;int value;<br \/>\n};<\/code><\/p>\n<p>The <em>suit<\/em> member is of the <em>wchar_t<\/em> type, a <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2568\">wide character<\/a>. This member holds the symbol for Spades (&#9824;), Clubs (&#9827;), Hearts (&#9829;), and Diamonds (&#9830;). It not only helps the code recognize the suit, it stores the unicode character value for output.<\/p>\n<p>The <em>face<\/em> member is a string that holds the card&#8217;s face value. This includes the string representation of values 2 through 10, but also the characters &#8220;A&#8221; for Ace, &#8220;J&#8221; for Jack, &#8220;Q&#8221; for Queen, and &#8220;K&#8221; for King.<\/p>\n<p>The <em>value<\/em> member holds the card&#8217;s numeric value, 1 through 13.<\/p>\n<p>The updated <em>draw()<\/em> function from last week&#8217;s code now selects a random card from the deck and then fills and returns a <em>playing_card<\/em> structure. The structure&#8217;s members are based on the random card drawn, assigning the suit, face value string, and integer value to the card. Here&#8217;s the overhauled code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n#include &lt;locale.h&gt;\r\n#include &lt;wchar.h&gt;\r\n\r\n#define CARDS 52\r\n\r\nstruct playing_card {\r\n    wchar_t suit;\r\n    char face[3];\r\n    int value;\r\n};\r\n\r\nstruct playing_card draw(int d[]);\r\n\r\nint main()\r\n{\r\n    int deck[CARDS];\r\n    struct playing_card hand;\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 card *\/<\/span>\r\n    hand = draw(deck);\r\n    wprintf(L\"You drew the %s of %lcs\\n\",\r\n            hand.face,\r\n            hand.suit\r\n          );\r\n\r\n    return(0);\r\n}\r\n\r\nstruct playing_card draw(int d[])\r\n{\r\n    wchar_t suits[] = { 0x2660, 0x2663, 0x2665, 0x2666 };\r\n    struct playing_card c;\r\n    int r;\r\n\r\n    <span class=\"comments\">\/* search for an available card *\/<\/span>\r\n    while(1)\r\n    {\r\n        r = rand() % CARDS;\r\n        if( d[r] == 0 )\r\n            break;\r\n    }\r\n\r\n    <span class=\"comments\">\/* mark the card as drawn *\/<\/span>\r\n    d[r] = 1;\r\n\r\n    <span class=\"comments\">\/* assign suit *\/<\/span>\r\n    c.suit = suits[r\/13];\r\n\r\n    <span class=\"comments\">\/* assign card value *\/<\/span>\r\n    c.value = r % 13 + 1;\r\n    \r\n    <span class=\"comments\">\/* assign face wide string *\/<\/span>\r\n    switch(c.value)\r\n    {\r\n        case 1:\r\n            sprintf(c.face,\"A\");\r\n            break;\r\n        case 11:\r\n            sprintf(c.face,\"J\");\r\n            break;\r\n        case 12:\r\n            sprintf(c.face,\"Q\");\r\n            break;\r\n        case 13:\r\n            sprintf(c.face,\"K\");\r\n            break;\r\n        default:\r\n            sprintf(c.face,\"%d\",c.value);\r\n    }\r\n    \r\n    return(c);\r\n}<\/pre>\n<p>This code mixes both standard character and wide character output, required to generate the card suit symbols.<\/p>\n<p>Here are a few sample runs:<\/p>\n<p><code>You drew the K of &#9830;s<br \/>\nYou drew the J of &#9824;s<br \/>\nYou drew the 9 of &#9827;s<\/code><\/p>\n<p>You may not see the wide characters in some terminal windows. I recommend changing the font or trying a different terminal window program if the special characters don&#8217;t appear.<\/p>\n<p>The next step to creating a card game is to modify the code to draw and store a given number of cards. After that, the cards must be evaluated, depending on what type of game you want to play. This process continues in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3753\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To best work with a playing card simulation, a structure is created to hold information about each card drawn. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3746\">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-3746","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\/3746","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=3746"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3746\/revisions"}],"predecessor-version":[{"id":3775,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3746\/revisions\/3775"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}