Home > Back-end >  Chess board in c
Chess board in c

Time:11-15

I was wondering how can i make a c program that only prints the black positions on a chess board for example:
- = (empty space);

-| A8 - C8 - E8 - G8 -
-| - B7 - D7 - F7 - H7
-| A6 - C6 - E6 - G6 -
-| - B5 - D5 - F5 - H5
-| A4 - C4 - E4 - G4 -
-| - B3 - D3 - F3 - H3
-| A2 - C2 - E2 - G2 -
-| - B1 - D1 - F1 - H1

#include <stdio.h>

int main()
{
    int n = 8;

    int i,j;
    char a[100][100] = {
        "A8"," ","C8"," ","E8"," ","G8",
        " ","B7"," ","D7"," ","F8"," ","H7",
        "A6"," ","C6"," ","E6"," ","G6",
        " ","B5"," ","D5"," ","F5"," ","H5",
        "A4"," ","C4"," ","E4"," ","G4",
        " ","B3"," ","D3"," ","F3"," ","H3",
        "A2"," ","C2"," ","E2"," ","G2",
        " ","B1"," ","D1"," ","F1"," ","H1",
    };
    for(i = 0;i < n;i  ){
        for(j = 0;j < n;j  ){
            printf("%c ",a[i][j]);
        }
        printf("\n");
    }
    return 0;
}

CodePudding user response:

There is absolutely no reason to use a 2D array here. Representation on the screen and memory layout of data structures don't need to match. As an example:

#include <stdio.h>

int main(void)
{
    unsigned int i;

    for (i = 0; i < 64; i  ) {
        if ((((i % 8)   (i / 8)) & 1) == 0) {
            printf("%c%u ", 'A'   (i % 8), 8 - (i / 8));
        } else {
            printf("   ");
        }

        if ((i % 8) == 7) {
            printf("\n");
        }
    }

    return 0;
}

This code isn't pretty but it works. It doesn't even use an array. All you need is some operations like division and modulus to determine row and column. Then you need to notice that squares with coordinates (X,Y) share the same color if X Y is even.

The code isn't pretty, but it uses very simple logic. As an exercise, within the for loop, try to get the coordinates X and Y into separate variables. Then it might be easier to understand.

CodePudding user response:

Please read the comments marked with // CHANGE HERE.

char a[100][100] creates 100 character strings each of max length 99 (1 character for null terminator '\0' (Imagine 100 rows with 99 columns)

#include <stdio.h>

int main()
{
    //int n = 8;  // CHANGE HERE - unused

    int i;

    // CHANGE HERE
    // 1. Replaced ' ' with '  ' (two spaces)
    // 2. Added '  ' after G for white H column cells
    char a[100][100] = {
        "A8","  ","C8","  ","E8","  ","G8","  ",
        "  ","B7","  ","D7","  ","F8","  ","H7",
        "A6","  ","C6","  ","E6","  ","G6","  ",
        "  ","B5","  ","D5","  ","F5","  ","H5",
        "A4","  ","C4","  ","E4","  ","G4","  ",
        "  ","B3","  ","D3","  ","F3","  ","H3",
        "A2","  ","C2","  ","E2","  ","G2","  ",
        "  ","B1","  ","D1","  ","F1","  ","H1",
    };
    
    // CHANGE HERE - chess board has 64 cells
    for (i = 0; i < 64; i  ) {
        // CHANGE HERE - print a new line after every 8 entries
        if (i != 0 && i % 8 == 0)
        {
            printf("\n");
        }
        printf("%s", a[i]);
    }
    printf("\n");
    return 0;
}
  • Related