我正在用 C 编写井字游戏。
一切似乎都很顺利,但我喜欢为这样的程序实施错误处理技术,以防用户输入错误数据。
我要求用户输入一个 1-9 之间的数字来填充井字游戏图上的一个空位。当我一起使用 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;
}