Probleme puissance 4 en C
omegas27
-
omegas27 -
omegas27 -
Bonjour, je rencontre un problème dans mon programme au niveau de la fonction Jouer dans la boucle while
while((choix!=0)||(choix!=1)||(choix!=2)||(choix!=3)||(choix!=4)||(choix!=5)||(choix!=6)||(choix!='s')){
printf("La commande n'est pas bonne. Recommencez\t=>");
scanf("%c%*c", &choix);
}
voici le programme.
#include<stdio.h>
#define N 15
void Credits (void);
int Jouer (char grille[N][N], char *nom, int val);
int Selection (void);
void Affiche (char grille[N][N]);
int main (void){
char grille[N][N];
char joueur1[20], joueur2[20], ordinateur[6]={"Omega"};
int choix, val, resultat=1;
choix=Selection();
if (choix==1){
Affiche (grille);
printf("\n");
}
if (choix==2){
printf("Joueur 1, quel est votre prenom?\n");
scanf("%s", joueur1);
printf("Joueur 2, quel est votre prenom?\n");
scanf("%s", joueur2);
printf("\n");
Affiche (grille);
printf("\n");
while(resultat!=0){
val=1;
Jouer (grille, joueur1, val);
printf("\n");
Affiche (grille);
printf("\n");
val=2;
Jouer (grille, joueur2, val);
printf("\n");
Affiche (grille);
printf("\n");
}
}
if (choix==3){
printf("\n");
}
if (choix==4){
Credits();
printf("\n");
}
}
int Jouer (char grille[N][N], char *nom, int val){
int j, resultat=1, colonne;
char choix=0;
printf("A vous de jouer : %s\n", nom);
scanf("%c%*c", &choix);
while(resultat!=0){
while((choix!=0)||(choix!=1)||(choix!=2)||(choix!=3)||(choix!=4)||(choix!=5)||(choix!=6)||(choix!='s')){
printf("La commande n'est pas bonne. Recommencez\t=>");
scanf("%c%*c", &choix);
}
if (choix=='s'){
resultat=0;
break;
}
else colonne=choix;
if (colonne==0)
colonne++;
else if (colonne!=0)
colonne+=colonne+1;
while(grille[colonne][0]!=' '){
printf("La colonne est pleine. Choisissez en une autre.\t=>");
scanf("%d", &colonne);
}
for(j=6;j>=0;j--){
if (grille[colonne][j]==' '){
if(val==1){
grille[colonne][j]=='X';
break;
}
else if(val==2){
grille[colonne][j]=='O';
break;
}
}
}
}
return resultat;
}
void Affiche (char grille[N][N]){
int i, j;
printf("Selectionnez une valeur entre 0 et 6 pour jouer ou s pour sauvegarder.\n");
printf(" | 0 | 1 | 2 | 3 | 4 | 5 | 6 | \n\n");
for (i=0;i<6;i++){
for (j=0;j<N;j+=2){
grille[i][j]='|';
}
}
for (i=0;i<6;i++){
for (j=1;j<N;j+=2){
grille[i][j]=' ';
}
}
for (i=0;i<6;i++){
for (j=0;j<N;j++){
printf(" %c " , grille[i][j]);
if (j==N-1){
printf("\n");
}
}
}
}
int Selection (void){
int rep;
printf("\n\n\n\n\t\t\t\t\t\t\t\tJEU PUISSANCE 4\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("\n\n\t\t\t\t1-Partie simple\n\n\t\t\t\t2-Partie deux joueurs\n\n\t\t\t\t3-Charger une partie\n\n\t\t\t\t4-Crédits\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
scanf("%d", &rep);
return (rep);
}
void Credits (void){
printf("\n\n\n\n\n\t\t\t\t\t\t\t\t\t\tPuissance 4\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t Developpé dans le cadre du projet de C à l'IUT de Caen\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\t\t\tPar Quentin Bisson\n\n\n\n\n\n\n\n\n");
}
Merci pour vos reponses.
while((choix!=0)||(choix!=1)||(choix!=2)||(choix!=3)||(choix!=4)||(choix!=5)||(choix!=6)||(choix!='s')){
printf("La commande n'est pas bonne. Recommencez\t=>");
scanf("%c%*c", &choix);
}
voici le programme.
#include<stdio.h>
#define N 15
void Credits (void);
int Jouer (char grille[N][N], char *nom, int val);
int Selection (void);
void Affiche (char grille[N][N]);
int main (void){
char grille[N][N];
char joueur1[20], joueur2[20], ordinateur[6]={"Omega"};
int choix, val, resultat=1;
choix=Selection();
if (choix==1){
Affiche (grille);
printf("\n");
}
if (choix==2){
printf("Joueur 1, quel est votre prenom?\n");
scanf("%s", joueur1);
printf("Joueur 2, quel est votre prenom?\n");
scanf("%s", joueur2);
printf("\n");
Affiche (grille);
printf("\n");
while(resultat!=0){
val=1;
Jouer (grille, joueur1, val);
printf("\n");
Affiche (grille);
printf("\n");
val=2;
Jouer (grille, joueur2, val);
printf("\n");
Affiche (grille);
printf("\n");
}
}
if (choix==3){
printf("\n");
}
if (choix==4){
Credits();
printf("\n");
}
}
int Jouer (char grille[N][N], char *nom, int val){
int j, resultat=1, colonne;
char choix=0;
printf("A vous de jouer : %s\n", nom);
scanf("%c%*c", &choix);
while(resultat!=0){
while((choix!=0)||(choix!=1)||(choix!=2)||(choix!=3)||(choix!=4)||(choix!=5)||(choix!=6)||(choix!='s')){
printf("La commande n'est pas bonne. Recommencez\t=>");
scanf("%c%*c", &choix);
}
if (choix=='s'){
resultat=0;
break;
}
else colonne=choix;
if (colonne==0)
colonne++;
else if (colonne!=0)
colonne+=colonne+1;
while(grille[colonne][0]!=' '){
printf("La colonne est pleine. Choisissez en une autre.\t=>");
scanf("%d", &colonne);
}
for(j=6;j>=0;j--){
if (grille[colonne][j]==' '){
if(val==1){
grille[colonne][j]=='X';
break;
}
else if(val==2){
grille[colonne][j]=='O';
break;
}
}
}
}
return resultat;
}
void Affiche (char grille[N][N]){
int i, j;
printf("Selectionnez une valeur entre 0 et 6 pour jouer ou s pour sauvegarder.\n");
printf(" | 0 | 1 | 2 | 3 | 4 | 5 | 6 | \n\n");
for (i=0;i<6;i++){
for (j=0;j<N;j+=2){
grille[i][j]='|';
}
}
for (i=0;i<6;i++){
for (j=1;j<N;j+=2){
grille[i][j]=' ';
}
}
for (i=0;i<6;i++){
for (j=0;j<N;j++){
printf(" %c " , grille[i][j]);
if (j==N-1){
printf("\n");
}
}
}
}
int Selection (void){
int rep;
printf("\n\n\n\n\t\t\t\t\t\t\t\tJEU PUISSANCE 4\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("\n\n\t\t\t\t1-Partie simple\n\n\t\t\t\t2-Partie deux joueurs\n\n\t\t\t\t3-Charger une partie\n\n\t\t\t\t4-Crédits\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
scanf("%d", &rep);
return (rep);
}
void Credits (void){
printf("\n\n\n\n\n\t\t\t\t\t\t\t\t\t\tPuissance 4\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t Developpé dans le cadre du projet de C à l'IUT de Caen\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\t\t\tPar Quentin Bisson\n\n\n\n\n\n\n\n\n");
}
Merci pour vos reponses.
A voir également:
- Probleme puissance 4 en C
- Test puissance pc - Guide
- Code gta 4 ps4 - Guide
- Puissance en c - Forum Programmation
- Puissance signal wifi - Guide
- A4 divisé en 4 - Forum Graphisme
3 réponses
Ouais, quand tu saisis un caractère, par exemple zéro: la valeur que tu retrouves dans 'choix' n'est pas le nombre zéro mais le caractère zéro soit 0x30 en hexa ou 0b00110000 en binaire ou 48 en décimal.
D'autre part on ne fait pas des tests du type:
(choix!=0)||(choix!=1)||(choix!=2)||(choix!=3)||(choix!=4)||(choix!=5)||(choix!=6)||(choix!='s')
mais plus lisiblement (et plus simplement):
(choix < '0') || ((choix > '6')) && (choix != 's'))
Bonne réflexion.
D'autre part on ne fait pas des tests du type:
(choix!=0)||(choix!=1)||(choix!=2)||(choix!=3)||(choix!=4)||(choix!=5)||(choix!=6)||(choix!='s')
mais plus lisiblement (et plus simplement):
(choix < '0') || ((choix > '6')) && (choix != 's'))
Bonne réflexion.
D'autre part:
while(resultat != 0) { if (choix == 's') { resultat = 0; break; } else colonne = choix; if (colonne == 0) colonne++; else if (colonne !=0 ) // Si ce n'est pas nul, c'est donc non nul ! colonne+=colonne+1; // Veux-tu vraiment faire: colonne = 2*colonne + 1 ? ... }peut se simplifier en:
while(resultat) { if (choix == 's') { resultat = 0; break; } colonne++; ... }Bonne continuation.
for (j=6; j>=0; j--) { if (grille[colonne][j] == ' ') { if (val == 1) { grille[colonne][j] == 'X'; // Faux, mettre un simple '=' break; } else if (val == 2) { grille[colonne][j] == 'O'; break; } } }Ne serait-il pas plus judicieux d'utiliser, pour 'val', les valeurs 'X' et 'O'; dans ce cas ton code deviendrait:
for (j=6; j>=0; j--) { if (grille[colonne][j] == ' ') { grille[colonne][j] = val; break; } }Bonne continuation.