我正在用C编写一个tic tac toe游戏。
一切似乎都没问题,但我喜欢在用户输入错误数据的情况下为这样的程序实现错误处理技术。
我要求用户在1-9之间输入一个数字来填充tic tac toe图上的一个插槽。当我一起使用fgets和sscanf时,它在第一场比赛中表现良好。然后,当用户选择“Y”或“y”继续玩新游戏时,似乎没有任何变量值刷新,并且它基本上导致程序内的混乱。
任何提示?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
int printmatch(int array[3][3]);
int check(int array[3][3]);
char y;
char Y;
int complacer = 0;
int complacer2 = 0;
int main()
{
do
{
int fill = 0;
int j = 0;
int slot = 0;
int array[3][3];
array[0][0] = 0;
array[0][1] = 0;
array[0][2] = 0;
array[1][0] = 0;
array[2][0] = 0;
array[1][1] = 0;
array[2][1] = 0;
array[1][2] = 0;
array[2][2] = 0;
srand(time(NULL ));
printmatch(array);
char line[20];
do
{
do
{
tryagain: printf("\nEnter Position 1-9(from left to right):");
//I was using fgets and sscanf as an error handling technique incase user inputs incompatible data type, but after the 1st game is over, it seems this code messes up the functionality of the program
//fgets(line,sizeof(line),stdin);
//sscanf(line,"%d",&slot);
//when I just use scanf for data input, all is fine, but limited error handling
scanf("%d", &slot);
if (slot > 9 || slot < 1)
{
printf("Incorrect data input! Try again. \n");
}
} while (!(slot > 0 && slot < 10));
switch (slot)
{
case 1:
if (array[0][0] == -1)
{
printf("\nWoops, try again!");
goto tryagain;
}
array[0][0] = 1;
check(array);
break;
case 2:
if (array[1][0] == -1)
{
printf("\nWoops, try again!");
goto tryagain;
}
array[1][0] = 1;
check(array);
break;
case 3:
if (array[2][0] == -1)
{
printf("\nWoops, try again!");
goto tryagain;
}
array[2][0] = 1;
check(array);
break;
case 4:
if (array[0][1] == -1)
{
printf("\nWoops, try again!");
goto tryagain;
}
array[0][1] = 1;
check(array);
break;
case 5:
if (array[1][1] == -1)
{
printf("\nWoops, try again!");
goto tryagain;
}
array[1][1] = 1;
check(array);
break;
case 6:
if (array[2][1] == -1)
{
printf("\nWoops, try again!");
goto tryagain;
}
array[2][1] = 1;
check(array);
break;
case 7:
if (array[0][2] == -1)
{
printf("\nWoops, try again!");
goto tryagain;
}
array[0][2] = 1;
check(array);
break;
case 8:
if (array[1][2] == -1)
{
printf("\nWoops, try again!");
goto tryagain;
}
array[1][2] = 1;
check(array);
break;
case 9:
if (array[2][2] == -1)
{
printf("\nWoops, try again!");
goto tryagain;
}
array[2][2] = 1;
check(array);
break;
}
if (array[0][0] != 0 && array[0][1] != 0 && array[0][2] != 0
&& array[1][0] != 0 && array[2][0] != 0 && array[1][1] != 0
&& array[2][1] != 0 && array[1][2] != 0 && array[2][2] != 0)
{
check(array);
if (check(array) == 1)
{
printf("The user wins!\n");
}
else if (check(array) == -1)
{
printf("The computer wins.\n");
}
else
{
printmatch(array);
printf("It's a draw!\n");
}
goto done;
}
++fill;
label:
complacer = rand() % 3;
complacer2 = rand() % 3;
if (array[complacer][complacer2] == 0)
{
array[complacer][complacer2] = -1;
check(array);
}
else
goto label;
++fill;
printmatch(array);
int fullcheck = check(array);
if (fullcheck == 1)
{
printf("The user wins!");
break;
}
if (fullcheck == -1)
{
printf("The computer wins.");
break;
}
if (fill > 9)
break;
} while (fill < 10);
done: printf("\nDo you want to continue? Y/N\n");
scanf("%c %c", &y, &Y);
} while ((Y == 'Y' || Y == 'y'));
getchar();
return 0;
}
int printmatch(int array[3][3])
{
int i;
int j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d\t", array[j][i]);
}
printf("\n");
}
}
int check(int array[3][3])
{
int settle;
if (array[0][0] == 1 && array[1][1] == 1 && array[2][2] == 1)
{
settle = 1;
}
else if (array[0][0] == -1 && array[1][1] == -1 && array[2][2] == -1)
{
settle = -1;
}
if (array[0][0] == 1 && array[0][1] == 1 && array[0][2] == 1)
{
settle = 1;
}
else if (array[0][0] == -1 && array[0][1] == -1 && array[0][2] == -1)
{
settle = -1;
}
if (array[0][2] == 1 && array[1][2] == 1 && array[2][2] == 1)
{
settle = 1;
}
else if (array[0][2] == -1 && array[1][2] == -1 && array[2][2] == -1)
{
settle = -1;
}
if (array[0][1] == 1 && array[1][1] == 1 && array[2][1] == 1)
{
settle = 1;
}
else if (array[0][1] == -1 && array[1][1] == -1 && array[2][1] == -1)
{
settle = -1;
}
if (array[1][0] == 1 && array[1][1] == 1 && array[1][2] == 1)
{
settle = 1;
}
else if (array[1][0] == -1 && array[1][1] == -1 && array[1][2] == -1)
{
settle = -1;
}
if (array[0][0] == 1 && array[1][0] == 1 && array[2][0] == 1)
{
settle = 1;
}
else if (array[1][0] == -1 && array[1][1] == -1 && array[1][2] == -1)
{
settle = -1;
}
if (array[2][0] == 1 && array[1][1] == 1 && array[0][2] == 1)
{
settle = 1;
}
else if (array[1][0] == -1 && array[1][1] == -1 && array[1][2] == -1)
{
settle = -1;
}
if (array[2][0] == 1 && array[2][1] == 1 && array[2][2] == 1)
{
settle = 1;
}
else if (array[2][0] == -1 && array[2][1] == -1 && array[2][2] == 1)
{
settle = -1;
}
return settle;
}
答案 0 :(得分:1)
用户输入 evil 。当你想要一个号码时,有人会输入'A'。你想要一个'y'或'n',你得到几十个字母的输入。感谢您尝试使用防御性错误处理来改进编码。
使用sscanf()
或scanf()
等时,请务必检查结果
int result;
result = sscanf(buffer, "%this %that ...", &var1);
if (result != ExpectedResult) // handle error
将fgets()
与getchar()
和scanf()
混合可能会混淆问题。建议不要将fgets()
用于那些2. fgets()
是面向行的,scanf()
经常在使用新行之前停止。
仅仅输入'Y'或'y'继续使用它很有趣,但使用“letter&amp; enter_key”组合并不是那么糟糕,并且最初更容易理清。
fgets()
sscanf()
对可能导致打嗝,但最终更容易理清。建议返回该样式并检查sscanf()
的结果。
实施例
scanf("%d", &slot);
在按“Enter”之前,您不知道用户是否输入了任何数字。即使用户输入了一个数字,你的scanf()也会消耗前导空格和数字,但是为 next 输入函数留下'Enter'。代替:
int ScanCount;
const char *prompt = "\nEnter Position 1-9(from left to right):";
do {
fputs(prompt, stdout);
prompt = "Incorrect data input! Try again. \n"; // For the maybe next time around
if (fgets(line, sizeof(line), stdin) == NULL) {
// Standard input is closed or some grievous I/O error, let's go home.
return 0;
}
ScanCount = sscanf(line,"%d",&slot);
} while ((ScanCount != 1) || (slot < 1) || slot > 9));
顺便说一句:这是一个小技巧来测试是否在“9z”之类的数字之后输入额外的文本。将ScanCount = ... slot > 9));
替换为
char c;
ScanCount = sscanf(line,"%d%[^\n]",&slot, &c);
} while ((ScanCount != 1) || (slot < 1) || slot > 9)); // still ScanCount != 1