{"id":5618,"date":"2022-11-12T00:01:29","date_gmt":"2022-11-12T08:01:29","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5618"},"modified":"2022-11-19T08:59:01","modified_gmt":"2022-11-19T16:59:01","slug":"why-theres-the-knight","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5618","title":{"rendered":"Why, There&#8217;s the Knight!"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/11\/knight_moves.png\" alt=\"\" width=\"150\" height=\"150\" class=\"alignnone size-full wp-image-5605\" \/><br \/>\nContinuing from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5603\">last week&#8217;s Lesson<\/a>, the task is now to combine the chessboard output from this month&#8217;s Exercise solution with the knight&#8217;s random location. This step is the next one leading to the result, which is to plot how a knight moves on a chessboard.<br \/>\n<!--more--><br \/>\nThe code below generates the knight&#8217;s random position as a linear value, zero through <code>SIZE*SIZE<\/code>, which is 63 for a standard 64 square (8-by-8) chessboard. Next, this location is output using the chessboard <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5612\">solution from last month&#8217;s Exercise<\/a>.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_11_12-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_11_12-Lesson.c<\/a><\/h3>\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\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(int k)\r\n{\r\n    char board[SIZE*SIZE];\r\n    int x,row,col,pos;\r\n\r\n    <span class=\"comments\">\/* initialize the board and set the knight *\/<\/span>\r\n    for( x=0; x&lt;SIZE*SIZE; x++ )\r\n    {\r\n        <span class=\"comments\">\/* obtain knight's position and set *\/<\/span>\r\n        if( k == x )\r\n            board[x] = 'K';\r\n        else\r\n            board[x] = ' ';\r\n    }\r\n\r\n    <span class=\"comments\">\/* output the board *\/<\/span>\r\n    for( row=0; row&lt;SIZE; row++ )\r\n    {\r\n        for( col=0; col&lt;SIZE; col++ )\r\n        {\r\n            <span class=\"comments\">\/* obtain the true offset *\/<\/span>\r\n            pos = row*SIZE + col;\r\n            <span class=\"comments\">\/* set the square character *\/<\/span>\r\n            if( row%2 )\r\n            {\r\n                if( col%2 )\r\n                    printf(\"%s %c\",COLOR_WHITE,board[pos]);\r\n                else\r\n                    printf(\"%s %c\",COLOR_CYAN,board[pos]);\r\n            }\r\n            else\r\n            {\r\n                if( col%2 )\r\n                    printf(\"%s %c\",COLOR_CYAN,board[pos]);\r\n                else\r\n                    printf(\"%s %c\",COLOR_WHITE,board[pos]);\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    int knight;\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n\r\n    <span class=\"comments\">\/* pluck out a random square\r\n       for the knight *\/<\/span>\r\n    knight = rand() % (SIZE*SIZE);\r\n\r\n    <span class=\"comments\">\/* output the board *\/<\/span>\r\n    chess_board(knight);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code seems long, but it&#8217;s merely copy-and-paste from what&#8217;s already been done if you follow along with this blog.<\/p>\n<p>In the <em>main()<\/em> function, the knight&#8217;s position is randomly generated as value from zero through (<code>SIZE*SIZE<\/code>). This value is passed to an updated version of the <em>chess_board()<\/em> function at Line 65.<\/p>\n<p>A <em>char<\/em> array, <code>board[]<\/code>, is declared at Line 12. This array is not a string; it&#8217;s not terminated with a null character. Instead, it holds the characters that appear at each position on the board. For this step in the process, the characters are either a space or the letter K to denote the knight&#8217;s position.<\/p>\n<p>A <em>for<\/em> loop churns through each position on the board, starting at Line 16. When the passed value <code>k<\/code> is encountered (the knight&#8217;s random position), a K is assigned to the array. Otherwise, it&#8217;s filled with spaces<\/p>\n<p>The <em>chess_board()<\/em> function&#8217;s nested loops use the same decisions to generate the grid pattern. At Line 31, a translation is made from the row\/column values into a linear value, which is the reverse of the expressions covered in last week&#8217;s Lesson. Variable <code>pos<\/code> is used in the modified <em>print()<\/em> statements to determine which character is output in which square: a space or a K for the knight.<\/p>\n<p>Figure 1 shows the output. Run several times, the Knight hops over the board in a random pattern.<\/p>\n<div id=\"attachment_5620\" style=\"width: 360px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5620\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/11\/1112-figure1.png\" alt=\"chessboard with knight\" width=\"350\" height=\"394\" class=\"size-full wp-image-5620\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/11\/1112-figure1.png 350w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/11\/1112-figure1-266x300.png 266w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><p id=\"caption-attachment-5620\" class=\"wp-caption-text\">Figure 1. The text mode chessboard with the knight (K) at a random position.<\/p><\/div>\n<p><a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5628\">Next week<\/a>, I continue to build this code by adding output that shows the knight&#8217;s valid moves on the chessboard output.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Combining code from previous Lessons and this month&#8217;s Exercise, it&#8217;s time to visually plot the knight&#8217;s position on a text mode chessboard. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5618\">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":[2],"tags":[],"class_list":["post-5618","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5618","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=5618"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5618\/revisions"}],"predecessor-version":[{"id":5645,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5618\/revisions\/5645"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}