# Output a Colorful Chessboard – Solution

This month’s Exercise may not seem that difficult, especially given the variety of grid challenges and dimensional array lessons I’ve posted on this blog. Obviously, you need a nested loop. The tough part, however, is generating the grid of alternating colors.

Figure 1. A better text mode chessboard created with ANSI color output.

Figure 1 shows the desired output, using my two defined colors listed in the Exercise post, cyan and white. The trick is to alternate the odd-even rows as well as the odd-even columns to generate this checkerboard pattern. For this type of decision, I use my old pal the modulus operator: `x%2`

The result of `x%2` is TRUE for odd values, FALSE for even. This expression is key to determining the chessboard’s color values. And because a grid pattern is desired, the operation must be performed for each individual square. Here is the code for my solution:

### 2022_11-Exercise.c

```#include <stdio.h>

#define SIZE 8
#define 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.

## 4 thoughts on “Output a Colorful Chessboard – Solution”

1. #include <stdio.h>

void chess_board(void)
{
int size = 8;
int squares = size * size;
const char *colours[] = {“\x1b[30;47m”, “\x1b[30;46m”, “\x1b[0m”};
int colidx = 0;

for(int square = 0; square < squares; square++)
{
printf(“%s “, colours[colidx]);

// flip 0 or 1
colidx = 1 – colidx;

// check if end of row, if so print \n and flip colidx
if((square + 1) % size == 0)
{
printf(“\n”);
colidx = 1 – colidx;
}
}

printf(“%s”, colours[2]);
putchar(‘\n’);
}

int main()
{
chess_board();

return(0);
}

2. Sorry, lost the indentation. I always mess this up don’t I?

AAAARRRRGGGGHHHH!!!!

3. Hmmm. Apparently the new update doesn’t allow <pre> tags. Sad.

I like your solution! More elegant than mine.

4. Then it’s probably necessary to replace tabs with 4 nbsps. I should have done that with the second space in the first printf as it doesn’t show up in html.