changement total de la difficulté
This commit is contained in:
parent
3e92ebda4a
commit
51b14f6e15
62
pendu.c
62
pendu.c
@ -20,9 +20,11 @@ const char *words[MAX_WORDS] = {
|
|||||||
"gyroscope",
|
"gyroscope",
|
||||||
"periclitation",
|
"periclitation",
|
||||||
"susurrer",
|
"susurrer",
|
||||||
"eclesiastique",
|
"ecclesiastique",
|
||||||
"de",
|
"test",
|
||||||
"a"
|
"oui",
|
||||||
|
"non"
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void display_hangman(int tries) {
|
void display_hangman(int tries) {
|
||||||
@ -36,77 +38,70 @@ void display_hangman(int tries) {
|
|||||||
case 6: printf(" ----\n | |\n | O\n | /|\\\n | / \\\n |\n--------\n"); break;
|
case 6: printf(" ----\n | |\n | O\n | /|\\\n | / \\\n |\n--------\n"); break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Fonction pour choisir des mots jusqu'à atteindre la longueur demandée
|
||||||
char *hidden_word(int length_choice) {
|
char *hidden_word(int length_choice) {
|
||||||
|
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
char word = words[rand() % MAX_WORDS];
|
|
||||||
|
char *total_word = malloc((length_choice + 1) * sizeof(char));
|
||||||
|
total_word[0] = '\0';
|
||||||
|
|
||||||
|
int total_length = 0;
|
||||||
|
|
||||||
|
|
||||||
|
while (total_length < length_choice) {
|
||||||
|
const char *word = words[rand() % MAX_WORDS];
|
||||||
int word_length = strlen(word);
|
int word_length = strlen(word);
|
||||||
int stay_length = length_choice;
|
|
||||||
char *total_word;
|
|
||||||
int number_of_word = 0;
|
|
||||||
int total_length = word_length;
|
|
||||||
|
|
||||||
for(int y=0; word_length > length_choice; y++){
|
if (total_length + word_length <= length_choice) {
|
||||||
word = *words[rand() % MAX_WORDS];
|
if (total_length > 0) {
|
||||||
word_length = strlen(word);
|
strcat(total_word, " ");
|
||||||
|
total_length++;
|
||||||
}
|
}
|
||||||
|
|
||||||
total_word = word;
|
strcat(total_word, word);
|
||||||
|
total_length += word_length;
|
||||||
for(int y=0; word_length > stay_length; y++){
|
|
||||||
|
|
||||||
word = *words[rand() % MAX_WORDS];
|
|
||||||
word_length = strlen(total_word);
|
|
||||||
|
|
||||||
if(stay_length >= word_length){
|
|
||||||
total_word = word +'\0'+ *total_word;
|
|
||||||
total_length = strlen(total_word);
|
|
||||||
stay_length = total_length - length_choice;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return total_word;
|
return total_word;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int length_choice = 26;
|
int length_choice;
|
||||||
printf("Entrez la difficulté (Taille maximum du mot à deviner) : ");
|
printf("Entrez la difficulté (Taille maximum du mot à deviner) : ");
|
||||||
scanf("%d", &length_choice);
|
scanf("%d", &length_choice);
|
||||||
|
|
||||||
char *word = hidden_word(length_choice);
|
char *word = hidden_word(length_choice);
|
||||||
int word_length = strlen(word);
|
int word_length = strlen(word);
|
||||||
|
|
||||||
char guessed[word_length];
|
char guessed[word_length + 1];
|
||||||
int tries = 0;
|
int tries = 0;
|
||||||
int guessed_correctly = 0;
|
int guessed_correctly = 0;
|
||||||
|
|
||||||
|
|
||||||
for (int i = 0; i < word_length; i++) {
|
for (int i = 0; i < word_length; i++) {
|
||||||
guessed[i] = '_';
|
guessed[i] = (word[i] == ' ') ? ' ' : '_';
|
||||||
}
|
}
|
||||||
guessed[word_length] = '\0';
|
guessed[word_length] = '\0';
|
||||||
|
|
||||||
while (tries < MAX_TRIES && guessed_correctly < word_length) {
|
while (tries < MAX_TRIES && guessed_correctly < word_length) {
|
||||||
printf("\nMot à deviner : %s\n", guessed);
|
printf("\nMot à deviner : %s\n", guessed);
|
||||||
display_hangman(tries);
|
display_hangman(tries);
|
||||||
|
|
||||||
char guess;
|
char guess;
|
||||||
printf("Entrez une lettre : ");
|
printf("Entrez une lettre : ");
|
||||||
scanf(" %c", &guess);
|
scanf(" %c", &guess);
|
||||||
int found = 0;
|
|
||||||
|
|
||||||
|
int found = 0;
|
||||||
for (int i = 0; i < word_length; i++) {
|
for (int i = 0; i < word_length; i++) {
|
||||||
if (word[i] == guess) {
|
if (word[i] == guess && guessed[i] == '_') {
|
||||||
if (guessed[i] == '_') {
|
|
||||||
guessed[i] = guess;
|
guessed[i] = guess;
|
||||||
guessed_correctly++;
|
guessed_correctly++;
|
||||||
}
|
|
||||||
found = 1;
|
found = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
tries++;
|
tries++;
|
||||||
}
|
}
|
||||||
@ -119,5 +114,6 @@ int main() {
|
|||||||
display_hangman(MAX_TRIES);
|
display_hangman(MAX_TRIES);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(word);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user