{"id":5587,"date":"2022-10-29T00:01:37","date_gmt":"2022-10-29T07:01:37","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5587"},"modified":"2022-11-05T08:57:51","modified_gmt":"2022-11-05T15:57:51","slug":"plotting-knight-moves","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5587","title":{"rendered":"Plotting Knight Moves"},"content":{"rendered":"<p>Computers and chess go together like Mr. Spock and <em>Star Trek<\/em>. The character of Spock is pure genius, a crazy addition to the mix that provided depth and wonder to the TV show and later movies. In chess, it&#8217;s the knight that provides the crazy addition, making the game of chess more than an advanced variation on checkers.<br \/>\n<!--more--><br \/>\nUnlike all the other pieces, the knight moves in a unique, L-shape pattern, bolstered by the capability to leapfrog other pieces. This L shape move is two squares by one square: left, right, up, down. Figure 1 illustrates the potential moves for the knight piece.<\/p>\n<div id=\"attachment_5588\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5588\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/10\/1029-figure1.png\" alt=\"knight on a chessboard\" width=\"500\" height=\"500\" class=\"size-full wp-image-5588\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/10\/1029-figure1.png 500w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/10\/1029-figure1-300x300.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/10\/1029-figure1-150x150.png 150w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><p id=\"caption-attachment-5588\" class=\"wp-caption-text\">Figure 1. Potential moves for a knight on a chessboard.<\/p><\/div>\n<p>The pattern of knight moves generates eight possible permutations. No, I&#8217;m not a math genius, I just wrote down the movement as left-right, up-down pairs:<\/p>\n<p>-2\/-1, -2\/1, -1\/-2, -1\/2, 1\/-2, 1\/2, 2\/-1, and 2\/1<\/p>\n<p>The knight can move two squares to the left (-2) and one square down (-1), two squares left (-2) and one square up (1), and so on through the list: left-right squares first, then up-down. Negative values move left and down; positive values move right and up.<\/p>\n<p>When it comes to C programming, a specific data type isn&#8217;t available to clump and list pairs such as these. The alternative is to create an array of structures. Each structure contains the left-right, up-down pairs. This array lists the possible knight moves.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_10_29-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_10_29-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define KM 8\r\n\r\nint main()\r\n{\r\n    struct position {\r\n        int row;\r\n        int col;\r\n    };\r\n    struct position pairs[KM] = {\r\n        { -2,-1 }, { -2, 1 }, { -1, -2 }, { -1, 2 },\r\n        { 1, -2 }, { 1, 2 }, { 2, -1 }, { 2, 1 }\r\n    };\r\n    int x;\r\n\r\n    for( x=0; x&lt;KM; x++ )\r\n    {\r\n        printf(\"%d, %d\\n\",\r\n                pairs[x].row,\r\n                pairs[x].col\r\n              );\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>I keep the number of potential knight moves in defined constant <code>KM<\/code> for convenience, declared at Line 3.<\/p>\n<p>Line 7 creates a structure, <em>position<\/em>, that holds integer members <code>row<\/code> and <code>col<\/code>. These values represent the knight&#8217;s horizontal and vertical movements, or they could represent a location on the chessboard grid.<\/p>\n<p>A <em>position<\/em> structure variable <code>pairs<\/code> is declared at Line 11 as an array of eight (value <code>KM<\/code>) elements. The values are assigned, matching the knight&#8217;s possible movements.<\/p>\n<p>Rather than code a complete game of chess, the code just spits out the <code>pairs[]<\/code> values in a <em>for<\/em> loop at line 17. Here&#8217;s the output:<\/p>\n<p><code>-2, -1<br \/>\n-2, 1<br \/>\n-1, -2<br \/>\n-1, 2<br \/>\n1, -2<br \/>\n1, 2<br \/>\n2, -1<br \/>\n2, 1<\/code><\/p>\n<p>Yes, this output is far from a game of chess that would even mildly challenge Mr. Spock. Still, you must start somewhere. My thought is that if you want to plot a knight&#8217;s movement, you must locate valid locations relative to its current position. If you know the piece&#8217;s row and column values, churning through an array like <code>pairs[]<\/code> is one way to discover the piece&#8217;s next potential move.<\/p>\n<p>Over the next few Lessons, I&#8217;ll expand upon the notion of moving a knight on a chessboard. Like any complex programming task, it&#8217;s best handled one step at a time. The next step is to translate a chessboard from the real world into the data a C program can manipulate. I continue this exploration with <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5603\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unlike all other chess pieces, the knight moves in a funky, crazy, and useful pattern. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5587\">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-5587","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\/5587","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=5587"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5587\/revisions"}],"predecessor-version":[{"id":5625,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5587\/revisions\/5625"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}