{"id":2110,"date":"2016-09-08T00:01:23","date_gmt":"2016-09-08T07:01:23","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2110"},"modified":"2016-09-04T08:51:57","modified_gmt":"2016-09-04T15:51:57","slug":"take-your-turn-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2110","title":{"rendered":"Take Your Turn &#8211; Solution"},"content":{"rendered":"<p>This month&#8217;s <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2091\">Exercise<\/a> is to simulate turns in a game of tic-tac-toe (naughts and crosses). So the solution requires that you randomly work through available squares in the game matrix, alternating placement of an <code>'x'<\/code> or <code>'o'<\/code> token.<br \/>\n<!--more--><br \/>\nIn the <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/08\/09exercise.c\">sample code<\/a>, a <em>for<\/em> loop places random <code>'x'<\/code> or <code>'o'<\/code> characters in the <code>grid[]<\/code> array. For my solution, however, I chose to use a <em>while<\/em> countdown loop instead.<\/p>\n<p>Within the countdown loop, a randomizer attempts to place a token for <code>'x'<\/code> or <code>'o'<\/code>, depending on the value of the countdown variable (odd or even). When a token is placed, the countdown variable is incremented, otherwise the loop keeps spinning until an empty grid space is found.<\/p>\n<p>Here is the countdown loop portion of my solution that places tokens in the game matrix:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* fill the grid *\/<\/span>\r\n    x = 0;\r\n    while(x<9)\r\n    {\r\n        if(x%2) <span class=\"comments\">\/* set 'x' token *\/<\/span>\r\n        {\r\n            r = rand() % 9;\r\n            if(grid[r] == ' ')\r\n            {\r\n                grid[r] = 'x';\r\n                x++;\r\n            }\r\n        }\r\n        else    <span class=\"comments\">\/* set 'o' token *\/<\/span>\r\n        {\r\n            r = rand() % 9;\r\n            if(grid[r] == ' ')\r\n            {\r\n                grid[r] = 'o';\r\n                x++;\r\n            }\r\n        }\r\n    }<\/pre>\n<p>Variable <code>x<\/code> is a counting variable. It references all 9 squares in the matrix, but not a specific array element. It just ensures that the <em>while<\/em> loop spins 9 times. (And it counts up, not down, but I still refer to the structure as a <em>countdown loop<\/em>.)<\/p>\n<p>The <em>if<\/em> statement at Line 17 (in the final code) is triggered for even counts of variable <code>x<\/code>. That&#8217;s when the <code>'x'<\/code> token is placed. When <code>x<\/code> is odd, the <em>else<\/em> statement at Line 26 runs and the code attempts to place an <code>'o'<\/code> token. In the big picture, this process is how the simulation takes turns placing each token.<\/p>\n<p>Just because the <em>if<\/em> or <em>else<\/em> statements are run, however, doesn&#8217;t guarantee that a token is placed.<\/p>\n<p>Within each set of statements (<em>if<\/em> at Line 17 or <em>else<\/em> at Line 26), variable <code>r<\/code> contains a random value, 0 through 8, representing an element in array <code>grid[]<\/code> &mdash; a location in the game matrix. An <em>if<\/em> statement checks to see whether that element is available. If so, the token is placed (Lines 22 and 31) and variable <code>x<\/code> is incremented. If not, the <em>while<\/em> loop continues to spin; variable <code>x<\/code> isn&#8217;t incremented.<\/p>\n<p>The <em>while<\/em> loop may repeat several dozen times, especially as the value of variable <code>x<\/code> increases. It take longer to locate available elements as the <code>grid[]<\/code> array fills. Granted, this technique may not be very efficient, but it works to evenly-distribute the tokens.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/09\/09exercise-solution.c\">Click here<\/a> to view my full solution.<\/p>\n<p>Here are a few sample runs:<\/p>\n<pre><code> x  o  x\r\n o  o  o\r\n x  x  o\r\n\r\n o  o  x\r\n o  x  x\r\n x  o  o\r\n\r\n x  o  o\r\n x  x  o\r\n x  o  o<\/code><\/pre>\n<p>As you can see, the tokens are distributed evenly in each simulation.<\/p>\n<p>My solution is only one approach to solving the problem. If your solution generates output that shows tokens distributed in a proper manner, then great!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise is to simulate turns in a game of tic-tac-toe (naughts and crosses). So the solution requires that you randomly work through available squares in the game matrix, alternating placement of an &#8216;x&#8217; or &#8216;o&#8217; token.<\/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-2110","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\/2110","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=2110"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2110\/revisions"}],"predecessor-version":[{"id":2130,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2110\/revisions\/2130"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}