{"id":5595,"date":"2022-11-01T00:01:26","date_gmt":"2022-11-01T07:01:26","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5595"},"modified":"2022-11-12T10:40:04","modified_gmt":"2022-11-12T18:40:04","slug":"output-a-colorful-chessboard","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5595","title":{"rendered":"Output a Colorful Chessboard"},"content":{"rendered":"<h2>Difficulty: &#9733; &#9733; &#9733; &#9734;<\/h2>\n<p>Text mode need not be so dreary when it comes to generating a game of chess. You have several options, all of which stink to varying degrees.<br \/>\n<!--more--><br \/>\nThe worst is shown in Figure 1, which is something I concocted in a brutal attempt to create an ugly text mode chessboard. I&#8217;m sure some vintage mainframe in the 1970s forced users to endure playing chess on such a thing. Yikes.<\/p>\n<div id=\"attachment_5597\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5597\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/10\/1101-figure1.png\" alt=\"text mode chessboard\" width=\"550\" height=\"344\" class=\"size-full wp-image-5597\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/10\/1101-figure1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/10\/1101-figure1-300x188.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/10\/1101-figure1-480x300.png 480w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-5597\" class=\"wp-caption-text\">Figure 1. What could be a potential chess game on a terminal back in 1974.<\/p><\/div>\n<p>Better is to use color to fashion a light, friendly version of a text mode chessboard. I covered ASNI color text output <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5270\">in a post<\/a> from earlier this year. You could use such techniques to generate output such as shown in Figure 2.<\/p>\n<div id=\"attachment_5599\" style=\"width: 410px\" 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.png\" alt=\"colorful ANSI chessboard\" width=\"400\" height=\"448\" class=\"size-full wp-image-5599\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/11\/1101-figure2.png 400w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/11\/1101-figure2-268x300.png 268w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><p id=\"caption-attachment-5599\" class=\"wp-caption-text\">Figure 1. A better text mode chessboard created with ANSI color output.<\/p><\/div>\n<p>Your challenge for this month&#8217;s Exercise is to write code that generates the chessboard in Figure 2 as output. This task may seem trivial, but the difficultly level rises because the grid shows alternating colors. When you first write your code, you may see a striped pattern, not a grid. Extra decisions are required to output a grid.<\/p>\n<p>As mentioned earlier, refer to the ANSI color Lessons for details on color output in C. The two codes\/colors I use are defined in my solution as:<\/p>\n<p><code>#define COLOR_WHITE \"\\x1b[30;47m\"<br \/>\n#define COLOR_CYAN \"\\x1b[30;46m\"<\/code><\/p>\n<p>Both set black text in the foreground, but white and cyan colors for the background, as lovingly shown in Figure 2.<\/p>\n<p>Please try this exercise on your own before you check out <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5612\">my solution<\/a>. This code is used in later posts to generate chessboard output for my <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5587\">Knight Moves<\/a> series of lessons.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So many computer things involve chess. Why not create a pretty text-mode chessboard? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5595\">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-5595","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\/5595","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=5595"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5595\/revisions"}],"predecessor-version":[{"id":5637,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5595\/revisions\/5637"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}