{"id":4095,"date":"2020-05-01T00:01:14","date_gmt":"2020-05-01T07:01:14","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4095"},"modified":"2020-05-08T08:23:13","modified_gmt":"2020-05-08T15:23:13","slug":"tic-tac-toe-evaluation","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4095","title":{"rendered":"Tic-Tac-Toe Evaluation"},"content":{"rendered":"<p>This month&#8217;s Exercise may seem like a repeat of the Exercise <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=154\">&#8220;We Have a Winnah!&#8221;<\/a> from July of 2013. It&#8217;s not.<br \/>\n<!--more--><br \/>\nThe July 2013 Exercise involved solving a tic-tac-toe like grid. Alas, the grid was filled with ones and zeros and only one grid pattern was presented. In a true game of tic-tac-toe, three characters occupy the grid: X, O, and blank. Further, multiple possibilities must be tested to assure a victory, which is why I&#8217;m presenting this, updated challenge.<\/p>\n<p>For this month&#8217;s Exercise, your task is to determine whether a completed and valid tic-tac-toe game grid holds a winning combination, which is three Xs or three Os across, down, or diagonal. To help meet this goal, I&#8217;ve provided a program skeleton available below as well as on <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_05_01-Exercise.c\" rel=\"noopener noreferrer\" target=\"_blank\">GitHub<\/a>.<\/p>\n<p>Something I learned while researching this project is that you can&#8217;t randomly pack an array and expect it to represent realistic game play. Therefore, each string in the <code>game[]<\/code> array represents a valid 3-by-3 game grid as a string of x, o, and space characters:<\/p>\n<p><code>char *game[6] = {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\"xxoooxxox\",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\"o x xox  \",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\"xx oxxooo\",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\"o xooox x\",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\" xo ox  o\",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\"x oxo xox\"<br \/>\n};<br \/>\n<\/code><\/p>\n<p>Each string lays out as rows and columns: <code>\"xxoooxxox\"<\/code> stands for the completed game shown in Figure 1.<\/p>\n<div id=\"attachment_4099\" style=\"width: 334px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4099\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/05\/0501-figure1.png\" alt=\"sample game\" width=\"324\" height=\"348\" class=\"size-full wp-image-4099\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/05\/0501-figure1.png 324w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/05\/0501-figure1-279x300.png 279w\" sizes=\"auto, (max-width: 324px) 100vw, 324px\" \/><p id=\"caption-attachment-4099\" class=\"wp-caption-text\">Figure 1. A sample game grid, no winner.<\/p><\/div>\n<p>The six strings in the array represent various possible solutions to a game of tic-tac-toe, along with some cat&#8217;s games (no winner). The code skeleton provided below contains the <code>game[]<\/code> array, the <em>output_grid()<\/em> function to display the array as a tic-tac-toe game, but the code is missing the <em>is_winner()<\/em> function that evaluates each string for a winner and returns the winning token character.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n<span class=\"comments\">\/* generate the tic-tac-toe grid *\/<\/span>\r\nvoid output_grid( char *g )\r\n{\r\n    int row;\r\n\r\n    for( row=0; row&lt;3; row++)\r\n    {\r\n        printf(\" %c %c %c\\n\",\r\n                *(g+(3*row)),\r\n                *(g+(3*row)+1),\r\n                *(g+(3*row)+2)\r\n              );\r\n    }\r\n}\r\n\r\n<span class=\"comments\">\/* determine whether the game was won *\/<\/span>\r\nchar is_winner( char *g )\r\n{\r\n    return(' ');\r\n}\r\n\r\nint main()\r\n{\r\n    char *game[6] = {\r\n        \"xxoooxxox\",\r\n        \"o x xox  \",\r\n        \"xx oxxooo\",\r\n        \"o xooox x\",\r\n        \" xo ox  o\",\r\n        \"x oxo xox\"\r\n    };\r\n    int x,r;\r\n\r\n    <span class=\"comments\">\/* examine all five game results *\/<\/span>\r\n    for( x=0; x&lt;6; x++ )\r\n    {\r\n        output_grid( game[x] );\r\n        printf(\"Game %d: \",x+1);\r\n        r = is_winner( game[x] );\r\n        switch(r)\r\n        {\r\n            case ' ':\r\n                puts(\"No winner\\n\");\r\n                break;\r\n            case 'x':\r\n                puts(\"X wins!\\n\");\r\n                break;\r\n            case 'o':\r\n                puts(\"O wins!\\n\");\r\n                break;\r\n            default:\r\n                puts(\"Unknown value\\n\");\r\n        }\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Your task for the Exercise is to code the <em>is_winner()<\/em> function. Ensure that it evaluates all possible solutions and returns the winning token character, &#8216;x&#8217; or &#8216;o&#8217;, or &#8216; &#8216; (space) for no winner. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4109\">Click here<\/a> to view my solution.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Determine whether a given matrix wins a game of tic-tac-toe. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4095\">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-4095","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\/4095","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=4095"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4095\/revisions"}],"predecessor-version":[{"id":4135,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4095\/revisions\/4135"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}