# Plotting Knight Moves

Computers and chess go together like Mr. Spock and Star Trek. 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’s the knight that provides the crazy addition, making the game of chess more than an advanced variation on checkers.

Unlike 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.

Figure 1. Potential moves for a knight on a chessboard.

The pattern of knight moves generates eight possible permutations. No, I’m not a math genius, I just wrote down the movement as left-right, up-down pairs:

-2/-1, -2/1, -1/-2, -1/2, 1/-2, 1/2, 2/-1, and 2/1

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.

When it comes to C programming, a specific data type isn’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.

### 2022_10_29-Lesson.c

```#include <stdio.h>

#define KM 8

int main()
{
struct position {
int row;
int col;
};
struct position pairs[KM] = {
{ -2,-1 }, { -2, 1 }, { -1, -2 }, { -1, 2 },
{ 1, -2 }, { 1, 2 }, { 2, -1 }, { 2, 1 }
};
int x;

for( x=0; x<KM; x++ )
{
printf("%d, %d\n",
pairs[x].row,
pairs[x].col
);
}

return(0);
}```

I keep the number of potential knight moves in defined constant `KM` for convenience, declared at Line 3.

Line 7 creates a structure, position, that holds integer members `row` and `col`. These values represent the knight’s horizontal and vertical movements, or they could represent a location on the chessboard grid.

A position structure variable `pairs` is declared at Line 11 as an array of eight (value `KM`) elements. The values are assigned, matching the knight’s possible movements.

Rather than code a complete game of chess, the code just spits out the `pairs[]` values in a for loop at line 17. Here’s the output:

```-2, -1 -2, 1 -1, -2 -1, 2 1, -2 1, 2 2, -1 2, 1```

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’s movement, you must locate valid locations relative to its current position. If you know the piece’s row and column values, churning through an array like `pairs[]` is one way to discover the piece’s next potential move.

Over the next few Lessons, I’ll expand upon the notion of moving a knight on a chessboard. Like any complex programming task, it’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 next week’s Lesson.