{"id":7084,"date":"2025-08-02T00:01:36","date_gmt":"2025-08-02T07:01:36","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7084"},"modified":"2025-07-26T10:36:29","modified_gmt":"2025-07-26T17:36:29","slug":"my-dear-lord-this-is-so-boring-dice-rolling-game","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7084","title":{"rendered":"My &#8220;Dear Lord This Is So Boring&#8221; Dice-Rolling Game"},"content":{"rendered":"<p>Rolling seven dice over and over is how I passed time &#8220;playing&#8221; D&#038;D. But I also played a game with the dice, one that I introduced in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7075\">last week&#8217;s Lesson<\/a>. That lesson&#8217;s code got things started. This Lesson finishes the project.<br \/>\n<!--more--><br \/>\nThe goal of the game is to roll the seven dice until each maxes out its value. For example, keep rolling a D20 until the value 20 (&#8220;nat 20!&#8221;) appears. Then the D20 is eliminated and the rest of the dice are rolled over and over until each one maxes out. Yes, it&#8217;s a stupid way to pass the time, but it was less distracting for the other players than had I started reading a book or humming loudly.<\/p>\n<p>The updates to last week&#8217;s code include a new array, <code>done[]<\/code>. This array tracks when each die has maxed is value. It&#8217;s a simple, binary array with zeros set for each element representing each die in the set.<\/p>\n<p>I&#8217;ve also added <em>int<\/em> variable <code>count<\/code> to monitor the total number of throws. Variable <code>tally<\/code>  determines when all the dice have maxed out.<\/p>\n<p>An endless <em>while<\/em> loop keeps &#8220;throwing&#8221; the dice until the game is over. For each roll, a <em>if<\/em> test determines whether a dice has maxed out. If true, the die&#8217;s corresponding element in the <code>done[]<\/code> array is set to one. A second check is made to monitor the <code>done[]<\/code> array to check when all the dice are maxed out. Otherwise, the <em>while<\/em> loop keeps spinning.<\/p>\n<p>Here is the full code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_08_02-Lesson.c\" rel=\"noopener\" target=\"_blank\">2025_08_02-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],done[die];        <span class=\"comments\">\/* six different die *\/<\/span>\r\n    int x,count,tally;\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n    <span class=\"comments\">\/* initialize the done[] array *\/<\/span>\r\n    for( x=0; x&lt;die; x++ )\r\n        done[x] = 0;\r\n\r\n    <span class=\"comments\">\/* show results *\/<\/span>\r\n    printf(\"%6s%5s%5s%5s%5s%5s%5s\\n\",\r\n            \" Count\",\"D4\",\"D6\",\"D8\",\"D12\",\"D20\",\"D100\"\r\n          );\r\n    <span class=\"comments\">\/* throw dem bones! *\/<\/span>\r\n    count = 1;\r\n    while(1)\r\n    {\r\n        printf(\"%5d:\",count);\r\n        for( x=0; x&lt;die; x++ )\r\n        {\r\n            d[x] = roll( sides[x] );\r\n            if( d[x]==sides[x] )\r\n                done[x] = 1;\r\n            printf(\"%5d\", done[x] ? sides[x] : d[x] );\r\n        }\r\n        putchar('\\n');\r\n\r\n        <span class=\"comments\">\/* test for completion *\/<\/span>\r\n        tally = 0;\r\n        for( x=0; x&lt;die; x++ )\r\n            tally += done[x];\r\n        if( tally==die )\r\n            break;        <span class=\"comments\">\/* end the loop *\/<\/span>\r\n        count++;\r\n    }\r\n\r\n    <span class=\"comments\">\/* summary *\/<\/span>\r\n    printf(\"It took %d throws for each die to max out\\n\",count);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>In the code, I modified the initial <em>printf()<\/em> statement adjusting output to account for a running throw total.<\/p>\n<p>The first <em>for<\/em> loop in the endless <em>while<\/em> loop rolls each die: <code>d[x] = roll( sides[x] );<\/code> The <em>if<\/em> test checks to see whether the dice has maxed out:<\/p>\n<p><code>if( d[x]==sides[x] )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;done[x] = 1;<\/code><\/p>\n<p>When a die is maxed out, its value is equal to the number of sides (represented in the <code>sides[]<\/code> array). At this point, its corresponding element in the <code>done[]<\/code> array is set to one.<\/p>\n<p>The final statement in the first <em>for<\/em> loop outputs either the die&#8217;s current value or, when the die&#8217;s corresponding <code>done[]<\/code> element is set, the max value is output:<\/p>\n<p><code>printf(\"%5d\", done[x] ? sides[x] : d[x] );<\/code><\/p>\n<p>Yes, this evaluation means that the program continues to roll each die, but the output shows only that a die is maxed.<\/p>\n<p>The second <em>for<\/em> loop tests to see whether all the dice have maxed out. The total of all elements in the <code>done[]<\/code> array is saved in variable <code>tally<\/code>. When tally is equal to the number of dice (variable <code>die<\/code>), the endless while <em>loop<\/em> breaks. The game is over and the total number of rolls is output.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>&nbsp;Count&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;1:&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;12&nbsp;&nbsp;&nbsp;&nbsp;8&nbsp;&nbsp;&nbsp;61<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;2:&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;&nbsp;7&nbsp;&nbsp;&nbsp;12&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;64<br \/>\n&nbsp;&n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&nbsp;&nbsp;&nbsp;8&nbsp;&nbsp;&nbsp;12&nbsp;&nbsp;&nbsp;20&nbsp;&nbsp;&nbsp;10<br \/>\n&nbsp;&nbsp;&nbsp;16:&nbsp;&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;&nbsp;8&nbsp;&nbsp;&nbsp;12&nbsp;&nbsp;&nbsp;20&nbsp;&nbsp;100<br \/>\nIt&nbsp;took&nbsp;16&nbsp;throws&nbsp;for&nbsp;each&nbsp;die&nbsp;to&nbsp;max&nbsp;out<\/code><\/p>\n<p>It was fortunate that the program took only 16 rolls to max out each die for the sample run. Other sample runs ran into the multiple hundreds of throws. But this is the nature of this silly game, basically to waste time while others were enjoying themselves playing a game I just don&#8217;t get.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How many times can you roll a set of D&#038;D dice until each maxes out? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7084\">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-7084","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\/7084","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=7084"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7084\/revisions"}],"predecessor-version":[{"id":7096,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7084\/revisions\/7096"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}