Bonjour, suite au tutoriel sur le C, j'ai essayé de faire le puissance 4 mais j'ai un soucis.

Voilà le code des fonctions :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 
 
 
int Puiss4_indice(Puiss4 A, int nb_case)
{
  int current_indice = 0;
  int j;
  for(j = 0 ; j < A->lignes ; j++)
  {
      if((A->data[j][nb_case - 1] == 'x') || (A->data[j][nb_case - 1] == 'o'))
      {
        current_indice = j;
        printf("Current_indice : %d\n", current_indice);
        return current_indice;
      }
 
  }
}
int Puiss4_parcours_vertical(Puiss4 A, int nb_case)
{
  int j;
  int compteur = 1;
  for(j = 0 ; j < A->lignes - 1 ; j++)
  {
    if(A->data[j][nb_case - 1] == A->data[j + 1][nb_case - 1])
    {
      compteur++;
    }
    else
      compteur = 1;
 
  }
  return compteur;
}
 
/*void Puiss4_parcours_diagonal(Puiss4 A)
{
 
}*/
int Puiss4_parcours_horizontal(Puiss4 A, int nb_case)
{
  int i;
  int compteur = 1;
  printf("Compteur : %d\n", compteur);
  int current_indice = Puiss4_indice(A, nb_case);
  for(i = 0 ; i < A->colonnes - 1 ;i++)
  {
    if(A->data[current_indice - 1][i] == A->data[current_indice - 1][i + 1])
    {
      compteur++;
      printf("%c = %c \n", A->data[current_indice - 1][i],A->data[current_indice - 1][i + 1]);
      printf("Compteur : %d\n", compteur);
    }
    else
      compteur = 1;
  }
  return compteur;
}
Le main :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
 
#include <stdio.h>
#include <stdlib.h>
#include "puiss4Proto.h"
#define sortie 4
 
 
int main(int argc, char** argv)
{
  int indice = 0;
  int nb_case = 0;
  int compteur;
  int compteur2;
  /*int sortie = 4;*/
 
  Puiss4 A = Puiss4_create(6, 7, ' ');
  printf("Bienvenue sur le jeu du Puissance 4 !\n");
  printf("Joueur 1 : x\n");
  printf("Joueur 2 : o\n");
 
 
  while(compteur != sortie)
  {
  Puiss4_print(A);
  printf("\n");
  indice = Puiss4_indice(A, nb_case);
  printf("\n");
  printf("Joueur 1 : ");
  scanf("%d", &nb_case);
  Puiss4_add(A, nb_case, 'x');
  Puiss4_print(A);
  printf("\n");
  compteur = Puiss4_parcours_vertical(A, nb_case);
  printf("Puiss4_parcours_vertical : Compteur | Sortie = %d | %d \n", compteur, sortie);
  printf("\n");
  /*compteur2 = Puiss4_parcours_horizontal(A, nb_case);
  printf("Puiss4_parcours_horizontal : Compteur | Sortie = %d | %d \n", compteur2, sortie);
  printf("\n");*/
  indice = Puiss4_indice(A, nb_case);
  printf("\n");
  printf("Joueur 2 : ");
  scanf("%d", &nb_case);
  Puiss4_add(A, nb_case, 'o');
  printf("\n");
  Puiss4_print(A);
  printf("\n");
  compteur = Puiss4_parcours_vertical(A, nb_case);
  printf("Puiss4_parcours_vertical : Compteur | sortie = %d | %d\n", compteur, sortie);
  printf("\n");
  /*compteur2 = Puiss4_parcours_horizontal(A, nb_case);
  printf("Puiss4_parcours_horizontal : Compteur | Sortie = %d | %d \n", compteur2, sortie);
  printf("\n");*/
  indice = Puiss4_indice(A, nb_case);
  printf("\n");
  }
 
  Puiss4_destroy(A, 6, 7);
  printf("Vous avez gagné !\n");
 
  return EXIT_SUCCESS;
}
voilà les problèmes :

- se situe au niveau de la fonction Puiss4_parcours_horizontal, dès le premier caractère saisi, compteur est égal à 7 alors qu'il devrait être égal à 1;


-La fonction qui parcours le tableau de façon verticale marche par contre lorsque j'ai 4 jetons empilé o ça quitte bien la boucle mais lorsque c'est 4 jetons x ça ne quitte pas la boucle alors que compteur est bien égal à 4 et la variable sortie l'est également;