Board of Chess

Thanks to Mr. Spock (and God bless you, Mr. Nimoy!), many people think of Chess when they think of computer programming. Unlike mankind’s favorite Vulcan, coding your own computer chess game would be a tremendous undertaking. Even so, you can start with the basics and write a program that outputs a chessboard.

More specifically, your challenge for the month of April is to write code that generates a square game board, like a checkerboard, but one of variable size.

For review, a chessboard features 64 squares arranged in 8 rows by 8 columns. And you could write code that simply barfs out that board by cramming a host of printf() statements into the main() function. Or you could be clever and use a loop. Or you could obey my directions and craft code that outputs a variable size chessboard.

Your code must read the command line arguments, of which only one is valid: A value from 1 to n that indicates the number of rows/columns on the chessboard. When a value isn’t specified (or the value is out of range or just weird), output an 8-by-8 board.

Figure 1 illustrates the output from my solution, although your code’s output doesn’t need to be identical. In fact, I’ve merely included a link to Figure 1 so that if you don’t want to peek at my code’s output, you can dive right into the Exercise.

Figure 1.

Click here to view my solution. Please consider trying this exercise on your own before you do so.

Leave a Reply