Pendu
This commit is contained in:
parent
416e22c986
commit
67b7016376
254
pendu.c
254
pendu.c
@ -1,82 +1,172 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <ctype.h>
|
||||||
#define MAX_WORDS 14
|
#include <stdbool.h>
|
||||||
#define MAX_TRIES 6
|
|
||||||
|
#define MAX_WORDS 17
|
||||||
const char *words[MAX_WORDS] = {
|
#define MAX_TRIES 6
|
||||||
"programmation",
|
#define TIME_LIMIT 30 // Time limit for the game in seconds
|
||||||
"ordinateur",
|
|
||||||
"langage",
|
const char *words[MAX_WORDS] = {
|
||||||
"jeu",
|
"programming", "computer", "language", "game", "algorithm",
|
||||||
"algorithmique",
|
"fontainebleau", "koala", "anticonstitutionally", "code",
|
||||||
"fontainebleau",
|
"duck", "gyroscope", "endangerment", "whisper", "ecclesiastic",
|
||||||
"koala",
|
"test", "yes", "no"
|
||||||
"anticonstitutionnellement",
|
};
|
||||||
"code",
|
|
||||||
"canard",
|
// Display hangman based on the number of incorrect tries
|
||||||
"gyroscope",
|
void display_hangman(int tries) {
|
||||||
"periclitation",
|
switch (tries) {
|
||||||
"susurrer",
|
case 0: printf(" ----\n | |\n |\n |\n |\n |\n--------\n"); break;
|
||||||
"eclesiastique"
|
case 1: 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;
|
||||||
void display_hangman(int tries) {
|
case 4: printf(" ----\n | |\n | O\n | /|\\\n |\n |\n--------\n"); break;
|
||||||
switch (tries) {
|
case 5: printf(" ----\n | |\n | O\n | /|\\\n | /\n |\n--------\n"); break;
|
||||||
case 0: printf(" ----\n | |\n |\n |\n |\n |\n--------\n"); break;
|
case 6: printf(" ----\n | |\n | O\n | /|\\\n | / \\\n |\n--------\n"); break;
|
||||||
case 1: 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;
|
// Generate a word of the specified length or construct a word if not found
|
||||||
case 5: printf(" ----\n | |\n | O\n | /|\\\n | /\n |\n--------\n"); break;
|
char *generate_word(int target_length) {
|
||||||
case 6: printf(" ----\n | |\n | O\n | /|\\\n | / \\\n |\n--------\n"); break;
|
srand(time(NULL));
|
||||||
}
|
for (int i = 0; i < MAX_WORDS; i++) {
|
||||||
}
|
if (strlen(words[i]) == target_length) {
|
||||||
|
char *selected_word = malloc((target_length + 1) * sizeof(char));
|
||||||
int main() {
|
strcpy(selected_word, words[i]);
|
||||||
srand(time(NULL));
|
return selected_word;
|
||||||
const char *word = words[rand() % MAX_WORDS];
|
}
|
||||||
int word_length = strlen(word);
|
}
|
||||||
char guessed[word_length];
|
char *constructed_word = malloc((target_length + 1) * sizeof(char));
|
||||||
int tries = 0;
|
constructed_word[0] = '\0';
|
||||||
int guessed_correctly = 0;
|
int current_length = 0, attempts = 0;
|
||||||
|
|
||||||
for (int i = 0; i < word_length; i++) {
|
while (current_length < target_length && attempts < 100) {
|
||||||
guessed[i] = '_';
|
const char *random_word = words[rand() % MAX_WORDS];
|
||||||
}
|
int word_length = strlen(random_word);
|
||||||
guessed[word_length] = '\0';
|
|
||||||
|
if (current_length + word_length <= target_length) {
|
||||||
while (tries < MAX_TRIES && guessed_correctly < word_length) {
|
if (current_length > 0) {
|
||||||
printf("\nMot à deviner : %s\n", guessed);
|
strcat(constructed_word, " ");
|
||||||
display_hangman(tries);
|
current_length++;
|
||||||
char guess;
|
}
|
||||||
printf("Entrez une lettre : ");
|
strcat(constructed_word, random_word);
|
||||||
scanf(" %c", &guess);
|
current_length += word_length;
|
||||||
int found = 0;
|
}
|
||||||
|
attempts++;
|
||||||
for (int i = 0; i < word_length; i++) {
|
}
|
||||||
if (word[i] == guess) {
|
|
||||||
if (guessed[i] == '_') {
|
if (attempts >= 100) {
|
||||||
guessed[i] = guess;
|
printf("Error: Unable to create a word of specified length.\n");
|
||||||
guessed_correctly++;
|
free(constructed_word);
|
||||||
}
|
exit(1);
|
||||||
found = 1;
|
}
|
||||||
}
|
return constructed_word;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!found) {
|
// Process guessed letter and update game status
|
||||||
tries++;
|
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 (guessed_correctly == word_length) {
|
if (word[i] == guess && guessed[i] == '_') {
|
||||||
printf("Félicitations ! Vous avez deviné le mot : %s\n", word);
|
guessed[i] = guess;
|
||||||
} else {
|
(*guessed_correctly)++;
|
||||||
printf("Désolé, vous avez perdu. Le mot était : %s\n", word);
|
(*score) += 10;
|
||||||
display_hangman(MAX_TRIES);
|
found = 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return 0;
|
|
||||||
}
|
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("\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);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user