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

176
pendu.c
View File

@ -2,27 +2,21 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <stdbool.h>
#define MAX_WORDS 14
#define MAX_WORDS 17
#define MAX_TRIES 6
#define TIME_LIMIT 30 // Time limit for the game in seconds
const char *words[MAX_WORDS] = {
"programmation",
"ordinateur",
"langage",
"jeu",
"algorithmique",
"fontainebleau",
"koala",
"anticonstitutionnellement",
"code",
"canard",
"gyroscope",
"periclitation",
"susurrer",
"eclesiastique"
"programming", "computer", "language", "game", "algorithm",
"fontainebleau", "koala", "anticonstitutionally", "code",
"duck", "gyroscope", "endangerment", "whisper", "ecclesiastic",
"test", "yes", "no"
};
// Display hangman based on the number of incorrect tries
void display_hangman(int tries) {
switch (tries) {
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));
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 < MAX_WORDS; i++) {
if (strlen(words[i]) == target_length) {
char *selected_word = malloc((target_length + 1) * sizeof(char));
strcpy(selected_word, words[i]);
return selected_word;
}
}
char *constructed_word = malloc((target_length + 1) * sizeof(char));
constructed_word[0] = '\0';
int current_length = 0, attempts = 0;
while (current_length < target_length && attempts < 100) {
const char *random_word = words[rand() % MAX_WORDS];
int word_length = strlen(random_word);
if (current_length + word_length <= target_length) {
if (current_length > 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++) {
guessed[i] = '_';
if (word[i] == guess && guessed[i] == '_') {
guessed[i] = guess;
(*guessed_correctly)++;
(*score) += 10;
found = 1;
}
}
if (!found) {
(*tries)++;
(*score) -= 5;
}
}
// Log the game's statistics to a file
void add_to_logs(const char *word, int tries, int guessed_correctly, bool won, char guessed[], double time_spent, int score) {
FILE *log_file = fopen("hangman.log", "a");
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("\nMot à deviner : %s\n", guessed);
printf("\nWord to guess: %s\n", guessed);
display_hangman(tries);
char guess;
printf("Entrez une lettre : ");
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);
timeout = 1;
break;
}
printf("Enter a letter: ");
scanf(" %c", &guess);
int found = 0;
for (int i = 0; i < word_length; i++) {
if (word[i] == guess) {
if (guessed[i] == '_') {
guessed[i] = guess;
guessed_correctly++;
}
found = 1;
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");
}
}
if (!found) {
tries++;
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);
}
}
if (guessed_correctly == word_length) {
printf("Félicitations ! Vous avez deviné le mot : %s\n", word);
} else {
printf("Désolé, vous avez perdu. Le mot était : %s\n", word);
display_hangman(MAX_TRIES);
}
printf("Final score: %d\n", score);
free(word);
return 0;
}