{"id":3469,"date":"2019-02-01T00:01:59","date_gmt":"2019-02-01T08:01:59","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3469"},"modified":"2019-02-09T08:38:14","modified_gmt":"2019-02-09T16:38:14","slug":"the-wandering-king","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3469","title":{"rendered":"The Wandering King"},"content":{"rendered":"<p>The chessboard is empty. In the center is the king. He can move only one square at a time, but in any direction &mdash; or he can choose not to move at all. How many turns would it take him, moving randomly, to exit the chessboard?<br \/>\n<!--more--><br \/>\nA chessboard is an 8-by-8 grid. For this month&#8217;s Exercise, create a 9-by-9 grid so that you can place the king directly in the center. Figure 1 illustrates how this setup might look by using primitive text graphics output from a C program.<\/p>\n<div id=\"attachment_3470\" style=\"width: 402px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3470\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2019\/01\/0201_wanderingking-figure1.png\" alt=\"\" width=\"392\" height=\"334\" class=\"size-full wp-image-3470\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2019\/01\/0201_wanderingking-figure1.png 392w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2019\/01\/0201_wanderingking-figure1-300x256.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2019\/01\/0201_wanderingking-figure1-352x300.png 352w\" sizes=\"auto, (max-width: 392px) 100vw, 392px\" \/><p id=\"caption-attachment-3470\" class=\"wp-caption-text\">Figure 1. The king is poised to wander.<\/p><\/div>\n<p>Each turn, the king moves to one of 9 squares: north, northeast, east, southeast, south, southwest, west, northwest, or the king can stay where he is, as illustrated in Figure 2, which uses better graphics than Figure 1.<\/p>\n<div id=\"attachment_3472\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3472\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2019\/02\/0201_kingmoves-figure2.png\" alt=\"\" width=\"400\" height=\"408\" class=\"size-full wp-image-3472\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2019\/02\/0201_kingmoves-figure2.png 400w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2019\/02\/0201_kingmoves-figure2-294x300.png 294w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><p id=\"caption-attachment-3472\" class=\"wp-caption-text\">Figure 2. The king can move in any of these 8 directions or not at all.<\/p><\/div>\n<p>Moving randomly, at some point, the king wanders off the chessboard (game grid) and ends the simulation.<\/p>\n<p>Your task for this month&#8217;s Exercise is to code the Wandering King puzzle:<\/p>\n<p>Start with the king in the center of a 9-by-9 game grid. Each turn, move the king in one of the nine directions illustrated in Figure 2. Keep in mind that not moving is also an option.<\/p>\n<p>When the king slips from the game grid, report the number of turns it took him to do so. In my trials, it was surprising that sometimes the king wandered right off but other times it took more than 100 turns for him to find his way out.<\/p>\n<p>You can optionally display the game grid each turn.<\/p>\n<p>Please try this Exercise on your own before you look at <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3482\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How long does it take for the king to amble off a chessboard? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3469\">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":[3],"tags":[],"class_list":["post-3469","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3469","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=3469"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3469\/revisions"}],"predecessor-version":[{"id":3506,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3469\/revisions\/3506"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}