首页 > 解决方案 > C 中的井字游戏(使用 2D 字符数组)

问题描述

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

代码如下:

初始化网格

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
 .
 .
 .
 .
 .

期望的输出

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

标签: carraystic-tac-toe

解决方案


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

我还添加了代码来标记行和列。请注意,当 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$ 

推荐阅读