Имея
int colonne = choix % 3;
, предполагая choix положительное число двоеточие значения от 0 до 2
In
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
edit: оупс вы уже поставили вопрос решенным, я теряю время на бесполезное предложение: - (