This commit is contained in:
Nathan BOUZON 2024-10-25 18:18:43 +02:00
parent 416e22c986
commit 67b7016376

174
pendu.c
View File

@ -2,27 +2,21 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <ctype.h>
#include <stdbool.h>
#define MAX_WORDS 14 #define MAX_WORDS 17
#define MAX_TRIES 6 #define MAX_TRIES 6
#define TIME_LIMIT 30 // Time limit for the game in seconds
const char *words[MAX_WORDS] = { const char *words[MAX_WORDS] = {
"programmation", "programming", "computer", "language", "game", "algorithm",
"ordinateur", "fontainebleau", "koala", "anticonstitutionally", "code",
"langage", "duck", "gyroscope", "endangerment", "whisper", "ecclesiastic",
"jeu", "test", "yes", "no"
"algorithmique",
"fontainebleau",
"koala",
"anticonstitutionnellement",
"code",
"canard",
"gyroscope",
"periclitation",
"susurrer",
"eclesiastique"
}; };
// Display hangman based on the number of incorrect tries
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;
@ -35,48 +29,144 @@ void display_hangman(int tries) {
} }
} }
int main() { // Generate a word of the specified length or construct a word if not found
char *generate_word(int target_length) {
srand(time(NULL)); srand(time(NULL));
const char *word = words[rand() % MAX_WORDS]; for (int i = 0; i < MAX_WORDS; i++) {
int word_length = strlen(word); if (strlen(words[i]) == target_length) {
char guessed[word_length]; char *selected_word = malloc((target_length + 1) * sizeof(char));
int tries = 0; strcpy(selected_word, words[i]);
int guessed_correctly = 0; return selected_word;
for (int i = 0; i < word_length; i++) {
guessed[i] = '_';
} }
guessed[word_length] = '\0'; }
char *constructed_word = malloc((target_length + 1) * sizeof(char));
constructed_word[0] = '\0';
int current_length = 0, attempts = 0;
while (tries < MAX_TRIES && guessed_correctly < word_length) { while (current_length < target_length && attempts < 100) {
printf("\nMot à deviner : %s\n", guessed); const char *random_word = words[rand() % MAX_WORDS];
display_hangman(tries); int word_length = strlen(random_word);
char guess;
printf("Entrez une lettre : "); if (current_length + word_length <= target_length) {
scanf(" %c", &guess); if (current_length > 0) {
int found = 0; strcat(constructed_word, " ");
current_length++;
}
strcat(constructed_word, random_word);
current_length += word_length;
}
attempts++;
}
if (attempts >= 100) {
printf("Error: Unable to create a word of specified length.\n");
free(constructed_word);
exit(1);
}
return constructed_word;
}
// Process guessed letter and update game status
void process_guess(const char *word, char *guessed, char guess, int *guessed_correctly, int *score, int *tries) {
int found = 0, word_length = strlen(word);
for (int i = 0; i < word_length; i++) { for (int i = 0; i < word_length; i++) {
if (word[i] == guess) { if (word[i] == guess && guessed[i] == '_') {
if (guessed[i] == '_') {
guessed[i] = guess; guessed[i] = guess;
guessed_correctly++; (*guessed_correctly)++;
} (*score) += 10;
found = 1; found = 1;
} }
} }
if (!found) { if (!found) {
tries++; (*tries)++;
} (*score) -= 5;
} }
}
if (guessed_correctly == word_length) { // Log the game's statistics to a file
printf("Félicitations ! Vous avez deviné le mot : %s\n", word); void add_to_logs(const char *word, int tries, int guessed_correctly, bool won, char guessed[], double time_spent, int score) {
} else { FILE *log_file = fopen("hangman.log", "a");
printf("Désolé, vous avez perdu. Le mot était : %s\n", word); if (log_file == NULL) {
printf("Error: Cannot open file.\n");
return;
}
fprintf(log_file, "Log date: %s\n", __DATE__);
fprintf(log_file, "Correct letters guessed: %d\n", guessed_correctly);
fprintf(log_file, "Total tries: %d\n", tries + guessed_correctly);
fprintf(log_file, "Word: %s\n", word);
fprintf(log_file, "Guessed letters: %s\n", guessed);
fprintf(log_file, "Result: %s\n", won ? "Win" : "Lose");
fprintf(log_file, "Time elapsed: %.2f sec\n", time_spent);
fprintf(log_file, "Final score: %d\n\n", score);
fclose(log_file);
}
// Main function for hangman game
int main() {
srand(time(NULL));
int length_choice;
printf("Enter the difficulty (Maximum word length to guess): ");
scanf("%d", &length_choice);
char *word = generate_word(length_choice);
int word_length = strlen(word);
char guessed[word_length + 1];
int tries = 0, guessed_correctly = 0, score = 0, timeout = 0;
char guess;
for (int i = 0; i < word_length; i++) {
guessed[i] = (word[i] == ' ') ? ' ' : '_';
}
guessed[word_length] = '\0';
time_t game_start_time = time(NULL);
while (tries < MAX_TRIES && guessed_correctly < word_length) {
printf("\nWord to guess: %s\n", guessed);
display_hangman(tries);
if (difftime(time(NULL), game_start_time) > TIME_LIMIT) {
printf("Time's up! You exceeded %d seconds.\n", TIME_LIMIT);
printf("Sorry, you lost. The word was: %s\n", word);
display_hangman(MAX_TRIES); display_hangman(MAX_TRIES);
timeout = 1;
break;
} }
printf("Enter a letter: ");
scanf(" %c", &guess);
if (!isalpha(guess)) {
printf("Please enter a valid alphabetical letter.\n");
continue;
}
process_guess(word, guessed, guess, &guessed_correctly, &score, &tries);
}
double time_spent = difftime(time(NULL), game_start_time);
if (!timeout) {
if (guessed_correctly == word_length) {
printf("Congratulations! You've guessed the word: %s\n", word);
if (tries < 3) {
score += 20;
printf("Well done! Bonus points for guessing the word in less than 3 tries.\n");
}
add_to_logs(word, tries, guessed_correctly, true, guessed, time_spent, score);
} else {
printf("Sorry, you lost. The word was: %s\n", word);
display_hangman(MAX_TRIES);
add_to_logs(word, tries, guessed_correctly, false, guessed, time_spent, score);
}
}
printf("Final score: %d\n", score);
free(word);
return 0; return 0;
} }