C 中的井字棋(使用二维字符数组)
Tic-Tac-Toe in C (Using a 2D Character Array)
我查看了考虑如何在 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
.
.
.
.
.
我希望我已经把一切都说清楚了。我现在尝试了各种不同的方法,但就是无法正确打印 board/grid。
以上评论都是正确的。我已将所有更正后的代码放在下面,现在代码按预期运行。
我还添加了代码来标记行和列。注意当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 中实现 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
.
.
.
.
.
我希望我已经把一切都说清楚了。我现在尝试了各种不同的方法,但就是无法正确打印 board/grid。
以上评论都是正确的。我已将所有更正后的代码放在下面,现在代码按预期运行。
我还添加了代码来标记行和列。注意当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$