{"id":2091,"date":"2016-09-01T00:01:08","date_gmt":"2016-09-01T07:01:08","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2091"},"modified":"2022-01-12T20:46:55","modified_gmt":"2022-01-13T04:46:55","slug":"take-your-turn","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2091","title":{"rendered":"Take Your Turn"},"content":{"rendered":"<p>The game of Tic-Tac-Toe, also called Noughts and Crosses, provides a fertile field to plow for any budding programmer. It involves a matrix (array), logic, decisions, and all sorts of fun. If you haven&#8217;t yet coded your own Tic-Tac-Toe game, I urge you to do so, but that&#8217;s not the topic for this month&#8217;s Exercise.<br \/>\n<!--more--><br \/>\nNo, this month your task is to <em>simulate<\/em> a game of Tic-Tac-Toe. As an example, here is code that does a crude game simulation:<\/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\nint main()\r\n{\r\n    char grid[9] = \"         \";\r\n    char token;\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n\r\n    <span class=\"comments\">\/* fill the grid *\/<\/span>\r\n    for(x=0;x&lt;9;x++)\r\n    {\r\n        token = rand() % 2 ? 'x' : 'o';\r\n        grid[x] = token;\r\n    }\r\n\r\n    <span class=\"comments\">\/* display the grid *\/<\/span>\r\n    for(x=0;x&lt;9;x+=3)\r\n    {\r\n        printf(\" %c  %c  %c\\n\",\r\n                grid[x],\r\n                grid[x+1],\r\n                grid[x+2]);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Line 7 initializes the <code>grid[]<\/code> array, which is the game&#8217;s 3-by-3 matrix. Spaces represent un-played squares in the game.<\/p>\n<p>The <em>for<\/em> loop at Line 15 fills the grid with a random <code>'x'<\/code> or <code>'o'<\/code> character. (The <code>token<\/code> variable isn&#8217;t really needed, but I find the ternary (<code>?:<\/code>) operator to be confusing, so I don&#8217;t like to over-code that statement.)<\/p>\n<p>Finally, the <em>for<\/em> loop at Line 22 displays the <code>grid[]<\/code> matrix in a tic-tac-toe style.<\/p>\n<p>Here&#8217;s sample output from the program:<\/p>\n<pre><code> x  x  o\r\n x  x  x\r\n o  x  o<\/code><\/pre>\n<p>Does anything seem wrong with that output? Perhaps you&#8217;re a big brother and that&#8217;s the type of tic-tac-toe game you would play with your little sister when she didn&#8217;t understand the rules. Effectively, player X has allowed player O to play only every other turn. That&#8217;s not proper.<\/p>\n<p>When the tokens are randomized, as is done in the sample code, the output can be anything. Theoretically, the board could show all <code>'x'<\/code> tokens. That&#8217;s unrealistic because, in real life, X and O take turns. X places a token, then O places a token.<\/p>\n<p>In an actual game simulation, token placement is made strategically, but that&#8217;s not your task for this month&#8217;s Exercise. Nope, your task is to re-code the &#8220;fill the grid&#8221; portion of the sample program so that the <code>'x'<\/code> and <code>'o'<\/code> tokens are placed one after the other; X plays first, then O, until the board is filled.<\/p>\n<p>You cannot place a token in a square that already has a token. And you don&#8217;t need to worry about playing strategically or stopping the simulation when one player wins: Just fill the grid so that the output is more-or-less evenly split between <code>'x'<\/code> and <code>'o'<\/code> tokens.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2110\">Click here<\/a> to read my solution, but please try the Exercise on your own before you take a peek at what I&#8217;ve done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A more logical approach to simulating a game of tic-tac-toe. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2091\">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-2091","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\/2091","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=2091"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2091\/revisions"}],"predecessor-version":[{"id":5165,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2091\/revisions\/5165"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}