{"id":5603,"date":"2022-11-05T00:01:28","date_gmt":"2022-11-05T07:01:28","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5603"},"modified":"2022-11-12T10:39:25","modified_gmt":"2022-11-12T18:39:25","slug":"from-linear-array-to-row-column","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5603","title":{"rendered":"From Linear Array to Row\/Column"},"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=\"alignleft size-full wp-image-5605\" \/> A chessboard is a gird, traditionally eight-by-eight squares for 64 positions. This game matrix can be implemented in code in a variety of ways, with a two-dimensional array being the most obvious. But this option isn&#8217;t what I chose when programming how a knight moves (from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5587\">last week&#8217;s Lesson<\/a>).<br \/>\n<!--more--><br \/>\nTrust me, I did a lot of trial and error.<\/p>\n<p>At first I decided to eschew using a specific data structure for the chessboard. Instead, I would store the knight&#8217;s position in a structure, similar to the <code>position<\/code> structure from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5587\">last week&#8217;s Lesson<\/a>:<\/p>\n<p><code>struct position {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;int row;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;int col;<br \/>\n};<\/code><\/p>\n<p>This structure would be filled with random values and bandied about various functions that manipulate the knight and plot where it can move. I wrote a lot of code around this approach &mdash; but it was clumsy and I grew annoyed and unhappy.<\/p>\n<p>A two-dimensional array seems obvious, but it also adds some complexity I didn&#8217;t want to deal with. After all, a 2D array in C is really a one-dimensional array all gussied up. Even so, I abandoned both the structure and 2D array approaches and instead use a single integer value to represent the knight&#8217;s position based on the chessboard&#8217;s size.<\/p>\n<p>To allow for a chessboard of varying size (one thing math geeks like to play with), I use the defined constant <code>SIZE<\/code> to set the matrix. Yet, I still need to know the row and column where a piece sits to calculate its potential moves. Therefore some translation is required.<\/p>\n<p>The following code plows through all 64 (or <code>SIZE*SIZE<\/code>) possible locations on a chessboard, using two statements that report each square&#8217;s row and column position for the given grid size.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_11_05-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2022_11_05-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define SIZE 8\r\n\r\nint main()\r\n{\r\n    int square,row,column;\r\n\r\n    for( square=0; square&lt;SIZE*SIZE; square++ )\r\n    {\r\n        <span class=\"comments\">\/* calculate row and column *\/<\/span>\r\n        column = square % SIZE;\r\n        row = (square-column)\/SIZE;\r\n        printf(\"%d:%d \",row,column);\r\n        if( column==SIZE-1 )\r\n            putchar('\\n');\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>for<\/em> loop processes each of the &#8220;squares&#8221; in the matrix. based on values from 0 through the matrix size (<code>SIZE*SIZE<\/code>). Line 12 calculates the <em>column<\/em> first, which is the <em>square<\/em>&#8216;s value modulo <code>SIZE<\/code>. This value helps Line 13 find the <em>row<\/em> number: subtracting the <em>column<\/em> offset from the <em>square<\/em> value, then dividing by constant <code>SIZE<\/code>.<\/p>\n<p>Line 15 adds a newline after each row of grid pairs is output:<\/p>\n<p><code>0:0 0:1 0:2 0:3 0:4 0:5 0:6 0:7<br \/>\n1:0 1:1 1:2 1:3 1:4 1:5 1:6 1:7<br \/>\n2:0 2:1 2:2 2:3 2:4 2:5 2:6 2:7<br \/>\n3:0 3:1 3:2 3:3 3:4 3:5 3:6 3:7<br \/>\n4:0 4:1 4:2 4:3 4:4 4:5 4:6 4:7<br \/>\n5:0 5:1 5:2 5:3 5:4 5:5 5:6 5:7<br \/>\n6:0 6:1 6:2 6:3 6:4 6:5 6:6 6:7<br \/>\n7:0 7:1 7:2 7:3 7:4 7:5 7:6 7:7<\/code><\/p>\n<p>This output represents the chessboard matrix as a series of row:column pairs, successfully translating sequential values zero through 63. The code confirms that I&#8217;m able to extract row\/column pairs based on linear values.<\/p>\n<p>My next task in building a program to plot how a knight moves is to set a virtual knight on the chessboard at a random position and report the position.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_11_05-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2022_11_05-Lesson-b.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\r\nint main()\r\n{\r\n    int knight,row,column;\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n\r\n    <span class=\"comments\">\/* pick a random square *\/<\/span>\r\n    knight = rand() % (SIZE*SIZE);\r\n    printf(\"The knight is at square %d\\n\",knight);\r\n\r\n    <span class=\"comments\">\/* calculate row and column *\/<\/span>\r\n    column = knight % SIZE;\r\n    row = (knight-column)\/SIZE;\r\n    printf(\"That's row %d, column %d\\n\",row,column);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Position for variable <em>knight<\/em> is assigned at Line 15, a random value from zero to 63 inclusive. This value is reported (output) at Line 16. Then Lines 19 and 20 extract the <em>row<\/em> and <em>column<\/em> values. Here&#8217;s a sample run:<\/p>\n<p><code>The knight is at square 47<br \/>\nThat's row 5, column 7<\/code><\/p>\n<p>This code represents the typical testing I do when I attempt a complex project like calculating where a knight can move on a chessboard.<\/p>\n<p>The next step is to combine the solution from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5595\">this month&#8217;s Exercise<\/a>, so that the knight&#8217;s position is output in the color-coded chessboard on the text screen. This task is covered in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5618\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Several choices are available for storing chessboard data. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5603\">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-5603","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\/5603","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=5603"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5603\/revisions"}],"predecessor-version":[{"id":5636,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5603\/revisions\/5636"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}