TD3_DEV51_daniel_bouzon_lawson/pendu.c
2024-10-25 19:30:03 +02:00

188 lines
6.6 KiB
C

#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) {
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]);
}
// Choose or construct a word of the specified length
char *choose_or_construct_word(int target_length) {
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;
}
}
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) {
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++;
}
return attempts < 100 ? constructed_word : NULL;
}
// 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);
}
// 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';
}
// 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 = choose_or_construct_word(length_choice);
int word_length = strlen(word);
char guessed[word_length + 1];
int tries = 0, guessed_correctly = 0, score = 0, timeout = 0;
initialize_guessed_word(guessed, word);
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 (check_timeout(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;
}
play_turn(word, guessed, &guessed_correctly, &score, &tries);
}
double time_spent = difftime(time(NULL), game_start_time);
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);
}
// 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) {
if (!timeout) {
if (guessed_correctly == strlen(word)) {
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);
}