虽然循环不退出但条件为真

时间:2019-03-08 12:48:44

标签: c while-loop

我在这里是因为我有一个C项目,需要制作井字游戏。

我在这里所做的是,我问第一位玩家他想在哪里玩,我先印制板,然后检查同一位玩家是否有对角线(仅实现了\对角线),如果有{ {1}} =真。

问题出在while循环中,如果"type":"&app" = 9或etat = true,则不会退出循环,我也不明白为什么。我已经尝试过使用调试器,并且这些条件都成立。

compteur是一个简单的printf函数 etat是带有scanf的函数,它可以验证数字不是<1或> 9

我是猴子吗?

printTab()

2 个答案:

答案 0 :(得分:3)

拥有

int colonne = choix % 3;

假设 choix 为从0到2的正数 colonne

 tab[ligne][colonne - 1] = joueur;

colonne 为0时,您修改tab[ligne - 1][2]或不在数组中,这就是为什么在理论上是这种情况下,您找不到3个对齐的情况的原因

只要做

tab[ligne][colonne] = joueur;

这里有个建议:

#include <stdio.h>

#define COTE 3

void printTab(int (*tab)[COTE])
{
  for (int l = 0; l != COTE; ++l) {
    for (int c = 0; c != COTE; ++c) {
      printf("[%c]", *(" XO" + tab[l][c]));
    }
    putchar('\n');
  }
  putchar('\n');
}

int main()
{
  int tab[COTE][COTE] = { 0 };
  int joueur = 1, compteur = 0;

  printTab(tab);

  do {
    int l, c;

    printf("player %d, enter line and column (1..%d) : ", joueur, COTE);
    if ((scanf("%d %d", &l, &c) != 2) ||
        (l < 1) || (c < 1) ||
        (l > COTE) || (c > COTE) ||
        (tab[l - 1][c - 1] != 0)) {
      while (getchar() != '\n')
        ;
      puts("illegal position or not free");
    else {
      tab[l - 1][c - 1] = joueur;

      printTab(tab);      

      /* done ? */
      for (l = 0; l != COTE; ++l) {
        int j = tab[l][0];

        if (j != 0) {
          for (c = 1; ; c += 1) {
            if (c == COTE) {
              printf("joueur %d gagne\n", j);
              return 0;
            }
            if (tab[l][c] != j)
              break;
          }
        }
      }
      for (c = 0; c != COTE; ++c) {
        int j = tab[0][c];

        if (j != 0) {
          for (l = 1; ; l += 1) {
            if (l == COTE) {
              printf("joueur %d gagne\n", j);
              return 0;
            }
            if (tab[l][c] != j)
              break;
          }
        }
      }

      int j;

      j = tab[0][0];
      if (j != 0) {
        for (l = 0; ; l += 1) {
          if (l == COTE) {
            printf("joueur %d gagne\n", j);
            return 0;
          }
          if (tab[l][l] != j)
            break;
        }
      }

      j = tab[0][COTE - 1];
      if (j != 0) {
        for (l = 0; ; l += 1) {
          if (l == COTE) {
            printf("joueur %d gagne\n", j);
            return 0;
          }
          if (tab[l][COTE - l - 1] != j)
            break;
        }
      }

      if (++joueur == 3)
        joueur = 1;

      compteur += 1;
    }
  } while (compteur != COTE*COTE-1);

  puts("partie nulle");
}

编译和执行:

/tmp % gcc -pedantic -Wextra ttt.c
/tmp % ./a.out
[ ][ ][ ]
[ ][ ][ ]
[ ][ ][ ]

player 1, enter line and column (1..3) : a 2
illegal position or not free
player 1, enter line and column (1..3) : 1 4
illegal position or not free
player 1, enter line and column (1..3) : 1 1
[X][ ][ ]
[ ][ ][ ]
[ ][ ][ ]

player 2, enter line and column (1..3) : 2 1
[X][ ][ ]
[O][ ][ ]
[ ][ ][ ]

player 1, enter line and column (1..3) : 1 1
illegal position or not free
player 1, enter line and column (1..3) : 1 3
[X][ ][X]
[O][ ][ ]
[ ][ ][ ]

player 2, enter line and column (1..3) : 3 2
[X][ ][X]
[O][ ][ ]
[ ][O][ ]

player 1, enter line and column (1..3) : 2 1
illegal position or not free
player 1, enter line and column (1..3) : 1 2
[X][X][X]
[O][ ][ ]
[ ][O][ ]

joueur 1 gagne

/tmp % ./a.out
[ ][ ][ ]
[ ][ ][ ]
[ ][ ][ ]

player 1, enter line and column (1..3) : 1 1
[X][ ][ ]
[ ][ ][ ]
[ ][ ][ ]

player 2, enter line and column (1..3) : 2 2
[X][ ][ ]
[ ][O][ ]
[ ][ ][ ]

player 1, enter line and column (1..3) : 3 3
[X][ ][ ]
[ ][O][ ]
[ ][ ][X]

player 2, enter line and column (1..3) : 1 2
[X][O][ ]
[ ][O][ ]
[ ][ ][X]

player 1, enter line and column (1..3) : 3 2
[X][O][ ]
[ ][O][ ]
[ ][X][X]

player 2, enter line and column (1..3) : 3 1
[X][O][ ]
[ ][O][ ]
[O][X][X]

player 1, enter line and column (1..3) : 2 3
[X][O][ ]
[ ][O][X]
[O][X][X]

player 2, enter line and column (1..3) : 1 3
[X][O][O]
[ ][O][X]
[O][X][X]

joueur 2 gagne

编辑:很抱歉,您已经解决了这个问题,我浪费时间来做一个无用的建议:-(

答案 1 :(得分:2)

您的循环条件已损坏:

while ( compteur < 9 || etat != true) {

这意味着:当您尚未执行第9步或对角线不匹配时,请继续。

如果您有9招但没有赢家,那么它仍然继续。 另外,如果您有一条对角线但少于9个动作,则也继续。

如果其中一个条件成立,则必须终止。

while ( compteur < 9 && etat != true) {

注意: 这就是您在问题中提到的问题的原因。 尽管如此,还必须修正bruno指出的错误,以避免未定义的行为和潜在的崩溃等。