{"id":7075,"date":"2025-07-26T00:01:06","date_gmt":"2025-07-26T07:01:06","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7075"},"modified":"2025-08-02T08:16:55","modified_gmt":"2025-08-02T15:16:55","slug":"roll-dem-bones","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7075","title":{"rendered":"Roll &#8216;dem Bones!"},"content":{"rendered":"<p>You would think that I&#8217;d be deeply into Dungeons and Dragons, but no. I can&#8217;t stand the game. I find it tedious and predictable, boring. But I did enjoy rolling all those dice. Who knew that a 20-side die is a thing &mdash; and that rolling a &#8220;nat 20&#8221; is a big deal?<br \/>\n<!--more--><br \/>\nOne of the ways I passed time while everyone else enjoyed the game was to repeatedly roll my dice. In the typical D&#038;D set, you have seven die:<\/p>\n<ul>\n<li>One four-sided die.<\/li>\n<li>One six-sided die, which is the typical die normal humans are familiar with.<\/li>\n<li>One eight-sided die.<\/li>\n<li>One twelve-sided die.<\/li>\n<li>One twenty-sided die.<\/li>\n<li>Two ten-sided die, which are rolled together to obtain values from 0 through 99.<\/li>\n<\/ul>\n<p>You can find a 100-sided die in the gaming stores, but most sets are sold with two ten-sided die, one with numbers 0 through 9 and the other with values 00 through 90. As in C and other programming languages, I admire that the 100-sided die values are from zero to 99. The other dies all start with the number 1.<\/p>\n<p>Anyhoo . . .<\/p>\n<p>As I sat waiting my turn, one of the games I&#8217;d play was to repeatedly roll all my dice. When a die revealed it&#8217;s maximum value, I&#8217;d set it aside and continue to role until another die reached its maximum value. As I rolled, I counted the number of times it took before finally every die had rolled its top value. Such fun.<\/p>\n<p>The following code represents the first step in creating my &#8220;Dear Lord This Is So Boring&#8221; dice-rolling game I played while others around me enjoyed D&#038;D. It uses a <em>roll()<\/em> function that I&#8217;ve written about before, but this time I&#8217;ve modified the function to handle dice with more than six sides.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_07_26-Lesson.c\" rel=\"noopener\" target=\"_blank\">2025_07_26-Lesson.c<\/a><\/h3>\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<span class=\"comments\">\/* rolling an 's' sided die *\/<\/span>\r\nint roll(int s)\r\n{\r\n    return( rand() % s + 1 );\r\n}\r\n\r\nint main()\r\n{\r\n    const int die = 6;\r\n    <span class=\"comments\">\/* d4, d6, d8, d12, d20, d100 *\/<\/span>\r\n    const int sides[] = {\r\n        4, 6, 8, 12, 20, 100\r\n    };\r\n    int d[die];        <span class=\"comments\">\/* six different die *\/<\/span>\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\">\/* throw dem bones! *\/<\/span>\r\n    for( x=0; x&lt;die; x++ )\r\n        d[x] = roll( sides[x] );\r\n\r\n    <span class=\"comments\">\/* show results *\/<\/span>\r\n    printf(\"%5s%5s%5s%5s%5s%5s\\n\",\r\n            \"D4\",\"D6\",\"D8\",\"D12\",\"D20\",\"D100\"\r\n          );\r\n    for( x=0; x&lt;die; x++ )\r\n        printf(\"%5d\",d[x]);\r\n    putchar('\\n');\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>roll()<\/em> function is passed the number of sides, which helps generate the proper return value based on the die chosen. For example, the function returns 1 through 20 for rolling a 20-sided die. The D100 dice returns 1 through 100, not zero through 99, which is how the game is played.<\/p>\n<p>In the <em>main()<\/em> function, constant array <code>sides[]<\/code> holds the number of sides for each die. This value is used in the <em>for<\/em> loop to help roll each die. The resulting roll values are saved in array <code>d[]<\/code> and then output in a second <em>for<\/em> loop.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>&nbsp;&nbsp;&nbsp;D4&nbsp;&nbsp;&nbsp;D6&nbsp;&nbsp;&nbsp;D8&nbsp;&nbsp;D12&nbsp;&nbsp;D20&nbsp;D100<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;10&nbsp;&nbsp;&nbsp;14&nbsp;&nbsp;&nbsp;17<\/code><\/p>\n<p>In the game I played, the above output means that the six-sided die is eliminated; 6 is its maximum value. I&#8217;d continue rolling the rest of the dice until each maxes out. But this code proves only that the process works and rolls the dice. For <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7084\">next week&#8217;s Lesson<\/a>, I complete the program, rolling all the dice until each maxes out.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I didn&#8217;t like playing D&#038;D, but I enjoyed rolling all those dice. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7075\">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-7075","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\/7075","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=7075"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7075\/revisions"}],"predecessor-version":[{"id":7107,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7075\/revisions\/7107"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}