Et d’ailleurs, mon code était faux (enfin, si on suit la regle du pendu)
[code]// pendu.c
//
// Copyright 2010 veronique veronique@super-albert
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301, USA.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void demandeMotSecret(char motSecret[]);
int jeu(char motSecret[], char motAffiche[]);
char lireCaractere();
void viderBuffer();
void initialise(char motSecret[], char** motAffiche);
int main(int argc, char** argv)
{
int resultat = 0, compteur = 10, cont = 1;
char motSecret[100] = {0};
char *motAffiche = NULL;
printf("Bienvenue dans le pendu \n \n"); //message d'accueil
while (cont == 1) //boucle principale
{
demandeMotSecret(motSecret); //fonction pour définir le mot secret
initialise(motSecret, &motAffiche);
while ( (strcmp(motAffiche, motSecret)) != 0 && compteur >= 0)
{
printf("Il vous reste %d coups à jouer\n", compteur),
resultat = jeu(motSecret, motAffiche); // Cette fonction renvoie 1 si gagné, 0 sinon
if (resultat == 0)
{
compteur = compteur - 1;
}
}
if ( (strcmp(motAffiche, motSecret)) == 0 )
{
printf("Bravo, tu as gagné!\n\n");
}
else
{
printf("Bouh, tu as perdu! :P \n\n");
}
//là, le jeu est fini, on propose une autre partie
printf("Voulez-vous rejouer? \n");
printf("1. Oui\n2. Non\n");
scanf("%d", &cont);
printf("%d", cont);
}
free(motAffiche);
return EXIT_SUCCESS;
}
char lireCaractere()
{
char caractere = 0;
caractere = getchar(); // On lit le premier caractère
caractere = toupper(caractere); // On met la lettre en majuscule si elle ne l'est pas déjà
// On lit les autres caractères mémorisés un à un jusqu'à l'\n (pour les effacer)
while (getchar() != '\n') ;
return caractere; // On retourne le premier caractère qu'on a lu
}
void viderBuffer()
{
int c = 0;
while (c != ‘\n’ && c != EOF)
{
c = getchar();
}
}
void demandeMotSecret(char motSecret[])
{
char *entree = NULL;
int i = 0;
printf("Donnez le mot secret\n");
fgets(motSecret, 100, stdin);
entree = strchr(motSecret, '\n');
if (entree != NULL) // on remplace le retour à la ligne
{
*entree = '\0';
}
//viderBuffer();
for (i = 0; i < (strlen(motSecret)); i++)
{
motSecret[i] = toupper(motSecret[i]);
}
}
int jeu(char motSecret[], char motAffiche[])
{
char lettre = 0;
int j = 0, score = 0;
printf(“Quel est le mot secret? %s\n”, motAffiche);
printf("Proposez une lettre : ");
lettre = lireCaractere();
for ( j = 0; j < (strlen(motSecret)); j++)
{
if ( motSecret[j] == lettre )
{
motAffiche[j] = lettre;
score = 1;
}
}
return score;
}
void initialise(char motSecret[], char** motAffiche)
{
int i = 0, longueur = 0;
longueur = strlen(motSecret);
*motAffiche=malloc((longueur + 1) * sizeof(char));
for (i = 0; i < longueur ; i++)
{
(*motAffiche)[i] = '*' ;
}
}
[/code]