{"id":1351,"date":"2015-05-08T00:01:13","date_gmt":"2015-05-08T07:01:13","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1351"},"modified":"2021-05-04T11:33:55","modified_gmt":"2021-05-04T18:33:55","slug":"lets-go-bowling-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1351","title":{"rendered":"Let&#8217;s Go Bowling &#8211; Solution"},"content":{"rendered":"<p>You may find my solution for this month&#8217;s <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1336\">Exercise<\/a> to be more complex than necessary, but there&#8217;s a method to my madness.<br \/>\n<!--more--><br \/>\nFirst, I chose to use a structure, <code>frame<\/code>, to hold the information about the single frame bowled:<\/p>\n<pre><code>struct frame {\r\n    char b1,b2;\r\n    int ball1,ball2,score;\r\n};<\/code><\/pre>\n<p>This choice came about as I was coding and realized that each frame has distinct elements that I wanted to keep: The first ball&#8217;s value, the second ball&#8217;s value, and then the characters used for each ball&#8217;s &#8220;value&#8221; (always a single digit), plus the total score. <\/p>\n<p>Second, I chose to split up the solution into separate functions:<\/p>\n<p><em>ball()<\/em> retrieves a random number within a specific range. The range was added so that the second ball rolled would not exceed the number of pins standing.<\/p>\n<p><em>get_frame()<\/em> fills a <code>frame<\/code> structure. This function &#8220;rolls&#8221; the balls, then sets each <code>frame<\/code> structure member accordingly. It tests for a strike, spare, calculates the proper score and assigns characters for each ball rolled.<\/p>\n<p>Finally, the <em>main()<\/em> function is blessedly brief, thanks to the other two functions. It initializes the randomizer, fetches a <code>frame<\/code> structure, then displays the results.<\/p>\n<p>Here is my solution:<\/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\nstruct frame {\r\n    char b1,b2;\r\n    int ball1,ball2,score;\r\n};\r\n\r\nint ball(int pins)\r\n{\r\n    return( rand() % (pins + 1));\r\n}\r\n\r\nvoid get_frame(struct frame *f)\r\n{\r\n    f->ball1 = ball(10);\r\n    if(f-&gt;ball1==10)\r\n    {\r\n        f-&gt;ball2 = 0;\r\n        f-&gt;b1 = 'X';\r\n        f-&gt;b2 = ' ';\r\n        f-&gt;score = 10;\r\n        return;\r\n    }\r\n\r\n    f-&gt;ball2 = ball(10 - f-&gt;ball1);\r\n    f-&gt;score = f-&gt;ball1 + f-&gt;ball2;\r\n    f-&gt;b1 = f-&gt;ball1 ? '0' + f-&gt;ball1 : '-';\r\n    f-&gt;b2 = f-&gt;ball2 ? '0' + f-&gt;ball2 : '-';\r\n    if( f-&gt;score==10 )\r\n        f-&gt;b2 = '\/';\r\n}\r\n\r\nint main()\r\n{\r\n    struct frame game;\r\n\r\n    srand((unsigned)time(NULL));\r\n\r\n    get_frame(&game);\r\n\r\n    printf(\"| %c|%c|\\n\",game.b1,game.b2);\r\n    printf(\"| %2d |\\n\",game.score);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Remember that my code presents only one possible solution. My observation is that no one correct solution exists, and if your code displays consistent results and doesn&#8217;t blow up the computer, you&#8217;re doing well.<\/p>\n<p>As a heads-up, I used this modular design for another, nefarious reason: <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1377\">Next month&#8217;s Exercise<\/a> is to code entire bowling game simulation, 10 frames. If you think of this month&#8217;s problem in that perspective, then you can see why I made the choices I did.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You may find my solution for this month&#8217;s Exercise to be more complex than necessary, but there&#8217;s a method to my madness.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1351","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1351","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=1351"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1351\/revisions"}],"predecessor-version":[{"id":4760,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1351\/revisions\/4760"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}