forked from menault/TD3_DEV51_Qualite_Algo
Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
e0e0e25ef4 | |||
|
818c6db53d |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +0,0 @@
|
|||||||
*.out
|
|
106
pendu.c
106
pendu.c
@@ -5,28 +5,15 @@
|
|||||||
|
|
||||||
#define MAX_WORDS 14
|
#define MAX_WORDS 14
|
||||||
#define MAX_TRIES 6
|
#define MAX_TRIES 6
|
||||||
|
#define TIME_LIMIT 10 // Limite de temps en secondes
|
||||||
|
|
||||||
const char *words[MAX_WORDS] = {
|
const char *words[MAX_WORDS] = {
|
||||||
"programmation",
|
"programmation", "ordinateur", "langage", "jeu", "algorithmique",
|
||||||
"ordinateur",
|
"fontainebleau", "koala", "anticonstitutionnellement", "code",
|
||||||
"langage",
|
"canard", "gyroscope", "periclitation", "susurrer", "eclesiastique"
|
||||||
"jeu",
|
|
||||||
"algorithmique",
|
|
||||||
"fontainebleau",
|
|
||||||
"koala",
|
|
||||||
"anticonstitutionnellement",
|
|
||||||
"code",
|
|
||||||
"canard",
|
|
||||||
"gyroscope",
|
|
||||||
"periclitation",
|
|
||||||
"susurrer",
|
|
||||||
"ecclesiastique",
|
|
||||||
"test",
|
|
||||||
"oui",
|
|
||||||
"non"
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Fonction pour afficher le pendu selon le nombre d'essais incorrects
|
||||||
void display_hangman(int tries) {
|
void display_hangman(int tries) {
|
||||||
switch (tries) {
|
switch (tries) {
|
||||||
case 0: printf(" ----\n | |\n |\n |\n |\n |\n--------\n"); break;
|
case 0: printf(" ----\n | |\n |\n |\n |\n |\n--------\n"); break;
|
||||||
@@ -38,49 +25,51 @@ void display_hangman(int tries) {
|
|||||||
case 6: printf(" ----\n | |\n | O\n | /|\\\n | / \\\n |\n--------\n"); break;
|
case 6: printf(" ----\n | |\n | O\n | /|\\\n | / \\\n |\n--------\n"); break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Fonction pour choisir des mots jusqu'à atteindre la longueur demandée
|
|
||||||
char *hidden_word(int length_choice) {
|
|
||||||
srand(time(NULL));
|
|
||||||
|
|
||||||
char *total_word = malloc((length_choice + 1) * sizeof(char));
|
// Fonction pour mesurer le temps écoulé
|
||||||
total_word[0] = '\0';
|
double measure_time_taken(char *guess) {
|
||||||
|
time_t start_time = time(NULL); // Démarrer le chronomètre
|
||||||
|
printf("Entrez une lettre : ");
|
||||||
|
scanf(" %c", guess); // Demande d'entrée de la lettre à deviner
|
||||||
|
time_t end_time = time(NULL); // Terminer le chronomètre
|
||||||
|
return difftime(end_time, start_time);
|
||||||
|
}
|
||||||
|
|
||||||
int total_length = 0;
|
// Fonction pour traiter la lettre devinée et mettre à jour l'état du jeu
|
||||||
|
void process_guess(const char *word, char *guessed, char guess, int *guessed_correctly, int *score, int *tries) {
|
||||||
|
int found = 0;
|
||||||
while (total_length < length_choice) {
|
|
||||||
const char *word = words[rand() % MAX_WORDS];
|
|
||||||
int word_length = strlen(word);
|
int word_length = strlen(word);
|
||||||
|
|
||||||
if (total_length + word_length <= length_choice) {
|
for (int i = 0; i < word_length; i++) {
|
||||||
if (total_length > 0) {
|
if (word[i] == guess) {
|
||||||
strcat(total_word, " ");
|
if (guessed[i] == '_') {
|
||||||
total_length++;
|
guessed[i] = guess;
|
||||||
|
(*guessed_correctly)++;
|
||||||
|
(*score) += 10; // Bonus de points pour chaque lettre correcte
|
||||||
}
|
}
|
||||||
|
found = 1;
|
||||||
strcat(total_word, word);
|
|
||||||
total_length += word_length;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return total_word;
|
if (!found) {
|
||||||
|
(*tries)++;
|
||||||
|
(*score) -= 5; // Pénalité pour une lettre incorrecte
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int length_choice;
|
srand(time(NULL));
|
||||||
printf("Entrez la difficulté (Taille maximum du mot à deviner) : ");
|
const char *word = words[rand() % MAX_WORDS];
|
||||||
scanf("%d", &length_choice);
|
|
||||||
|
|
||||||
char *word = hidden_word(length_choice);
|
|
||||||
int word_length = strlen(word);
|
int word_length = strlen(word);
|
||||||
|
|
||||||
char guessed[word_length + 1];
|
char guessed[word_length + 1];
|
||||||
int tries = 0;
|
int tries = 0;
|
||||||
int guessed_correctly = 0;
|
int guessed_correctly = 0;
|
||||||
|
int score = 100; // Score initial
|
||||||
|
char guess; // Variable pour la lettre entrée
|
||||||
|
|
||||||
|
// Initialiser le mot deviné avec des underscores
|
||||||
for (int i = 0; i < word_length; i++) {
|
for (int i = 0; i < word_length; i++) {
|
||||||
guessed[i] = (word[i] == ' ') ? ' ' : '_';
|
guessed[i] = '_';
|
||||||
}
|
}
|
||||||
guessed[word_length] = '\0';
|
guessed[word_length] = '\0';
|
||||||
|
|
||||||
@@ -88,32 +77,33 @@ int main() {
|
|||||||
printf("\nMot à deviner : %s\n", guessed);
|
printf("\nMot à deviner : %s\n", guessed);
|
||||||
display_hangman(tries);
|
display_hangman(tries);
|
||||||
|
|
||||||
char guess;
|
// Appel de la fonction pour mesurer le temps
|
||||||
printf("Entrez une lettre : ");
|
double time_taken = measure_time_taken(&guess);
|
||||||
scanf(" %c", &guess);
|
|
||||||
|
|
||||||
int found = 0;
|
// Vérification du temps écoulé
|
||||||
for (int i = 0; i < word_length; i++) {
|
if (time_taken > TIME_LIMIT) {
|
||||||
if (word[i] == guess && guessed[i] == '_') {
|
printf("Temps écoulé ! Vous avez mis plus de %d secondes.\n", TIME_LIMIT);
|
||||||
guessed[i] = guess;
|
printf("Désolé, vous avez perdu. Le mot était : %s\n", word);
|
||||||
guessed_correctly++;
|
display_hangman(MAX_TRIES);
|
||||||
found = 1;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Appel de la fonction pour traiter la lettre devinée
|
||||||
|
process_guess(word, guessed, guess, &guessed_correctly, &score, &tries);
|
||||||
|
|
||||||
if (!found) {
|
printf("Temps écoulé pour cette tentative : %.2f secondes\n", time_taken);
|
||||||
tries++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (guessed_correctly == word_length) {
|
if (guessed_correctly == word_length) {
|
||||||
printf("Félicitations ! Vous avez deviné le mot : %s\n", word);
|
printf("Félicitations ! Vous avez deviné le mot : %s\n", word);
|
||||||
|
if (tries < 3) {
|
||||||
|
score += 20; // Bonus si deviné en moins de 3 essais
|
||||||
|
printf("Bravo ! Bonus de points pour avoir deviné le mot en moins de 3 essais.\n");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
printf("Désolé, vous avez perdu. Le mot était : %s\n", word);
|
printf("Désolé, vous avez perdu. Le mot était : %s\n", word);
|
||||||
display_hangman(MAX_TRIES);
|
display_hangman(MAX_TRIES);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(word);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Reference in New Issue
Block a user