Friday, April 19, 2024
HomeC ProgrammingPlotting the Knight’s Subsequent Transfer

Plotting the Knight’s Subsequent Transfer



The following step in hopping a knight round a chessboard is figuring out which squares from its present place characterize legitimate strikes. Code have to be mixed from final week’s lesson (setting the knight on the chessboard at a random spot) with the motion code offered just a few classes again.

Determine 1 reveals the outcomes desired, with the knight’s random place marked by the letter Ok and legitimate potential strikes proven as asterisks.

knight on a chessboard showing moves

Determine 1. Potential strikes for a knight on a chessboard.

To plot the knight’s strikes I created the moveto() operate. It accepts the knight’s present place (okay) and an array of potential strikes (m[]) as arguments. The knight’s place is randomly set within the principal() operate. The array of potential strikes can be declared there, however its values are initialized within the moveto() operate:



void moveto(int okay, int m[])
{
    struct place {
        int row;
        int col;
    };
    
    struct place pairs[KM] = {
        { -2,-1 }, { -2, 1 }, { -1, -2 }, { -1, 2 },
        { 1, -2 }, { 1, 2 }, { 2, -1 }, { 2, 1 }
    };
    int x,krow,kcol,r,c;

    
    kcol = okay % SIZE;
    krow = (okay - kcol)/SIZE;
    
    for( x=0; x<KM; x++ )
    {
        
        r = krow + pairs[x].row;
        c = kcol + pairs[x].col;
        
        if( (r<0 || r>=SIZE) || (c<0 || c>=SIZE) )
        {
            
            m[x] = -1;
        }
        else
        {
            
            m[x] = r * SIZE + c;
        }
    }
}

Literal KM equals the variety of potential knight strikes, set to eight. The strikes are outlined within the place construction pairs as row and column offsets. This matter was mentioned in an earlier lesson.

The knight’s row (krow) and column (kcol) place is obtained from these expressions:

kcol = okay % SIZE;
krow = (okay - kcol)/SIZE;

This translation is critical because the pairs[] array makes use of row and column offsets.

A for loop processes all potential strikes, zero via KM. For every pairs[] worth, a row (r) and column (c) is obtained:

r = krow + pairs[x].row;
c = kcol + pairs[x].col;

An if check confirms whether or not the row/column worth is in vary:

if( (r<0 || r>=SIZE) || (c<0 || c>=SIZE) )

If the row (r) or column (c) is out of vary, -1 is about within the m[] array. In any other case, the place is plotted and set within the array as a linear worth:

m[x] = r * SIZE + c;

The array argument m[] needn’t be returned from the moveto() operate. It’s a type of bizarre array/pointer issues that works in C.

The principal() operate is up to date so as to add the moveto() operate name. Additionally the chess_board() operate is up to date to just accept the strikes[] array as a second argument. A for loop is added to the operate to course of the strikes[] array, setting asterisks in these squares the place the knight can transfer and ignoring the -1 values within the array.

The complete code is out there on GitHub. The output doesn’t but match Determine 1 because the variety of legitimate strikes isn’t counted. Bear in mind: One step at a time!

As soon as I received the primary model of the code to work, I added one other operate, movecount(). This operate processes the strikes[] array, figuring out what number of strikes are legitimate. It tallies these components not equal to -1.



int movecount(int m[])
{
    int x,rely;

    rely = 0;
    
    for( x=0; x<KM; x++ )
    {
        if( m[x] >= 0 )
            rely++;
    }

    return(rely);
}

The up to date code is out there on GitHub.

The movecount() operate isn’t only for reporting the output, as proven in Determine 1. No, it turns out to be useful because the code progresses. The purpose is to find out which of the legitimate squares the knight can transfer to based mostly on the variety of subsequent strikes attainable. This matter is tackled in subsequent week’s Lesson.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments