简体   繁体   English

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

[英]While loop doesnt exit but condition are true

I'm here beacause i have a C project where i need to make a Tic Tac Toe. 我在这里是因为我有一个C项目,需要制作井字游戏。

What I do here is that i ask the first player where he wants to play, I print the board then I check if there is a diagonal (only the \\ diagonal is implemented yet) of the same player, if there is etat = true. 我在这里所做的是,我问第一个玩家想要在哪里玩,我先打印板子,然后检查是否存在同一玩家的对角线(只有\\对角线已实现),如果存在etat = true。

The problem is in the while loop, if compteur = 9 or etat = true it doesn't leave the loop and i don't understand why. 问题是在while循环中,如果compteur = 9或etat = true,它不会离开循环,我也不明白为什么。 I have tried with debuger and those condition are true. 我已经尝试过使用调试器,并且那些条件是正确的。

printTab() is a simple printf function saisieInt() is a function with scanf and verification that the number isn't <1 or >9 printTab()是一个简单的printf函数saisieInt()是具有scanf并验证数字不是<1或> 9的函数

Am I a monkey ? 我是猴子吗?

int main()  
{  
  int tab[COTE][COTE] = { 0 };  
  int compteur = 0, 
  joueur = 1,
  choix;  
  bool etat=false;
  printf("commande : ");
  choix = saisieInt();
  compteur++;

  while ( compteur < 9 || etat != true) {
///position where to place the piece///////////////////////////////
    int colonne = choix % 3;
    int ligne = choix / 3;
    tab[ligne][colonne - 1] = joueur;
///////////////////////////////////////////////

    printTab(tab);

///switch between the 2 players///////////////////////////////
    if (joueur == 1)
      joueur = 2;
    else
      joueur = 1;
///////////////////////////////////////////////

///check if one has a diagonal line //////////////////////////
    if (compteur >= 6) {
      int compteurdiag = 0;
      for (int i = 0; i < COTE; i++) {
        if (tab[i][i] == joueur) {
          compteurdiag++;
        }
        else {
          compteurdiag = 0;
        }
        if (compteurdiag == COTE)
          {
            etat = true;
          }
      }
    }
///////////////////////////////////////////////
//if (etat == false) {
    printf("compteur : %d commande : ", compteur);
    choix = saisieInt();
    compteur++;
//}
  }
  printf("compteur : %d termine\n", compteur);
}

void printTab(int t[COTE][COTE]) {
    int i, j;
    puts("\n|---|---|---|");
    for (i = 0; i < COTE; i++) {
        for (j = 0; j < COTE; j++) {
            printf("|%2d ", t[i][j]);
        }
        puts("|");
        for (j = 0; j < COTE; j++) {
            printf("|---");
        }
        puts("|");
    }
}


int saisieInt() {
    int valeur, n;
    n = scanf("%d", &valeur);
    while (n != 1  || valeur > 9) {
        printf("Attention, erreur de saisie\nRechoisissez : ");
        while (getchar() != '\n');
        n = scanf("%d", &valeur);
    }
    return(valeur);
}

Having

 int colonne = choix % 3; 

supposing choix a positive number colonne values from 0 to 2 假定从0到2 喜爱将一个正数Colonne的

In

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

when colonne is 0 you modify tab[ligne - 1][2] or out of the array, this is why you do not find 3 aligned cases when in theory it is the case Colonne为0时,您可以修改tab[ligne - 1][2]或不在数组中,这就是为什么在理论上没有找到3个对齐情况的原因

just do 做就是了

tab[ligne][colonne] = joueur;

Here a proposal : 这里有个建议:

#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");
}

Compilation and execution : 编译与执行:

/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

edit : oupps you already put the question solved, I loose my time for a useless proposal :-( 编辑:oupps,您已经把问题解决了,我浪费了时间,没有用的提案:-(

Your condition for the loop is broken: 您的循环条件已损坏:

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

This means: While you haven't done the 9th move yet or there is not matching diagonal, continue. 这意味着:当您尚未执行第9步或对角线不匹配时,请继续。

If you have 9 moves but no winner, it still goes on. 如果您有9招但没有获胜者,那么它仍然继续。 Also if you have a diagonal line but less than 9 moves, continue as well. 另外,如果您有一条对角线但少于9个动作,则也要继续。

You must terminate if one of the conditions is true, not both. 如果满足以下条件之一,则必须终止,但不能同时满足。

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

Note: This is the reason for the problem you stated in your question. 注意:这就是您在问题中提到的问题的原因。 Nevertheless the error pointed out by bruno also has to be fixed to avoid undefined behaviour and potential crashes etc.. 不过,还必须解决bruno指出的错误,以避免未定义的行为和潜在的崩溃等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM