{"id":3731,"date":"2019-09-07T00:01:32","date_gmt":"2019-09-07T07:01:32","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3731"},"modified":"2019-09-14T08:53:22","modified_gmt":"2019-09-14T15:53:22","slug":"draw-five-cards","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3731","title":{"rendered":"Draw Five Cards (Poker I)"},"content":{"rendered":"<p>To code a card game, you must start with the deck: 52 cards divided into 4 suits each consisting of 10 number cards and three face cards. Seems easy.<br \/>\n<!--more--><br \/>\nI&#8217;ve <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=220\">written before<\/a> about simulating a deck of cards, but not really using them to play a game. Further, the deck must be randomized so you can&#8217;t draw the same card twice. These steps come first.<\/p>\n<p>The following code creates an array <code>cards[]<\/code> consisting of 52 <em>int<\/em> values. It&#8217;s initialized to all zeros, where zero indicates a card hasn&#8217;t been drawn. When a card is drawn, the corresponding array element is set to 1.<\/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\r\n#define CARDS 52\r\n\r\nint main()\r\n{\r\n    int deck[CARDS];\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand((unsigned)time(NULL));\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    return(0);\r\n}<\/pre>\n<p>The code seeds the randomizer at Line 13, which is used later. The <em>for<\/em> loop at Line 18 initializes array <code>deck[]<\/code> to all zeros. This code has no output.<\/p>\n<p>In the next modification, I add the <em>draw()<\/em> function. This function looks for elements in array <code>deck[]<\/code> that haven&#8217;t been drawn. Once found, it sets that element to 1 (meaning the card has been drawn) and returns the card&#8217;s element value.<\/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\r\n#define CARDS 52\r\n\r\nint draw(int d[]);\r\n\r\nint main()\r\n{\r\n    int deck[CARDS];\r\n    int x,c;\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand((unsigned)time(NULL));\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    c = draw(deck);\r\n    printf(\"You drew the %d\\n\",c);\r\n\r\n    return(0);\r\n}\r\n\r\nint draw(int d[])\r\n{\r\n    int r;\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    d[r] = 1;\r\n    \r\n    return(r);\r\n}<\/pre>\n<p>The <em>while<\/em> loop in the <em>draw()<\/em> function endlessly spins, looking for an element in array <code>d[]<\/code> (the passed deck of cards) that hasn&#8217;t yet been drawn. When one is found, the loop breaks. The given element is set to 1, then the element value is returned.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>You drew the 16<\/code><\/p>\n<p>Drawing &#8220;the 16&#8221; doesn&#8217;t many anything to someone familiar with the standard deck of playing cards. The <em>draw()<\/em> function works, but it needs more heft if the value returned is to be meaningful to a player as well as to the rest of the code when a card game is implemented.<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3746\">next week&#8217;s Lesson<\/a>, I upgrade the <em>draw()<\/em> function to report a card&#8217;s face value and suit.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Drawing cards is an exercise I&#8217;ve covered before. Now it&#8217;s time to do something with those cards. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3731\">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-3731","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\/3731","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=3731"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3731\/revisions"}],"predecessor-version":[{"id":3764,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3731\/revisions\/3764"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}