drinky05
Messages postés1Date d'inscriptiondimanche 16 mars 2008StatutMembreDernière intervention10 mai 2008
-
10 mai 2008 à 11:12
StormEnnairo
Messages postés4Date d'inscriptionvendredi 24 février 2012StatutMembreDernière intervention27 février 2012
-
27 févr. 2012 à 19:31
Bonjour, j'aurai besoin d'aide pour mon programme, grand débutant, c'est pour un projet en cours, je dois réaliser un puissance 4, il fonctionne a peu près comme je le souhaite mais j'ai un probleme pour les tests. En vertical ça fonctionne, mais pas en horizontal, je ne comprends pas pouquoi, ça parait juste pourtant .
Voila le programme, merci d'avance pour votre aide.
#include
<stdio.h>
#define N 6
#define M 7
char
tab[N][M];
void
initgrille (char tab[N][M])
{
int i;
for(i= 0;i<6;i++)
{
int j;
for(j=0;j<7;j++)
{
tab [i][j]=' ';
}
}
}
static void afficherGrille (char tab[N][M])
{
//Affiche la grille
en mode texte, et pose un jeton O ou X dans
//la case
souhaitée.
int i,j;
for (i = 0; i < N; i++)
{
printf("\n |");
for (j = 0; j < M; j++)
{
printf ("+ " );
}
printf ("+|\n |" );
for (j = 0; j < M; j++)
{
printf (" %c ", tab[i][j]);
}
printf (" |" );
}
printf("\n |");
for (j = 0; j < M; j++)
{
printf ("+ " );
}
printf ("+|\n" );
printf("
-----------------------------\n");
printf(" 0 1
2 3 4
5 6 \n\n");
}
int placejeton1 (char tab[N][M],int joueur)
{
int
lignes,colonnes,ok =0,res1,res2;
scanf("%d",&colonnes);
for(lignes= M;
lignes>=0 && ok==0; lignes--)
{
if(tab[lignes][colonnes] == ' ')
{
tab[lignes][colonnes]='X';
ok=1;
res1 =test_vertical(lignes,colonnes,1);
res2= test_horizontal(lignes,colonnes,1);
if(res1==0 || res2==0)
return 1;
return 2;
}
}
printf("La
colonne %d est pleine, choisissez une autre colonne",colonnes);
return 0;
}
int placejeton2 (char tab[N][M],int joueur)
{
int
lignes,colonnes,ok =0,res1,res2;
scanf("%d",&colonnes);
for(lignes= M;
lignes>=0 && ok==0; lignes--)
{
if(tab[lignes][colonnes] == ' ')
{
tab[lignes][colonnes]='O';
ok=1;
res1 =test_vertical(lignes,colonnes,2);
res2= test_horizontal(lignes,colonnes,2);
if(res1==0 || res2==0)
return 1;
return 2;
}
}
printf("La
colonne %d est pleine, choisissez une autre colonne",colonnes);
return 0;
}
int test_vertical(int x, int y,int joueur)
{
int i,j;
char jeton;
if(joueur == 1)
{
jeton ='X';
}
else if(joueur= =2)
{
jeton ='O';
}
/* test direction verticale */
printf("jeton :%c %d test avec
%c\n",tab[x][y],x,jeton);
for(i= x;tab[i][y]==jeton &&
i<N;i++)
{
printf("jeton
: %c %d\n",tab[i][y],i);
}
if((i-x)>3)
return 1;
return 0;
}
int test_horizontal(int x, int y,int joueur)
{
int i,j;
char jeton;
if(joueur == 1)
{
jeton ='X';
}
else if(joueur= =2)
{
jeton ='O';
}
/* test direction
horizontale */
printf("jeton
: %c %d test avec %c\n",tab[x][y],y,jeton);
for(i= y;tab[x][i]==jeton && i<M
;i++)
{
printf("jeton : %c
%d\n",tab[x][i],i);
}
for(j=y;tab[x][j]==jeton && j>0
; j++)
{
printf("jeton : %c
%d\n",tab[x][j],j);
if((y-j)>3 || (i-y)>3)
return 1;
return 0;
}
int main(int argc, char *argv[])
{
char j1[20],j2[20];
int t,r =0,joueur;
printf("Nom du
premier joueur:\n");
fgets(j1,20,stdin);
printf("Nom du
deuxieme joueur:\n");
fgets(j2,20,stdin);
initgrille (tab);
afficherGrille
(tab);
do
{
do
{
printf(" %s choisissez une colonne:\n",j1);
r= placejeton1
(tab,joueur);
if(r==2)
{
printf("
%s GAGNE la partie
!!!!!!!!!!!!!!!!!!!\n",j1);
}
afficherGrille (tab);
}
while(r==0);
do
{
printf("
%s choisissez une colonne:\n",j2);
r=placejeton2 (tab,joueur);
if(r ==2)
{
printf("
%s GAGNE la partie
!!!!!!!!!!!!!!!!!!!\n",j2);
StormEnnairo
Messages postés4Date d'inscriptionvendredi 24 février 2012StatutMembreDernière intervention27 février 2012 27 févr. 2012 à 19:31
ecoutr : primo, connaitre ton probleme exact ca aiderai ceux qui veullent te donner un coup de main. secondo essaye la prochaine de retirer les retours a la ligne qui,certe rend ton texte plus aeree, rend ton code encore plus long.