{"id":5628,"date":"2022-11-19T00:01:35","date_gmt":"2022-11-19T08:01:35","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5628"},"modified":"2022-11-26T09:02:44","modified_gmt":"2022-11-26T17:02:44","slug":"plotting-the-knights-next-move","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5628","title":{"rendered":"Plotting the Knight&#8217;s Next Move"},"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 \/>\nThe next step in hopping a knight around a chessboard is determining which squares from its current position represent valid moves. Code must be combined from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5618\">last week&#8217;s lesson<\/a> (setting the knight on the chessboard at a random spot) with the movement code presented a few lessons back.<br \/>\n<!--more--><br \/>\nFigure 1 shows the results desired, with the knight&#8217;s random position marked by the letter K and valid potential moves shown as asterisks.<\/p>\n<div id=\"attachment_5629\" style=\"width: 466px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5629\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/11\/1119-figure1.png\" alt=\"knight on a chessboard showing moves\" width=\"456\" height=\"328\" class=\"size-full wp-image-5629\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/11\/1119-figure1.png 456w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/11\/1119-figure1-300x216.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/11\/1119-figure1-417x300.png 417w\" sizes=\"auto, (max-width: 456px) 100vw, 456px\" \/><p id=\"caption-attachment-5629\" class=\"wp-caption-text\">Figure 1. Potential moves for a knight on a chessboard.<\/p><\/div>\n<p>To plot the knight&#8217;s moves I created the <em>moveto()<\/em> function. It accepts the knight&#8217;s current position (<code>k<\/code>) and an array of potential moves (<code>m[]<\/code>) as arguments. The knight&#8217;s position is randomly set in the <em>main()<\/em> function. The array of potential moves is also declared there, but its values are initialized in the <em>moveto()<\/em> function:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* plot the knight moves\r\n   k = position\r\n   m[] = array to hold potential moves *\/<\/span>\r\nvoid moveto(int k, int m[])\r\n{\r\n    struct position {\r\n        int row;\r\n        int col;\r\n    };\r\n    <span class=\"comments\">\/* offsets to move the knight *\/<\/span>\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,krow,kcol,r,c;\r\n\r\n    <span class=\"comments\">\/* calculate knight's row and column\r\n       to make later calculations more\r\n       readable *\/<\/span>\r\n    kcol = k % SIZE;\r\n    krow = (k - kcol)\/SIZE;\r\n    <span class=\"comments\">\/* plot the potential knight moves\r\n       up to KM (8) of them *\/<\/span>\r\n    for( x=0; x&lt;KM; x++ )\r\n    {\r\n        <span class=\"comments\">\/* find new square position *\/<\/span>\r\n        r = krow + pairs[x].row;\r\n        c = kcol + pairs[x].col;\r\n        <span class=\"comments\">\/* check for valid moves *\/<\/span>\r\n        if( (r&lt;0 || r&gt;=SIZE) || (c&lt;0 || c&gt;=SIZE) )\r\n        {\r\n            <span class=\"comments\">\/* square is out of bounds *\/<\/span>\r\n            m[x] = -1;\r\n        }\r\n        else\r\n        {\r\n            <span class=\"comments\">\/* plot position *\/<\/span>\r\n            m[x] = r * SIZE + c;\r\n        }\r\n    }\r\n}<\/pre>\n<p>Literal <code>KM<\/code> equals the number of potential knight moves, set to eight. The moves are defined in the <em>position<\/em> structure <code>pairs<\/code> as row and column offsets. This topic was discussed in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5587\">an earlier lesson<\/a>.<\/p>\n<p>The knight&#8217;s row (<code>krow<\/code>) and column (<code>kcol<\/code>) position is obtained from these expressions:<\/p>\n<p><code>kcol = k % SIZE;<br \/>\nkrow = (k - kcol)\/SIZE;<\/code><\/p>\n<p>This translation is necessary as the <code>pairs[]<\/code> array uses row and column offsets.<\/p>\n<p>A <em>for<\/em> loop processes all potential moves, zero through <code>KM<\/code>. For each <code>pairs[]<\/code> value, a row (<code>r<\/code>) and column (<code>c<\/code>) is obtained:<\/p>\n<p><code>r = krow + pairs[x].row;<br \/>\nc = kcol + pairs[x].col;<\/code><\/p>\n<p>An <em>if<\/em> test confirms whether the row\/column value is in range:<\/p>\n<p><code>if( (r&lt;0 || r&gt;=SIZE) || (c&lt;0 || c&gt;=SIZE) )<\/code><\/p>\n<p>If the row (<code>r<\/code>) or column (<code>c<\/code>) is out of range, -1 is set in the <code>m[]<\/code> array. Otherwise, the position is plotted and set in the array as a linear value:<\/p>\n<p><code>m[x] = r * SIZE + c;<\/code><\/p>\n<p>The array argument <code>m[]<\/code> need not be returned from the <em>moveto()<\/em> function. It&#8217;s one of those weird array\/pointer things that works in C.<\/p>\n<p>The <em>main()<\/em> function is updated to add the <em>moveto()<\/em> function call. Also the <em>chess_board()<\/em> function is updated to accept the <code>moves[]<\/code> array as a second argument. A <em>for<\/em> loop is added to the function to process the <code>moves[]<\/code> array, setting asterisks in those squares where the knight can move and ignoring the -1 values in the array.<\/p>\n<p>The full code is <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_11_19-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">available on GitHub<\/a>. The output doesn&#8217;t yet match Figure 1 as the number of valid moves isn&#8217;t counted. Remember: One step at a time!<\/p>\n<p>Once I got the first version of the code to work, I added another function, <em>movecount()<\/em>. This function processes the <code>moves[]<\/code> array, determining how many moves are valid. It tallies those elements not equal to -1.<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* count the valid moves\r\n   m[] = array of move counts\r\n   -1 value for no move *\/<\/span>\r\nint movecount(int m[])\r\n{\r\n    int x,count;\r\n\r\n    count = 0;\r\n    <span class=\"comments\">\/* scan the array and tally values\r\n       zero or greater (non -1) *\/<\/span>\r\n    for( x=0; x&lt;KM; x++ )\r\n    {\r\n        if( m[x] &gt;= 0 )\r\n            count++;\r\n    }\r\n\r\n    return(count);\r\n}<\/pre>\n<p>The updated code is <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_11_19-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">available on GitHub<\/a>.<\/p>\n<p>The <em>movecount()<\/em> function isn&#8217;t just for reporting the output, as shown in Figure 1. No, it comes in handy as the code progresses. The goal is to determine which of the valid squares the knight can move to based on the number of next moves possible. This topic is tackled in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5638\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If the knight on a chessboard moved in a traditional way (horizontally or vertically), plotting its moves would be a cinch. Alas, that&#8217;s just not the case. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5628\">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-5628","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\/5628","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=5628"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5628\/revisions"}],"predecessor-version":[{"id":5658,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5628\/revisions\/5658"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}