TD3_DEV51_Qualite_Algo/pendu.c

188 lines
6.6 KiB
C
Raw Normal View History

2024-10-25 18:18:43 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <stdbool.h>
#define MAX_WORDS 17
#define MAX_TRIES 6
#define TIME_LIMIT 30 // Time limit for the game in seconds
const char *words[MAX_WORDS] = {
"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) {
2024-10-25 19:30:03 +02:00
const char *stages[] = {
" ----\n | |\n |\n |\n |\n |\n--------\n",
" ----\n | |\n | O\n |\n |\n |\n--------\n",
" ----\n | |\n | O\n | |\n |\n |\n--------\n",
" ----\n | |\n | O\n | /|\n |\n |\n--------\n",
" ----\n | |\n | O\n | /|\\\n |\n |\n--------\n",
" ----\n | |\n | O\n | /|\\\n | /\n |\n--------\n",
" ----\n | |\n | O\n | /|\\\n | / \\\n |\n--------\n"
};
printf("%s", stages[tries]);
2024-10-25 18:18:43 +02:00
}
2024-10-25 19:30:03 +02:00
// Choose or construct a word of the specified length
char *choose_or_construct_word(int target_length) {
2024-10-25 18:18:43 +02:00
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;
}
}
2024-10-25 19:30:03 +02:00
return construct_word(target_length);
}
// Helper function to construct a word if none of the exact length is found
char *construct_word(int target_length) {
2024-10-25 18:18:43 +02:00
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++;
}
2024-10-25 19:30:03 +02:00
return attempts < 100 ? constructed_word : NULL;
2024-10-25 18:18:43 +02:00
}
// 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++) {
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);
}
2024-10-25 19:30:03 +02:00
// Initialize guessed word state with underscores
void initialize_guessed_word(char *guessed, const char *word) {
int word_length = strlen(word);
for (int i = 0; i < word_length; i++) {
guessed[i] = (word[i] == ' ') ? ' ' : '_';
}
guessed[word_length] = '\0';
}
2024-10-25 18:18:43 +02:00
// 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);
2024-10-25 19:30:03 +02:00
char *word = choose_or_construct_word(length_choice);
2024-10-25 18:18:43 +02:00
int word_length = strlen(word);
char guessed[word_length + 1];
int tries = 0, guessed_correctly = 0, score = 0, timeout = 0;
2024-10-25 19:30:03 +02:00
initialize_guessed_word(guessed, word);
2024-10-25 18:18:43 +02:00
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);
2024-10-25 19:30:03 +02:00
if (check_timeout(game_start_time, TIME_LIMIT)) {
2024-10-25 18:18:43 +02:00
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;
}
2024-10-25 19:30:03 +02:00
play_turn(word, guessed, &guessed_correctly, &score, &tries);
2024-10-25 18:18:43 +02:00
}
double time_spent = difftime(time(NULL), game_start_time);
2024-10-25 19:30:03 +02:00
display_game_result(word, guessed, guessed_correctly, tries, timeout, score, time_spent);
free(word);
return 0;
}
// Check if the game has exceeded the time limit
bool check_timeout(time_t start_time, int limit) {
return difftime(time(NULL), start_time) > limit;
}
// Play a single turn by asking the player for a guess and processing it
void play_turn(const char *word, char *guessed, int *guessed_correctly, int *score, int *tries) {
char guess;
printf("Enter a letter: ");
scanf(" %c", &guess);
if (!isalpha(guess)) {
printf("Please enter a valid alphabetical letter.\n");
return;
}
process_guess(word, guessed, guess, guessed_correctly, score, tries);
}
2024-10-25 18:18:43 +02:00
2024-10-25 19:30:03 +02:00
// Display the final game result
void display_game_result(const char *word, char *guessed, int guessed_correctly, int tries, int timeout, int score, double time_spent) {
2024-10-25 18:18:43 +02:00
if (!timeout) {
2024-10-25 19:30:03 +02:00
if (guessed_correctly == strlen(word)) {
2024-10-25 18:18:43 +02:00
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);
}