forked from menault/TD3_DEV51_Qualite_Algo
Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
e0e0e25ef4 | |||
|
818c6db53d |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +0,0 @@
|
|||||||
*.log
|
|
||||||
*.out
|
|
||||||
.vscode
|
|
189
pendu.c
189
pendu.c
@@ -2,139 +2,106 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
#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",
|
|
||||||
"eclesiastique"};
|
|
||||||
|
|
||||||
void display_hangman(int tries)
|
// Fonction pour afficher le pendu selon le nombre d'essais incorrects
|
||||||
{
|
void display_hangman(int tries) {
|
||||||
switch (tries)
|
switch (tries) {
|
||||||
{
|
case 0: printf(" ----\n | |\n |\n |\n |\n |\n--------\n"); break;
|
||||||
case 0:
|
case 1: printf(" ----\n | |\n | O\n |\n |\n |\n--------\n"); break;
|
||||||
printf(" ----\n | |\n |\n |\n |\n |\n--------\n");
|
case 2: printf(" ----\n | |\n | O\n | |\n |\n |\n--------\n"); break;
|
||||||
break;
|
case 3: printf(" ----\n | |\n | O\n | /|\n |\n |\n--------\n"); break;
|
||||||
case 1:
|
case 4: printf(" ----\n | |\n | O\n | /|\\\n |\n |\n--------\n"); break;
|
||||||
printf(" ----\n | |\n | O\n |\n |\n |\n--------\n");
|
case 5: printf(" ----\n | |\n | O\n | /|\\\n | /\n |\n--------\n"); break;
|
||||||
break;
|
case 6: printf(" ----\n | |\n | O\n | /|\\\n | / \\\n |\n--------\n"); break;
|
||||||
case 2:
|
|
||||||
printf(" ----\n | |\n | O\n | |\n |\n |\n--------\n");
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
printf(" ----\n | |\n | O\n | /|\n |\n |\n--------\n");
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
printf(" ----\n | |\n | O\n | /|\\\n |\n |\n--------\n");
|
|
||||||
break;
|
|
||||||
case 5:
|
|
||||||
printf(" ----\n | |\n | O\n | /|\\\n | /\n |\n--------\n");
|
|
||||||
break;
|
|
||||||
case 6:
|
|
||||||
printf(" ----\n | |\n | O\n | /|\\\n | / \\\n |\n--------\n");
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the game's stats to a file
|
// Fonction pour mesurer le temps écoulé
|
||||||
void add_to_logs(const char *word, int tries, int guessed_correctly, bool won, char guessed[], double time_spent)
|
double measure_time_taken(char *guess) {
|
||||||
{
|
time_t start_time = time(NULL); // Démarrer le chronomètre
|
||||||
FILE *log_file = fopen("pendu.log", "a");
|
|
||||||
if (log_file == NULL)
|
|
||||||
{
|
|
||||||
printf("Error : Can't open file.\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
fprintf(log_file, "Log of : %s\n", __DATE__);
|
|
||||||
fprintf(log_file, "Guessed correctly : %d\n", guessed_correctly);
|
|
||||||
fprintf(log_file, "Total tries : %d\n", tries + guessed_correctly);
|
|
||||||
fprintf(log_file, "Guessed letters : %s\n", guessed);
|
|
||||||
fprintf(log_file, "Word : %s\n", word);
|
|
||||||
|
|
||||||
if (won)
|
|
||||||
{
|
|
||||||
fprintf(log_file, "Result : Win\n");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
fprintf(log_file, "Result : Lose\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
fprintf(log_file, "Time spent : %fsec\n\n", time_spent);
|
|
||||||
|
|
||||||
fclose(log_file);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
time_t start = time(NULL);
|
|
||||||
srand(time(NULL));
|
|
||||||
const char *word = words[rand() % MAX_WORDS];
|
|
||||||
int word_length = strlen(word);
|
|
||||||
char guessed[word_length];
|
|
||||||
int tries = 0;
|
|
||||||
int guessed_correctly = 0;
|
|
||||||
|
|
||||||
for (int i = 0; i < word_length; i++)
|
|
||||||
{
|
|
||||||
guessed[i] = '_';
|
|
||||||
}
|
|
||||||
guessed[word_length] = '\0';
|
|
||||||
|
|
||||||
while (tries < MAX_TRIES && guessed_correctly < word_length)
|
|
||||||
{
|
|
||||||
printf("\nMot à deviner : %s\n", guessed);
|
|
||||||
display_hangman(tries);
|
|
||||||
char guess;
|
|
||||||
printf("Entrez une lettre : ");
|
printf("Entrez une lettre : ");
|
||||||
scanf(" %c", &guess);
|
scanf(" %c", guess); // Demande d'entrée de la lettre à deviner
|
||||||
int found = 0;
|
time_t end_time = time(NULL); // Terminer le chronomètre
|
||||||
|
return difftime(end_time, start_time);
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < word_length; i++)
|
// 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) {
|
||||||
if (word[i] == guess)
|
int found = 0;
|
||||||
{
|
int word_length = strlen(word);
|
||||||
if (guessed[i] == '_')
|
|
||||||
{
|
for (int i = 0; i < word_length; i++) {
|
||||||
|
if (word[i] == guess) {
|
||||||
|
if (guessed[i] == '_') {
|
||||||
guessed[i] = guess;
|
guessed[i] = guess;
|
||||||
guessed_correctly++;
|
(*guessed_correctly)++;
|
||||||
|
(*score) += 10; // Bonus de points pour chaque lettre correcte
|
||||||
}
|
}
|
||||||
found = 1;
|
found = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!found)
|
if (!found) {
|
||||||
{
|
(*tries)++;
|
||||||
tries++;
|
(*score) -= 5; // Pénalité pour une lettre incorrecte
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
srand(time(NULL));
|
||||||
|
const char *word = words[rand() % MAX_WORDS];
|
||||||
|
int word_length = strlen(word);
|
||||||
|
char guessed[word_length + 1];
|
||||||
|
int tries = 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++) {
|
||||||
|
guessed[i] = '_';
|
||||||
|
}
|
||||||
|
guessed[word_length] = '\0';
|
||||||
|
|
||||||
|
while (tries < MAX_TRIES && guessed_correctly < word_length) {
|
||||||
|
printf("\nMot à deviner : %s\n", guessed);
|
||||||
|
display_hangman(tries);
|
||||||
|
|
||||||
|
// Appel de la fonction pour mesurer le temps
|
||||||
|
double time_taken = measure_time_taken(&guess);
|
||||||
|
|
||||||
|
// Vérification du temps écoulé
|
||||||
|
if (time_taken > TIME_LIMIT) {
|
||||||
|
printf("Temps écoulé ! Vous avez mis plus de %d secondes.\n", TIME_LIMIT);
|
||||||
|
printf("Désolé, vous avez perdu. Le mot était : %s\n", word);
|
||||||
|
display_hangman(MAX_TRIES);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
time_t end = time(NULL);
|
// Appel de la fonction pour traiter la lettre devinée
|
||||||
double time_spent = difftime(end, start);
|
process_guess(word, guessed, guess, &guessed_correctly, &score, &tries);
|
||||||
if (guessed_correctly == word_length)
|
|
||||||
{
|
printf("Temps écoulé pour cette tentative : %.2f secondes\n", time_taken);
|
||||||
printf("Félicitations ! Vous avez deviné le mot : %s\n", word);
|
|
||||||
add_to_logs(word, tries, guessed_correctly, 1, guessed, time_spent);
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
if (guessed_correctly == word_length) {
|
||||||
|
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 {
|
||||||
printf("Désolé, vous avez perdu. Le mot était : %s\n", word);
|
printf("Désolé, vous avez perdu. Le mot était : %s\n", word);
|
||||||
add_to_logs(word, tries, guessed_correctly, 0, guessed, time_spent);
|
|
||||||
display_hangman(MAX_TRIES);
|
display_hangman(MAX_TRIES);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user