我来这里是因为我有一个 C 项目,我需要在其中制作井字游戏。
我在这里做的是我问第一个玩家他想在哪里玩,我打印棋盘然后我检查是否有同一个玩家的对角线(目前只实现了\对角线),如果有 etat
= true.
问题出在 while 循环中,如果 compteur
= 9 或 etat
= true 它不会离开循环,我不明白为什么。我已经尝试使用调试器并且这些条件都是正确的。
printTab()
是一个简单的 printf 函数
saisieInt()
是一个带有 scanf 并验证数字不是 <1 或 >9
我是猴子吗?
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);
}
最佳答案
拥有
int colonne = choix % 3;
假设 choix 一个从 0 到 2 的正数 colonne 值
在
tab[ligne][colonne - 1] = joueur;
当 colonne 为 0 时,您修改 tab[ligne - 1][2]
或超出数组,这就是为什么在理论上是这样
就这样
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
编辑:你已经把问题解决了,我把时间浪费在一个无用的建议上了:-(
关于c - 虽然循环不退出但条件为真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55063581/