Question

I am writing a program that allows a user to play the game connect four against the computer. I am having trouble printing out the correct dimensions for the board. I tried using nested for loops, but the output is a little off. Here is a part of my code:

#include <stdio.h>

#define BOARD_SIZE_VERT 6
#define BOARD_SIZE_HORIZ 7

void display_board(int board[] [BOARD_SIZE_VERT]);

int main ()
{
  int board[BOARD_SIZE_HORIZ][BOARD_SIZE_VERT] = {{0}};
  display_board(board);
  return 0;

}


  void display_board(int board[] [BOARD_SIZE_VERT])
{
  int i,j;

  for (i=0; i<BOARD_SIZE_HORIZ; i++) {
    printf ("+---+---+---+---+---+---+---+");
    printf ("\n");
    for (j=0; j<BOARD_SIZE_VERT; j++)
     printf ("|   ");
     printf("\n");
  }
}

This is my output:

+---+---+---+---+---+---+---+
|   |   |   |   |   |   
+---+---+---+---+---+---+---+
|   |   |   |   |   |   
+---+---+---+---+---+---+---+
|   |   |   |   |   |   
+---+---+---+---+---+---+---+
|   |   |   |   |   |   
+---+---+---+---+---+---+---+
|   |   |   |   |   |   
+---+---+---+---+---+---+---+
|   |   |   |   |   |   
+---+---+---+---+---+---+---+
|   |   |   |   |   |   

And this is what I want it to look like:

+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
Was it helpful?

Solution

void display_board(int board[] [BOARD_SIZE_VERT]){
    int i,j;

    for (i=0; i<BOARD_SIZE_VERT; i++) {
        printf ("+");
        for (j=0; j<BOARD_SIZE_HORIZ; j++)
            printf("---+");
        printf ("\n");
        printf ("|");
        for (j=0; j<BOARD_SIZE_HORIZ; j++)
            printf("   |");
        printf ("\n");
    }

    printf ("+");
    for (j=0; j<BOARD_SIZE_HORIZ; j++)
        printf("---+");
    printf ("\n");
}

OTHER TIPS

Two main problems: your loops are inverted, and you need some extra characters printed to to get the extra lines on the bottom and right. Try this:

void display_board(int board[] [BOARD_SIZE_HORIZ])
{
  int row,col;

  for (row=0; row<BOARD_SIZE_VERT; row++) {
    printf ("+---+---+---+---+---+---+---+");
    printf ("\n");
    for (col=0; col<BOARD_SIZE_HORIZ; col++) {
      printf ("|   ");
    }
    printf("|\n");
  }
  printf ("+---+---+---+---+---+---+---+");
}

Here are the relevant changes:

  • I swapped the usage of the VERT/HORIZ constants in the loops. The outer loop controls the height of the board since you are printing one line at a time. The inner loop controls the number of columns in the board
  • Renamed i and j to the row and col so the code is more self-documenting
  • Added a pipe to the printf("\n"), because you need one more vertical separator line than the actual number of slots for each line
  • Added an extra printf("+---.... at the end, because you need one more horizontal separator than the actual number of rows

Also, I changed the array definition in the function prototype so that the first index is the row and the second is the column. If you choose to follow that, you would need to change other usages of the array in your code. For example:

int board[BOARD_SIZE_VERT][BOARD_SIZE_HORIZ] = {{0}};

Your code work very well. But you have eight "| " to display and You loops only BOARD_SIZE_VERT which worth 6. So you should increase it to 8 or add to it. Try this:

void display_board(int board[] [BOARD_SIZE_VERT]){
    int i,j;
    for (i=0; i<BOARD_SIZE_HORIZ - 1; i++) {
        printf ("+---+---+---+---+---+---+---+");
        printf ("\n");
        for (j=0; j<BOARD_SIZE_VERT + 2; j++)
             printf ("|   ");
         printf("\n");
    }
    printf ("+---+---+---+---+---+---+---+\n");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top