C中的Tic-Tac-Toe(使用2D字符阵列)

问题描述 投票:0回答:1

考虑过如何在C中实现Tic-Tac-Toe,我查看了其他各种帖子,但不幸的是遇到了问题。我有两个函数用于初始化和绘制网格,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
 .
 .
 .
 .
 .

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

c arrays tic-tac-toe
1个回答
1
投票

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

我还添加了代码来标记行和列。请注意,当gridizeize大于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$ 
© www.soinside.com 2019 - 2024. All rights reserved.