{"id":3482,"date":"2019-02-08T00:01:25","date_gmt":"2019-02-08T08:01:25","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3482"},"modified":"2019-02-02T09:30:56","modified_gmt":"2019-02-02T17:30:56","slug":"the-wandering-king-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3482","title":{"rendered":"The Wandering King &#8211; Solution"},"content":{"rendered":"<p>This <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3469\">month&#8217;s Exercise<\/a> was inspired by a program I recall from years ago called <em>The Drunk and the Lamppost<\/em>. It&#8217;s not the classic joke, but an examination of random movements and probability.<br \/>\n<!--more--><br \/>\nIn the simulation, the lamppost was in the center of a grid. The drunk would wander randomly about the grid, similar to the problem presented in this month&#8217;s Exercise. The observation made as that eventually the drunk would return to the lamppost. This program is a proof of some higher math concept that&#8217;s beyond me, but the puzzle stuck in my brain.<\/p>\n<p>Unlike the <em>Drunk and the Lamppost<\/em> example, the Wandering King tries to get off the grid. (When the drunk hit the edge of the grid, he changed directions.)<\/p>\n<p>My original solution used a two-dimensional array to track the king&#8217;s movements. After toying with it a while, I discovered a better solution is to keep the king&#8217;s location in a structure and dispense with the game grid array. Here is the structure is used:<\/p>\n<p><code>struct location {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;int x;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;int y;<br \/>\n};<\/code><\/p>\n<p>With the structure mapping the king&#8217;s location, the array was no longer necessary. I could check the <code>x<\/code> and <code>y<\/code> members of the structure to update and confirm the king&#8217;s position. The grid can still be output without an array; all that&#8217;s needed is the king&#8217;s coordinates. Here&#8217;s the <em>show_grid()<\/em> function I wrote as part of my solution:<\/p>\n<pre class=\"screen\">\r\nvoid show_grid(int t, struct location m)\r\n{\r\n    int x,y;\r\n\r\n    if( t == 0 )\r\n        puts(\"Start!\");\r\n    else\r\n        printf(\"Turn %d:\\n\",t);\r\n\r\n    for(x=0;x&lt;SIZE;x++)\r\n    {\r\n        for(y=0;y&lt;SIZE;y++)\r\n        {\r\n            if( x==m.x &amp;&amp; y==m.y )\r\n                printf(\" X \");\r\n            else\r\n                printf(\" . \");\r\n        }\r\n        putchar('\\n');\r\n    }\r\n}<\/pre>\n<p>Argument <code>t<\/code> is the turn number and <code>m<\/code> is the king&#8217;s structure with <code>x<\/code> and <code>y<\/code> members. The <code>SIZE<\/code> constant expression is the grid&#8217;s dimensions, which is set to 9 in my solution. In the function, the <code>x<\/code> and <code>y<\/code> (row and column) variables are compared with their counterparts in the king&#8217;s <code>m<\/code> structure. When the king&#8217;s position matches, an X is output instead of a dot in the grid. No array is necessary.<\/p>\n<p>A second function, <em>direction()<\/em>, returns the values -1, 0, or 1. This value is used in a <em>while<\/em> loop inside the <em>main()<\/em> function to change the king&#8217;s location. An <em>if<\/em> statement tests the updated location to see whether the king has wandered off the grid, with values of <code>x<\/code> or <code>y<\/code> less than 0 or greater than the <code>SIZE<\/code> constant.<\/p>\n<p><a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2019\/02\/02exercise.c\">Click here<\/a> to view my full solution.<\/p>\n<p>I hope that you enjoyed the Exercise and devised a clever solution. Remember, my solution isn&#8217;t the only or best one. Any programming puzzle can be solved in a number of interesting ways.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise was inspired by a program I recall from years ago called The Drunk and the Lamppost. It&#8217;s not the classic joke, but an examination of random movements and probability.<\/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-3482","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\/3482","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=3482"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3482\/revisions"}],"predecessor-version":[{"id":3512,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3482\/revisions\/3512"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}