Saturday, April 27, 2024
HomeC ProgrammingWhy, There’s the Knight! | C For Dummies Weblog

Why, There’s the Knight! | C For Dummies Weblog



Persevering with from final week’s Lesson, the duty is now to mix the chessboard output from this month’s Train resolution with the knight’s random location. This step is the following one resulting in the end result, which is to plot how a knight strikes on a chessboard.

The code beneath generates the knight’s random place as a linear worth, zero by way of SIZE*SIZE, which is 63 for the standard 64 sq. (8-by-8) chessboard. Subsequent, this location is output utilizing the chessboard resolution from final month’s Train.

2022_11_12-Lesson.c


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

#outline SIZE 8
#outline 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;

    
    for( x=0; x<SIZE*SIZE; x++ )
    {
        
        if( ok == x )
            board[x] = 'Okay';
        else
            board[x] = ' ';
    }

    
    for( row=0; row<SIZE; row++ )
    {
        for( col=0; col<SIZE; col++ )
        {
            
            pos = row*SIZE + col;
            
            if( rowpercent2 )
            {
                if( colpercent2 )
                    printf("%s %c",COLOR_WHITE,board[pos]);
                else
                    printf("%s %c",COLOR_CYAN,board[pos]);
            }
            else
            {
                if( colpercent2 )
                    printf("%s %c",COLOR_CYAN,board[pos]);
                else
                    printf("%s %c",COLOR_WHITE,board[pos]);
            }
        }
        printf("%s",COLOR_OFF);
        putchar('n');
    }
}

int fundamental()
{
    int knight;

    
    srand( (unsigned)time(NULL) );

    
    knight = rand() % (SIZE*SIZE);

    
    chess_board(knight);

    return(0);
}

This code appears lengthy, however it’s merely copy-and-paste from what’s already been performed for those who comply with together with this weblog.

Within the fundamental() operate, the knight’s place is randomly generated as worth from zero by way of (SIZE*SIZE). This worth is handed to an up to date model of the chess_board() operate at Line 65.

A char array, board[], is said at Line 12. This array will not be a string; it’s not terminated with a null character. As an alternative, it holds the characters that seem at every place on the board. For this step within the course of, the characters are both an area or the letter Okay to indicate the knight’s place.

A for loop churns by way of every place on the board, beginning at Line 16. When the handed worth ok is encountered (the knight’s random place), a Okay is assigned to the array. In any other case, it’s full of areas

The chess_board() operate’s nested loops use the identical selections to generate the grid sample. At Line 31, a translation is constituted of the row/column values right into a linear worth, which is the reverse of the expressions lined in final week’s Lesson. Variable pos is used within the modified print() statements to find out which character is output during which sq.: an area or a Okay for the knight.

Determine 1 reveals the output. Run a number of occasions, the Knight hops over the board in a random sample.

chessboard with knight

Determine 1. The textual content mode chessboard with the knight (Okay) at a random place.

Subsequent week, I proceed to construct this code by including output that reveals the knight’s legitimate strikes on the chessboard output.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments