我查看了其他各种文章,考虑了如何在C语言中实现井字游戏,但不幸的是遇到了问题。我有两个用于初始化和绘制网格的函数int init_grid(int gridsize)void draw_grid(int gridsize)。这些参数采用参数gridsize,因为用户可以从3x3到10x10的网格中进行选择。该程序到目前为止已经编译完毕,但是当输入板子的大小时,它会打印正确的数字“。”。字符,但仅在第一列中。

代码如下:

init_grid

int init_grid(int gridsize) {


for (int row = 0; row < gridsize ; row++) {

    for (int col = 0; col < gridsize; col++) {
        grid[row][col] = '.';
    }
}

if (gridsize > MaxGrid) {
    puts("Error, gridsize too large.");
    return 1;
}

else {
    return 0;
  }
}


draw_grid

void draw_grid(int gridsize) {

for (int row = 0; row < gridsize; row++)
{
    for (int col = 0; row < gridsize; row++)
    {
        putchar (' ');
        if (grid[row][col]) {
            putchar (grid[row][col]);
        }
        else {
            putchar ('.');
        }

    printf("\n");
   }
  }
 }


主要

int main() {

int gridsize = 0;

printf("Hello and welcome to Tic Tac Toe\n");
printf("Please enter the size of the grid you would like to play with (between 3 and 10):\n");
scanf("%d", &gridsize);

init_grid(gridsize);
draw_grid(gridsize);

return 0;
}


输出量

Hello and welcome to Tic Tac Toe
Please enter the size of the grid you would like to play with (between 3 and
10):
 5
 .
 .
 .
 .
 .




我希望我已经把一切弄清楚了。我现在已经尝试了各种不同的方法,但无法正确打印电路板/网格。

最佳答案

上面的评论都是正确的。我将所有更正的代码放在下面,现在代码按预期执行了。

我还添加了代码以标记行和列。请注意,当gridsize大于9时,需要进行更改。

#include <stdio.h>

int init_grid(int gridsize);
void draw_grid(int gridsize);

char grid[25][25];
int MaxGrid = 25;


int init_grid(int gridsize)
{
    for (int row = 0; row < gridsize ; row++)
    {
        for (int col = 0; col < gridsize; col++)
        {
            grid[row][col] = '.';
        }
    }

    if (gridsize > MaxGrid)
    {
        puts("Error, gridsize too large.");
        return 1;
    }
    else
    {
        return 0;
    }
}


void draw_grid(int gridsize)
{

    printf("    ");
    for( int i=0; i < gridsize; i++ )
    {
        printf("%d ", i+1);
    }
    printf("\n");

    for (int row = 0; row < gridsize; row++)
    {
        printf("%d  ", row+1);
        for (int col = 0; col < gridsize; col++)
        {
            putchar (' ');
            if (grid[row][col])
            {
                putchar (grid[row][col]);
            }
            else
            {
                putchar ('.');
            }
        }
        printf("\n");
    }
}


int main()
{

    int gridsize = 0;

    printf("Hello and welcome to Tic Tac Toe\n");
    printf("Please enter the size of the grid you would like to play with (between 3 and 10):\n");
    scanf("%d", &gridsize);

    init_grid(gridsize);
    draw_grid(gridsize);

    return 0;
}


输出:

jnorton@ubuntu:~/source$ ./a.out
Hello and welcome to Tic Tac Toe
Please enter the size of the grid you would like to play with (between 3 and 10):
5
    1 2 3 4 5
1   . . . . .
2   . . . . .
3   . . . . .
4   . . . . .
5   . . . . .
jnorton@ubuntu:~/source$ ./a.out
Hello and welcome to Tic Tac Toe
Please enter the size of the grid you would like to play with (between 3 and 10):
3
    1 2 3
1   . . .
2   . . .
3   . . .
jnorton@ubuntu:~/source$ ./a.out
Hello and welcome to Tic Tac Toe
Please enter the size of the grid you would like to play with (between 3 and 10):
10
    1 2 3 4 5 6 7 8 9 10
1   . . . . . . . . . .
2   . . . . . . . . . .
3   . . . . . . . . . .
4   . . . . . . . . . .
5   . . . . . . . . . .
6   . . . . . . . . . .
7   . . . . . . . . . .
8   . . . . . . . . . .
9   . . . . . . . . . .
10   . . . . . . . . . .
jnorton@ubuntu:~/source$

关于c - C中的井字游戏(使用2D字符数组),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54675993/

10-11 19:19