Monday, November 28, 2022
HomeC ProgrammingOutput a Colourful Chessboard – Resolution

Output a Colourful Chessboard – Resolution


This month’s Train might not appear that troublesome, particularly given the number of grid challenges and dimensional array classes I’ve posted on this weblog. Clearly, you want a nested loop. The powerful half, nevertheless, is producing the grid of alternating colours.

Determine 1. A greater textual content mode chessboard created with ANSI shade output.

Determine 1 reveals the specified output, utilizing my two outlined colours listed within the Train submit, cyan and white. The trick is to alternate the odd-even rows in addition to the odd-even columns to generate this checkerboard sample. For this kind of choice, I take advantage of my previous pal the modulus operator: xpercent2

The results of xpercent2 is TRUE for odd values, FALSE for even. This expression is vital to figuring out the chessboard’s shade values. And since a grid sample is desired, the operation should be carried out for every particular person sq.. Right here is the code for my resolution:

2022_11-Train.c


#embrace <stdio.h>

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

void chess_board(void)
{
    int row,col;

    for( row=0; row<SIZE; row++ )
    {
        for( col=0; col<SIZE; col++ )
        {
            if( row%2 )
            {
                if( col%2 )
                    printf("%s  ",COLOR_WHITE);
                else
                    printf("%s  ",COLOR_CYAN);
            }
            else
            {
                if( col%2 )
                    printf("%s  ",COLOR_CYAN);
                else
                    printf("%s  ",COLOR_WHITE);
            }
        }
        printf("%s",COLOR_OFF);
        putchar('n');
    }
}

int main()
{
    chess_board();

    return(0);
}

This code uses four defined constants:

SIZE sets the chessboard’s dimensions, which you can adjust later to confirm that the grid pattern holds for any size chessboard.

COLOR_WHITE and COLOR_CYAN set the foreground and background colors for each square. The foreground color (code 30) is black text for both.

COLOR_OFF is required to disable text coloring at the end of each row. If you forget this ANSI code, the final square’s color bleeds over the rest of the line. (Re-read my ANSI Lesson.)

I made chess_board() a function as it’s used in my Knight Moves series and called as such. In my solution, it appears in the main() function as the sole statement before return.

Within the chess_board() function, int variables row and col map out the chessboard grid. Each for loop uses defined constant SIZE to set the game board’s width and depth.

The inner for loop plots columns. Its if decisions help lay out the grid color pattern: The outer if decision maps out every other row. The inner if decision maps out columns.

It appears like the two sets of decisions are parallel, but they’re not: The COLOR_WHITE and COLOR_CYAN literals are swapped between lines 19-21 and lines 26-28. The effect is the checkerboard pattern, ensuring that each row and column combination are sequentially different.

At the end of the column loop, the COLOR_OFF code is output, restoring the terminal’s colors.

The code’s output is shown in Figure 1.

I hope your code met with success, and that the output matches mine. This type of chessboard is more useful than using hideous ASCII characters to map out a crude grid, as shown in the Exercise post. Lessons throughout this month employ this program’s output to plot the way a knight moves on a chessboard.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments