Why, There’s the Knight!


Continuing from last week’s Lesson, the task is now to combine the chessboard output from this month’s Exercise solution with the knight’s random location. This step is the next one leading to the result, which is to plot how a knight moves on a chessboard.

The code below generates the knight’s random position as a linear value, zero through SIZE*SIZE, which is 63 for a standard 64 square (8-by-8) chessboard. Next, this location is output using the chessboard solution from last month’s Exercise.

2022_11_12-Lesson.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 8
#define COLOR_WHITE "\x1b[30;47m"
#define COLOR_CYAN "\x1b[30;46m"
#define COLOR_OFF "\x1b[0m"

void chess_board(int k)
{
    char board[SIZE*SIZE];
    int x,row,col,pos;

    /* initialize the board and set the knight */
    for( x=0; x<SIZE*SIZE; x++ )
    {
        /* obtain knight's position and set */
        if( k == x )
            board[x] = 'K';
        else
            board[x] = ' ';
    }

    /* output the board */
    for( row=0; row<SIZE; row++ )
    {
        for( col=0; col<SIZE; col++ )
        {
            /* obtain the true offset */
            pos = row*SIZE + col;
            /* set the square character */
            if( row%2 )
            {
                if( col%2 )
                    printf("%s %c",COLOR_WHITE,board[pos]);
                else
                    printf("%s %c",COLOR_CYAN,board[pos]);
            }
            else
            {
                if( col%2 )
                    printf("%s %c",COLOR_CYAN,board[pos]);
                else
                    printf("%s %c",COLOR_WHITE,board[pos]);
            }
        }
        printf("%s",COLOR_OFF);
        putchar('\n');
    }
}

int main()
{
    int knight;

    /* seed the randomizer */
    srand( (unsigned)time(NULL) );

    /* pluck out a random square
       for the knight */
    knight = rand() % (SIZE*SIZE);

    /* output the board */
    chess_board(knight);

    return(0);
}

This code seems long, but it’s merely copy-and-paste from what’s already been done if you follow along with this blog.

In the main() function, the knight’s position is randomly generated as value from zero through (SIZE*SIZE). This value is passed to an updated version of the chess_board() function at Line 65.

A char array, board[], is declared at Line 12. This array is not a string; it’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’s position.

A for loop churns through each position on the board, starting at Line 16. When the passed value k is encountered (the knight’s random position), a K is assigned to the array. Otherwise, it’s filled with spaces

The chess_board() function’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’s Lesson. Variable pos is used in the modified print() statements to determine which character is output in which square: a space or a K for the knight.

Figure 1 shows the output. Run several times, the Knight hops over the board in a random pattern.

chessboard with knight

Figure 1. The text mode chessboard with the knight (K) at a random position.

Next week, I continue to build this code by adding output that shows the knight’s valid moves on the chessboard output.

Leave a Reply