Bonjour les experts !

Me mettant à la programmation ces temps-ci, je cherche à coder un puissance 4 en C, qui je crois est un exercice de TP assez fréquent.

Pour moi c'est compliqué, j'ai besoin de pratiquer.

Pour le moment, j'affiche la grille et je la remplis avec les jetons de chaque joueur, à tour de rôle.

Cependant, j'ai un bug que je ne comprends pas :

- quand un joueur place un jeton en colonne 0 cela place également un jeton à la (colonne 6) (ligne -1)
- et quand un joueur place un jeton en colonne 6 cela place un jeton aussi en (colonne 0)(ligne-1)

C'est très bizarre. Merci pour votre aide

Voici le code :
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <stdio.h>
#include <stdlib.h>
 
#define HAUTEUR 6
#define LARGEUR 7
char damier [LARGEUR][HAUTEUR];
int joueur = 1;
int nbtokens = HAUTEUR*LARGEUR;
char symbols[2];
//symbols[0] = 'X';
//symbols[1] = 'O';
 
void initdamier()
{
	int i;
	int j;
	for (i = 0; i < HAUTEUR; i++)
	{
		for (j = 0; j < LARGEUR; j++)
		{
			damier[i][j] = ' ';
		}
	}
}
 
int askcolonne(int joueur)
{
	int col;
	do
	{
		printf("Joueur %i, à vous de jouer !\n", joueur);
		printf("Entrez un numéro de colonne entre 0 et 6\n");	
		scanf("%d", &col); 
	}
	while (col<0 || col>6);
	printf("Le numéro de colonne choisi est %i\n", col);
	return col; 
}	
 
void displaydamier()
{
	int i;
	int j;
	printf("Tableau Puissance 4 :\n");
	printf("---------------------\n");
	for (i = 0; i < HAUTEUR; i++)
	{
		printf("+---+---+---+---+---+---+---+\n");
		printf("|");
		for (j = 0; j < LARGEUR; j++)
		{
			printf(" %c |", damier[i][j]);
		}
		printf("\n");
		}
	printf("+---+---+---+---+---+---+---+\n");
	printf("  0   1   2   3   4   5   6 \n");
	//printf("Le nombre de jetons restant dans le tableau est : %i\n", nbtokens);
 
}
 
void affichedamier()
{
	int m;
	int n;
	for (m = 0; m < 6; m++)
	{
		for (n = 0; n < 7; n++)
		{
			printf("Damier[%i][%i]=%c",m,n,damier[m][n]);
		}
		putchar('\n');
	}
 
}
 
 
int calcule_position(int col)
{
 
	int i = 6;
	int lig;
	do
	{
		i--;
	}
	while (damier[i][col] != ' ');
 
	//if (i >= 0 && i <= 5)
	//{
	lig = i;
 
	//damier[lig][col] = symbol;
//	printf("la valeur en ligne %i colonne %i vaut %c\n", lig, col, damier[lig][col]);
	return lig;
	//}
}
 
int searchp4(int col,int ligne)
{
	return 0; 
}
 
void displayresult(int winner, int joueur)
{
 
}
 
int main (int argc, char ** argv)
{
	int winner = 0;
	char symbol;
	initdamier();
	displaydamier();
	do {
		joueur = (joueur + 1)%2;
		int colonne = askcolonne(joueur);
		printf("Colonne vaut %i\n", colonne);
		int ligne = calcule_position(colonne);
		printf("Ligne vaut %i\n", ligne);
 
		if (joueur == 0)
		{
			symbol = 'X';
		}	
		else
		{
			symbol = 'O';
		}
		damier[ligne][colonne] = symbol;
		printf("symbol = %c\n", symbol);
 
		winner = searchp4(colonne,ligne);
		nbtokens--;
		displaydamier();
		printf("Winner vaut %i\n", winner);
	}
	while ( winner!=1 || nbtokens );
 
}