c - 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
.
.
.
.
.
我希望我已经把一切都说清楚了。我现在尝试了各种不同的方法,但无法正确打印板/网格。
解决方案
上面的评论都是正确的。我已将所有更正的代码放在下面,现在代码按预期运行。
我还添加了代码来标记行和列。请注意,当 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$
推荐阅读
- php - 如何删除内部联接中的查询
- azure - Quectel BG77 在尝试向 Azure 发送 POST 请求时返回 HTTPPOST: 719 错误
- node.js - 在 React Native 中使用自定义日历阻止日期
- javascript - chrome 扩展中出现意外的令牌错误
- shopify - 如何在不使用脚本标签的情况下在 shopify 中添加自定义脚本
- flutter - 应用程序因“没有 StoreProvider”而崩溃
> 找到。”在启动时。颤振还原 - node.js - 未处理的拒绝(TypeError):adminList 不可迭代
- python - 读取 tsv 文件时出现 UnicodeDecodeError
- android - 深色状态栏中的浅色文本在 Android 11 中不起作用
- c# - C# 将 JSON 文件保存为 SQL 数据库中的字段需要什么数据类型?