0

我正在用 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;
}
4

1 回答 1

1

用户输入是邪恶的。当你想要一个数字时,有人会输入“A”。你想要一个 'y' 或 'n' 并且你得到了几十个字母的输入。感谢您尝试通过防御性错误处理来改进您的编码。

使用sscanf()orscanf()等​​时,一定要检查结果

int result;  
result = sscanf(buffer, "%this %that ...", &var1);  
if (result != ExpectedResult) // handle error

fgets()getchar()和混合scanf()可能会使问题混淆。建议不要fgets()与那些 2. fgets()是面向行的,scanf()经常在使用新行之前停止。

只输入“Y”或“y”继续很有趣,但使用“letter & enter_key”组合并没有那么糟糕,而且一开始更容易整理。

fgets() sscanf()对可能造成了打嗝,但最终更容易解决。建议返回该样式并检查sscanf().

例子

scanf("%d", &slot);

在这里,您不知道用户在按“Enter”之前是否输入了任何数字。即使用户确实输入了一个数字,您的 scanf() 也会消耗前导空格和数字,但将“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
于 2013-08-01T00:51:14.490 回答