Tic-Tac-Toe游戏的C语言二维数组为什么出错*
第一个错误:8 22 I:\C语言学习文件\学习\Tic-tac-toe游戏网上版.cpp [Error] use of parameter outside function body before ‘]’ token
第二个错误:35 22 I:\C语言学习文件\学习\Tic-tac-toe游戏网上版.cpp [Error] ‘num’ was not declared in this scope
第三个错误:40 8 I:\C语言学习文件\学习\Tic-tac-toe游戏网上版.cpp [Error] ‘a’ was not declared in this scope
#include <stdio.h>
/*Tic-Tac-Toe游戏,主函数输入一个棋盘规模大小,
然后接着输入目前棋盘的形态,
然后调用judge函数,判断游戏是否结束的简单小游戏程序。
主要考察二维数组和标记变量的知识点。*/
int judge(int num,int a[num][num]); //函数声明
int main(void){
int num; //棋盘的大小
while (scanf("%d", &num) != EOF) //这是什么判断?
{
int a[num][num]; //输入棋盘内的数字
for(int i=0; i<num; i++){
for (int j=0; j<num; j++){
scanf("%d",&a[i][j]);
}
}
int over = judge(num,a);
if (over){
printf("Game Over!\n");
} else{
printf("Please Continue\n");
}
}
return 0;
}
int judge(int num,int a[num][num])
{
int over; //游戏结束的标志位
for (int i = 0; i < num; i++) //循环检查每一行的情况
{
int j;
for (j = 1; j < num; j++) //检查每一行和第一列是否匹配,不匹配跳出一层循环继续看下一行
{
if (a[i][0] != a[i][j]) { //判断第一个数字和其他几列数字是否相等,如果不等,后面的直接不用判断
over = 0; //游戏还没有结束
break;
}
}
if (j == num) //遍历完一行了,有相同的一行,游戏结束跳出循环
{
over = 1;
break;
}
}
if (!over){
for (int j = 0; j < num; j++) //循环检查每一列的情况
{
int i;
for (i = 1; i < num; i++) //遍历行循环
{
if (a[0][j] != a[i][j])
{
over = 0;
break;
}
}
if (i == num)
{
over = 1;
break;
}
}
}
if (!over) //检查主对角线
{
int i;
for (i = 1; i < num; i++)
{
if (a[0][0] != a[i][i])
{
over = 0;
break;
}
}
if (i == num)
{
over = 1;
}
}
if (!over)
{
int j;
for (j = 0; j < num - 1; j++)
{
if (a[0][num - 1] != a[num - 1 - j][j])
{
over = 0;
break;
}
}
if (j == num - 1)
{
over = 1;
}
}
if (!over)
{
return 0;
}
else
{
return 1;
}
}
8 28 I:\C语言学习文件\学习\Tic-tac-toe游戏网上版.cpp [Error] use of parameter outside function body before ‘]’ token
8 33 I:\C语言学习文件\学习\Tic-tac-toe游戏网上版.cpp [Error] use of parameter outside function body before ‘]’ token
32 28 I:\C语言学习文件\学习\Tic-tac-toe游戏网上版.cpp [Error] use of parameter outside function body before ‘]’ token
32 33 I:\C语言学习文件\学习\Tic-tac-toe游戏网上版.cpp [Error] use of parameter outside function body before ‘]’ token
I:\C语言学习文件\学习\Tic-tac-toe游戏网上版.cpp In function ‘int judge(…)’:
35 22 I:\C语言学习文件\学习\Tic-tac-toe游戏网上版.cpp [Error] ‘num’ was not declared in this scope
40 8 I:\C语言学习文件\学习\Tic-tac-toe游戏网上版.cpp [Error] ‘a’ was not declared in this scope
53 23 I:\C语言学习文件\学习\Tic-tac-toe游戏网上版.cpp [Error] ‘num’ was not declared in this scope
58 9 I:\C语言学习文件\学习\Tic-tac-toe游戏网上版.cpp [Error] ‘a’ was not declared in this scope
74 19 I:\C语言学习文件\学习\Tic-tac-toe游戏网上版.cpp [Error] ‘num’ was not declared in this scope
76 8 I:\C语言学习文件\学习\Tic-tac-toe游戏网上版.cpp [Error] ‘a’ was not declared in this scope
82 12 I:\C语言学习文件\学习\Tic-tac-toe游戏网上版.cpp [Error] ‘num’ was not declared in this scope
90 19 I:\C语言学习文件\学习\Tic-tac-toe游戏网上版.cpp [Error] ‘num’ was not declared in this scope
92 8 I:\C语言学习文件\学习\Tic-tac-toe游戏网上版.cpp [Error] ‘a’ was not declared in this scope
98 12 I:\C语言学习文件\学习\Tic-tac-toe游戏网上版.cpp [Error] ‘num’ was not declared in this scope
a 和num 在函数声明中都定义了int 类型了呀
为什么还会出错**