Friday, May 10, 2024
HomeC ProgrammingPlotting Knight Strikes | C For Dummies Weblog

Plotting Knight Strikes | C For Dummies Weblog


Computer systems and chess go collectively like Mr. Spock and Star Trek. The character of Spock is pure genius, a loopy addition to the combination that offered depth and surprise to the TV present and later motion pictures. In chess, it’s the knight that gives the loopy addition, making the sport of chess greater than a sophisticated variation on checkers.

In contrast to all the opposite items, the knight strikes in a singular, L-shape sample, bolstered by the aptitude to leapfrog different items. This L form transfer is 2 squares by one sq.: left, proper, up, down. Determine 1 illustrates the potential strikes for the knight piece.

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

The sample of knight strikes generates eight doable permutations. No, I’m not a math genius, I simply wrote down the motion as left-right, up-down pairs:

-2/-1, -2/1, -1/-2, -1/2, 1/-2, 1/2, 2/-1, and a pair of/1

The knight can transfer two squares to the left (-2) and one sq. down (-1), two squares left (-2) and one sq. up (1), and so forth by the record: left-right squares first, then up-down. Detrimental values transfer left and down; optimistic values transfer proper and up.

In terms of C programming, a selected knowledge kind isn’t obtainable to clump and record pairs comparable to these. The choice is to create an array of constructions. Every construction incorporates the left-right, up-down pairs. This array lists the doable knight strikes.

2022_10_29-Lesson.c


#embody <stdio.h>

#outline KM 8

int essential()
{
    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;

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

    return(0);
}

I maintain the variety of potential knight strikes in outlined fixed KM for comfort, declared at Line 3.

Line 7 creates a construction, place, that holds integer members row and col. These values signify the knight’s horizontal and vertical actions, or they may signify a location on the chessboard grid.

A place construction variable pairs is said at Line 11 as an array of eight (worth KM) parts. The values are assigned, matching the knight’s doable actions.

Quite than code an entire recreation of chess, the code simply spits out the pairs[] values in a for loop at line 17. Right here’s the output:

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

Sure, this output is way from a recreation of chess that may even mildly problem Mr. Spock. Nonetheless, you should begin someplace. My thought is that if you wish to plot a knight’s motion, you should find legitimate places relative to its present place. If you recognize the piece’s row and column values, churning by an array like pairs[] is one strategy to uncover the piece’s subsequent potential transfer.

Over the subsequent few Classes, I’ll broaden upon the notion of shifting a knight on a chessboard. Like every advanced programming activity, it’s greatest dealt with one step at a time. The following step is to translate a chessboard from the actual world into the information a C program can manipulate. I proceed this exploration with subsequent week’s Lesson.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments