TD3_DEV51_Qualite_Algo/pendu.c
2024-10-15 11:35:40 +02:00

214 lines
6.4 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_WORDS 14
#define MAX_TRIES 6
#define MAX_WORDS_LENGTH 50
const char *words[MAX_WORDS] = {
"programmation",
"ordinateur",
"langage",
"jeu",
"algorithmique",
"fontainebleau",
"koala",
"anticonstitutionnellement",
"code",
"canard",
"gyroscope",
"periclitation",
"susurrer",
"eclesiastique"
};
void display_hangman(int tries) {
switch (tries) {
case 0: printf(" ----\n | |\n |\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;
case 5: printf(" ----\n | |\n | O\n | /|\\\n | /\n |\n--------\n"); break;
case 6: printf(" ----\n | |\n | O\n | /|\\\n | / \\\n |\n--------\n"); break;
}
}
int ask_word_length() {
int word_length;
printf("Veuillez entrer la longueur du mot souhaitée (entier positif) : ");
while (scanf("%d", &word_length) != 1 || word_length <= 0) {
printf("Entrée invalide. Veuillez entrer un entier positif : ");
while (getchar() != '\n'); // vider le buffer
}
return word_length;
}
char* difficult(int word_length) {
int size1 = 0, size2 = 0;
char** selected_words = selecte_words(word_length, &size1);
if (selected_words == NULL) {
printf("Erreur lors de la sélection des mots\n");
return NULL;
}
char** merged_words = word_merge(word_length, &size2);
if (merged_words == NULL) {
printf("Erreur lors de la fusion des mots\n");
return NULL;
}
char** final_words = merge_string_arrays(selected_words, merged_words, size1, size2);
// Libérer la mémoire des tableaux intermédiaires
for (int i = 0; i < size1; i++) free(selected_words[i]);
for (int i = 0; i < size2; i++) free(merged_words[i]);
free(selected_words);
free(merged_words);
if (final_words == NULL) {
printf("Erreur lors de la fusion finale des mots\n");
return NULL;
}
// Sélectionner un mot aléatoire
char* chosen_word = final_words[rand() % (size1 + size2)];
return chosen_word;
}
char** merge_string_arrays(char** array1, char** array2, int size1, int size2) {
int merged_size = size1 + size2;
char** merged_array = (char**)malloc(merged_size * sizeof(char*));
if (merged_array == NULL) {
printf("Erreur d'allocation de mémoire\n");
return NULL;
}
for (int i = 0; i < size1; i++) {
merged_array[i] = (char*)malloc((strlen(array1[i]) + 1) * sizeof(char));
if (merged_array[i] == NULL) {
printf("Erreur d'allocation de mémoire pour array1[%d]\n", i);
return NULL;
}
strcpy(merged_array[i], array1[i]);
}
for (int i = 0; i < size2; i++) {
merged_array[size1 + i] = (char*)malloc((strlen(array2[i]) + 1) * sizeof(char));
if (merged_array[size1 + i] == NULL) {
printf("Erreur d'allocation de mémoire pour array2[%d]\n", i);
return NULL;
}
strcpy(merged_array[size1 + i], array2[i]);
}
return merged_array;
}
char** selecte_words(int word_length, int* size) {
char** word_Selected = (char**)malloc(MAX_WORDS * sizeof(char*));
if (word_Selected == NULL) {
printf("Erreur d'allocation de mémoire\n");
return NULL;
}
*size = 0; // initialiser la taille à 0
for (int i = 0; i < MAX_WORDS && words[i][0] != '\0'; i++) {
if (strlen(words[i]) == word_length) {
word_Selected[*size] = (char*)malloc((strlen(words[i]) + 1) * sizeof(char));
if (word_Selected[*size] == NULL) {
printf("Erreur d'allocation de mémoire pour le mot sélectionné\n");
return NULL;
}
strcpy(word_Selected[*size], words[i]);
(*size)++;
}
}
return word_Selected;
}
char** word_merge(int word_length, int* size2) {
int max_combinations = MAX_WORDS * MAX_WORDS;
char** word_combinations = (char**)malloc(max_combinations * sizeof(char*));
if (word_combinations == NULL) {
printf("Erreur d'allocation de mémoire pour les combinaisons\n");
return NULL;
}
*size2 = 0;
for (int i = 0; i < MAX_WORDS; i++) {
for (int j = 0; j < MAX_WORDS; j++) {
if (i != j && (strlen(words[i]) + strlen(words[j]) == word_length - 1)) {
word_combinations[*size2] = (char*)malloc((strlen(words[i]) + strlen(words[j]) + 2) * sizeof(char)); // +2 pour l'espace et '\0'
if (word_combinations[*size2] == NULL) {
printf("Erreur d'allocation de mémoire pour une combinaison\n");
return NULL;
}
snprintf(word_combinations[*size2], strlen(words[i]) + strlen(words[j]) + 2, "%s %s", words[i], words[j]);
(*size2)++;
}
}
}
return word_combinations;
}
int main() {
srand(time(NULL));
int word_length = ask_word_length();
const char* word = difficult(word_length);
if (word == NULL) {
printf("Erreur : aucun mot disponible avec cette longueur.\n");
return 1;
}
int word_len = strlen(word);
char guessed[word_len + 1];
int tries = 0, guessed_correctly = 0;
for (int i = 0; i < word_len; i++) guessed[i] = '_';
guessed[word_len] = '\0';
while (tries < MAX_TRIES && guessed_correctly < word_len) {
printf("\nMot à deviner : %s\n", guessed);
display_hangman(tries);
char guess;
printf("Entrez une lettre : ");
scanf(" %c", &guess);
int found = 0;
for (int i = 0; i < word_len; i++) {
if (word[i] == guess && guessed[i] == '_') {
guessed[i] = guess;
guessed_correctly++;
found = 1;
}
}
if (!found) {
tries++;
}
}
if (guessed_correctly == word_len) {
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);
}
return 0;
}