Reputation: 21
I need help/hints printing a checkerboard in C.
I want to print a 4x4 checkerboard like this:
+----+
| |
| |
| |
+----+
Naturally thats only 1x1 but I don't know how to do a 4x4 one.
I know I have to use some sort of nested for loop to do this. I also have to store it in an array. All I have is this:
#include <stdio.h>
#include <string.h>
char board[4][4];
int main() {
for (int i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
board[i][j] =
}
}
I don't know how I would go about storing a 1x1 box in board[1][1] and then again in board[2][2]...you get the idea... Can you guys please help me how to do this?
Thanks in advance!
Upvotes: 0
Views: 2231
Reputation: 477
A board goes like this:
+---+---+
| | |
+---+---+
| | |
etc.
I would first print several separator elements:
while(i < n){
printf("+---");
i++;
}
printf("+\n");
Than several cells:
while(i < n){
printf("| %c ", currcell);
i++;
}
printf("|\n")
where char curcell
would be your board[i],[j]
- a whitespace by default.
Sorry if it doesn't look clear enough, i'm very new to programming.
Upvotes: 1
Reputation: 2428
Consider that for every row, you will print +
and then a ----+
for each column (or |
and spaces):
+----+----+----+ ... ----+
| | | | ... |
.
.
.
| | | | ... |
+----+----+----+ ... ----+
| | | | ... |
And so on.
Also consider whether you need to store the board state or the board drawing in your arrays. The board state would be easier to analyze (and use less memory). When you need to "pretty print" the board, you can generate the drawing from the the board state array.
Upvotes: 2