{"id":5612,"date":"2022-11-08T00:01:32","date_gmt":"2022-11-08T08:01:32","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5612"},"modified":"2022-11-05T10:58:27","modified_gmt":"2022-11-05T17:58:27","slug":"output-a-colorful-chessboard-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5612","title":{"rendered":"Output a Colorful Chessboard &#8211; Solution"},"content":{"rendered":"<p>This <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5595\">month&#8217;s Exercise<\/a> may not seem that difficult, especially given the variety of grid challenges and dimensional array lessons I&#8217;ve posted on this blog. Obviously, you need a nested loop. The tough part, however, is generating the grid of alternating colors.<br \/>\n<!--more--><br \/>\n<div id=\"attachment_5599\" style=\"width: 278px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5599\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/11\/1101-figure2-268x300.png\" alt=\"colorful ANSI chessboard\" width=\"268\" height=\"300\" class=\"size-medium wp-image-5599\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/11\/1101-figure2-268x300.png 268w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/11\/1101-figure2.png 400w\" sizes=\"auto, (max-width: 268px) 100vw, 268px\" \/><p id=\"caption-attachment-5599\" class=\"wp-caption-text\">Figure 1. A better text mode chessboard created with ANSI color output.<\/p><\/div><\/p>\n<p>Figure 1 shows the desired output, using my two defined colors listed in the Exercise post, cyan and white. The trick is to alternate the odd-even rows as well as the odd-even columns to generate this checkerboard pattern. For this type of decision, I use my old pal the modulus operator: <code>x%2<\/code><\/p>\n<p>The result of <code>x%2<\/code> is TRUE for odd values, FALSE for even. This expression is key to determining the chessboard&#8217;s color values. And because a grid pattern is desired, the operation must be performed for each individual square. Here is the code for my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_11-Exercise.c\" rel=\"noopener\" target=\"_blank\">2022_11-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define SIZE 8\r\n#define COLOR_WHITE \"\\x1b[30;47m\"\r\n#define COLOR_CYAN \"\\x1b[30;46m\"\r\n#define COLOR_OFF \"\\x1b[0m\"\r\n\r\nvoid chess_board(void)\r\n{\r\n    int row,col;\r\n\r\n    for( row=0; row&lt;SIZE; row++ )\r\n    {\r\n        for( col=0; col&lt;SIZE; col++ )\r\n        {\r\n            if( row%2 )\r\n            {\r\n                if( col%2 )\r\n                    printf(\"%s  \",COLOR_WHITE);\r\n                else\r\n                    printf(\"%s  \",COLOR_CYAN);\r\n            }\r\n            else\r\n            {\r\n                if( col%2 )\r\n                    printf(\"%s  \",COLOR_CYAN);\r\n                else\r\n                    printf(\"%s  \",COLOR_WHITE);\r\n            }\r\n        }\r\n        printf(\"%s\",COLOR_OFF);\r\n        putchar('\\n');\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n    chess_board();\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code uses four defined constants:<\/p>\n<p><code>SIZE<\/code> sets the chessboard&#8217;s dimensions, which you can adjust later to confirm that the grid pattern holds for any size chessboard.<\/p>\n<p><code>COLOR_WHITE<\/code> and <code>COLOR_CYAN<\/code> set the foreground and background colors for each square. The foreground color (code 30) is black text for both.<\/p>\n<p><code>COLOR_OFF<\/code> is required to disable text coloring at the end of each row. If you forget this ANSI code, the final square&#8217;s color bleeds over the rest of the line. (Re-read my <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5270\">ANSI Lesson<\/a>.)<\/p>\n<p>I made <em>chess_board()<\/em> a function as it&#8217;s used in my <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5587\">Knight Moves series<\/a> and called as such. In my solution, it appears in the <em>main()<\/em> function as the sole statement before <em>return<\/em>.<\/p>\n<p>Within the <em>chess_board()<\/em> function, <em>int<\/em> variables <code>row<\/code> and <code>col<\/code> map out the chessboard grid. Each <em>for<\/em> loop uses defined constant <code>SIZE<\/code> to set the game board&#8217;s width and depth.<\/p>\n<p>The inner <em>for<\/em> loop plots columns. Its <em>if<\/em> decisions help lay out the grid color pattern: The outer <em>if<\/em> decision maps out every other row. The inner <em>if<\/em> decision maps out columns.<\/p>\n<p>It appears like the two sets of decisions are parallel, but they&#8217;re not: The <code>COLOR_WHITE<\/code> and <code>COLOR_CYAN<\/code> literals are swapped between lines 19-21 and lines 26-28. The effect is the checkerboard pattern, ensuring that each row and column combination are sequentially different.<\/p>\n<p>At the end of the column loop, the <code>COLOR_OFF<\/code> code is output, restoring the terminal&#8217;s colors.<\/p>\n<p>The code&#8217;s output is shown in Figure 1.<\/p>\n<p>I hope your code met with success, and that the output matches mine. This type of chessboard is more useful than using hideous ASCII characters to map out a crude grid, as shown in the Exercise post. Lessons throughout this month employ this program&#8217;s output to plot the way a knight moves on a chessboard.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise may not seem that difficult, especially given the variety of grid challenges and dimensional array lessons I&#8217;ve posted on this blog. Obviously, you need a nested loop. The tough part, however, is generating the grid of alternating &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5612\">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":[5],"tags":[],"class_list":["post-5612","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\/5612","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=5612"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5612\/revisions"}],"predecessor-version":[{"id":5626,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5612\/revisions\/5626"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}