如何将国际象棋棋盘格内容显示为C语言中的字符串并将字符串存储在表格中?

时间:2022-06-16 08:03:08

How can I display the chess board content as strings in C language (the chess pieces and dots or spaces for the empty spots) and store the strings in a table ? I can show what I have already done.

如何将国际象棋棋盘格内容显示为C语言中的字符串(空白点的棋子和点或空格)并将字符串存储在表格中?我可以展示我已经做过的事情。

2 个解决方案

#1


1  

In general what you need is an 8x8 array of strings. Since C strings are them selves zero-terminated char arrays, it ends up as a 3D char array.

一般来说,你需要的是一个8x8的字符串数组。由于C字符串是自己的零终止字符数组,因此它最终成为3D字符数组。

Something like:

#define MAX_TEXT 30
char board[8][8][MAX_TEXT];

int i, j;
for (i=0; i<8; ++i)
{
    for (j=0; j<8; ++j)
    {
        strcpy(board[i][j], ".");  // Make all spots empty
    }
}

strcpy(board[0][1], "knight"); // Put a knight at location (0, 1)

// and so on ...

Update due to comment

由于评论而更新

To place the 4 knights using loops, you can do something like:

要使用循环放置4个骑士,您可以执行以下操作:

for (i=0; i<8; i = i + 7)  // i will be 0 and 7
{
    for (j=1; j<8; j = j + 5)  // j will be 1 and 6
    {
        strcpy(board[i][j], "knight"); // Put a knight at location (0, 1)
                                       //                          (0, 6)
                                       //                          (7, 1)
                                       //                          (7, 6)
    }
}

p.s. I hope the locations are the correct once - I'm not a chess player...

附:我希望这些地点是正确的 - 我不是国际象棋选手......

#2


4  

char board[][sizeof("♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜")] = {
    {"♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜"},
    {"♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟"},
    {"… … … … … … … …"},
    {"… … … … … … … …"},
    {"… … … … … … … …"},
    {"… … … … … … … …"},
    {"♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙"},
    {"♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖"}
};

Each piece of the board and the dots are multibyte characters

每块板和点都是多字节字符

strlen("♜") == 3

strlen(“♜”)== 3

strlen("…") == 3

strlen(“...”)== 3

An example moving the horse:

移动马的一个例子:

♜ … ♝ ♛ ♚ ♝ ♞ ♜
♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
… … ♞ … … … … …

#define ROWS 8
#define DOT "…"
#define MBSZ sizeof(DOT)
#define CELLS (MBSZ + 1)

char *pt1 = board[0] + (CELLS * 1); /* 1 cell */
char *pt2 = board[2] + (CELLS * 2); /* 2 cells */

memmove(pt2, pt1, MBSZ);
memmove(pt1, DOT, MBSZ);

for (int i = 0; i < ROWS; i++) {
    printf("%s\n", board[i]);
}

#1


1  

In general what you need is an 8x8 array of strings. Since C strings are them selves zero-terminated char arrays, it ends up as a 3D char array.

一般来说,你需要的是一个8x8的字符串数组。由于C字符串是自己的零终止字符数组,因此它最终成为3D字符数组。

Something like:

#define MAX_TEXT 30
char board[8][8][MAX_TEXT];

int i, j;
for (i=0; i<8; ++i)
{
    for (j=0; j<8; ++j)
    {
        strcpy(board[i][j], ".");  // Make all spots empty
    }
}

strcpy(board[0][1], "knight"); // Put a knight at location (0, 1)

// and so on ...

Update due to comment

由于评论而更新

To place the 4 knights using loops, you can do something like:

要使用循环放置4个骑士,您可以执行以下操作:

for (i=0; i<8; i = i + 7)  // i will be 0 and 7
{
    for (j=1; j<8; j = j + 5)  // j will be 1 and 6
    {
        strcpy(board[i][j], "knight"); // Put a knight at location (0, 1)
                                       //                          (0, 6)
                                       //                          (7, 1)
                                       //                          (7, 6)
    }
}

p.s. I hope the locations are the correct once - I'm not a chess player...

附:我希望这些地点是正确的 - 我不是国际象棋选手......

#2


4  

char board[][sizeof("♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜")] = {
    {"♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜"},
    {"♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟"},
    {"… … … … … … … …"},
    {"… … … … … … … …"},
    {"… … … … … … … …"},
    {"… … … … … … … …"},
    {"♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙"},
    {"♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖"}
};

Each piece of the board and the dots are multibyte characters

每块板和点都是多字节字符

strlen("♜") == 3

strlen(“♜”)== 3

strlen("…") == 3

strlen(“...”)== 3

An example moving the horse:

移动马的一个例子:

♜ … ♝ ♛ ♚ ♝ ♞ ♜
♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
… … ♞ … … … … …

#define ROWS 8
#define DOT "…"
#define MBSZ sizeof(DOT)
#define CELLS (MBSZ + 1)

char *pt1 = board[0] + (CELLS * 1); /* 1 cell */
char *pt2 = board[2] + (CELLS * 2); /* 2 cells */

memmove(pt2, pt1, MBSZ);
memmove(pt1, DOT, MBSZ);

for (int i = 0; i < ROWS; i++) {
    printf("%s\n", board[i]);
}