main - 50 lignes
This commit is contained in:
parent
912354e062
commit
0ac6119bb3
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +0,0 @@
|
|||||||
*.exe
|
|
136
pendu.c
136
pendu.c
@ -3,26 +3,18 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
|
// Define constants for the maximum number of words and tries
|
||||||
#define MAX_WORDS 14
|
#define MAX_WORDS 14
|
||||||
#define MAX_TRIES 6
|
#define MAX_TRIES 6
|
||||||
|
|
||||||
|
// Array of words used in the game
|
||||||
const char *words[MAX_WORDS] = {
|
const char *words[MAX_WORDS] = {
|
||||||
"programmation",
|
"programmation", "ordinateur", "langage", "jeu", "algorithmique",
|
||||||
"ordinateur",
|
"fontainebleau", "koala", "anticonstitutionnellement", "code",
|
||||||
"langage",
|
"canard", "gyroscope", "periclitation", "susurrer", "eclesiastique"
|
||||||
"jeu",
|
|
||||||
"algorithmique",
|
|
||||||
"fontainebleau",
|
|
||||||
"koala",
|
|
||||||
"anticonstitutionnellement",
|
|
||||||
"code",
|
|
||||||
"canard",
|
|
||||||
"gyroscope",
|
|
||||||
"periclitation",
|
|
||||||
"susurrer",
|
|
||||||
"eclesiastique"
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Function to display the hangman figure based on the number of tries
|
||||||
void display_hangman(int tries) {
|
void display_hangman(int tries) {
|
||||||
switch (tries) {
|
switch (tries) {
|
||||||
case 0: printf(" ----\n | |\n |\n |\n |\n |\n--------\n"); break;
|
case 0: printf(" ----\n | |\n |\n |\n |\n |\n--------\n"); break;
|
||||||
@ -35,83 +27,53 @@ void display_hangman(int tries) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to randomly choose words and concatenate them to create a puzzle word
|
||||||
void choose_words(char *chosen_word, int max_length) {
|
void choose_words(char *chosen_word, int max_length) {
|
||||||
int total_length = 0;
|
int total_length = 0, used[MAX_WORDS] = {0}, word_index;
|
||||||
int used[MAX_WORDS] = {0}; // Array to mark used words
|
chosen_word[0] = '\0';
|
||||||
int word_index;
|
|
||||||
|
|
||||||
chosen_word[0] = '\0'; // Initialize empty string
|
|
||||||
|
|
||||||
|
// Select words until the maximum word length is reached or exceeded
|
||||||
while (total_length < max_length) {
|
while (total_length < max_length) {
|
||||||
word_index = rand() % MAX_WORDS;
|
word_index = rand() % MAX_WORDS;
|
||||||
|
|
||||||
// Ensure we do not exceed max_length or reuse a word
|
|
||||||
if (!used[word_index] && total_length + strlen(words[word_index]) + (total_length > 0 ? 1 : 0) <= max_length) {
|
if (!used[word_index] && total_length + strlen(words[word_index]) + (total_length > 0 ? 1 : 0) <= max_length) {
|
||||||
// Add a space if this isn't the first word
|
if (total_length > 0) { strcat(chosen_word, " "); total_length++; }
|
||||||
if (total_length > 0) {
|
strcat(chosen_word, words[word_index]);
|
||||||
strcat(chosen_word, " ");
|
|
||||||
total_length++; // Account for the space
|
|
||||||
}
|
|
||||||
|
|
||||||
strcat(chosen_word, words[word_index]); // Add the word to the chosen word
|
|
||||||
total_length += strlen(words[word_index]);
|
total_length += strlen(words[word_index]);
|
||||||
used[word_index] = 1; // Mark the word as used
|
used[word_index] = 1;
|
||||||
|
if (total_length == max_length) break;
|
||||||
// If we're already at the exact length, break
|
|
||||||
if (total_length == max_length) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Break if no more suitable words can be found
|
// Check if all words that could fit are used
|
||||||
int all_used = 1;
|
int all_used = 1;
|
||||||
for (int i = 0; i < MAX_WORDS; i++) {
|
for (int i = 0; i < MAX_WORDS; i++) {
|
||||||
if (!used[i] && total_length + strlen(words[i]) + 1 <= max_length) {
|
if (!used[i] && total_length + strlen(words[i]) + 1 <= max_length) { all_used = 0; break; }
|
||||||
all_used = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (all_used) break;
|
if (all_used) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
// Function to play the hangman game
|
||||||
srand(time(NULL));
|
void play_game(const char *chosen_word, int word_length, int letters_to_guess) {
|
||||||
|
char guessed[word_length + 1];
|
||||||
|
int tries = 0, guessed_correctly = 0;
|
||||||
|
|
||||||
int max_word_length;
|
// Initialize guessed word display with underscores
|
||||||
printf("Choisissez la taille maximum du mot à deviner : ");
|
for (int i = 0; i < word_length; i++) guessed[i] = (chosen_word[i] == ' ') ? ' ' : '_';
|
||||||
scanf("%d", &max_word_length);
|
|
||||||
|
|
||||||
char chosen_word[max_word_length]; // String to store the chosen combination of words
|
|
||||||
choose_words(chosen_word, max_word_length);
|
|
||||||
|
|
||||||
int word_length = strlen(chosen_word);
|
|
||||||
char guessed[word_length + 1]; // +1 for the null terminator
|
|
||||||
int tries = 0;
|
|
||||||
int guessed_correctly = 0;
|
|
||||||
int letters_to_guess = 0; // Total letters to guess (excluding spaces)
|
|
||||||
|
|
||||||
// Initialize the guessed array and count the total number of letters to guess
|
|
||||||
for (int i = 0; i < word_length; i++) {
|
|
||||||
if (chosen_word[i] == ' ') {
|
|
||||||
guessed[i] = ' '; // Spaces are pre-revealed
|
|
||||||
} else {
|
|
||||||
guessed[i] = '_';
|
|
||||||
letters_to_guess++; // Only count non-space characters
|
|
||||||
}
|
|
||||||
}
|
|
||||||
guessed[word_length] = '\0';
|
guessed[word_length] = '\0';
|
||||||
|
|
||||||
|
// Main game loop, stops if max tries are reached or the word is guessed
|
||||||
while (tries < MAX_TRIES && guessed_correctly < letters_to_guess) {
|
while (tries < MAX_TRIES && guessed_correctly < letters_to_guess) {
|
||||||
printf("\nMot à deviner : %s\n", guessed);
|
printf("\nWord to guess: %s\n", guessed);
|
||||||
display_hangman(tries);
|
display_hangman(tries);
|
||||||
|
|
||||||
|
// Get a letter guess from the player
|
||||||
char guess;
|
char guess;
|
||||||
printf("Entrez une lettre : ");
|
printf("Enter a letter: ");
|
||||||
scanf(" %c", &guess);
|
scanf(" %c", &guess);
|
||||||
int found = 0;
|
|
||||||
int already_revealed = 0;
|
int found = 0, already_revealed = 0;
|
||||||
|
|
||||||
// Check if the guessed letter is in the word
|
// Check if the letter is in the word and update guessed word display
|
||||||
for (int i = 0; i < word_length; i++) {
|
for (int i = 0; i < word_length; i++) {
|
||||||
if (chosen_word[i] == guess) {
|
if (chosen_word[i] == guess) {
|
||||||
if (guessed[i] == '_') {
|
if (guessed[i] == '_') {
|
||||||
@ -119,23 +81,43 @@ int main() {
|
|||||||
guessed_correctly++;
|
guessed_correctly++;
|
||||||
found = 1;
|
found = 1;
|
||||||
} else {
|
} else {
|
||||||
already_revealed = 1; // The letter is already revealed
|
already_revealed = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only increment tries if the letter was not found and not already revealed
|
// Increment tries if the letter was not found and not already revealed
|
||||||
if (!found && !already_revealed) {
|
if (!found && !already_revealed) tries++;
|
||||||
tries++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (guessed_correctly == letters_to_guess) {
|
// Display win or lose message based on game outcome
|
||||||
printf("Félicitations ! Vous avez deviné le mot : %s\n", chosen_word);
|
if (guessed_correctly == letters_to_guess)
|
||||||
} else {
|
printf("Congratulations! You've guessed the word: %s\n", chosen_word);
|
||||||
printf("Désolé, vous avez perdu. Le mot était : %s\n", chosen_word);
|
else {
|
||||||
|
printf("Sorry, you've lost. The word was: %s\n", chosen_word);
|
||||||
display_hangman(MAX_TRIES);
|
display_hangman(MAX_TRIES);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Main function to start the game
|
||||||
|
int main() {
|
||||||
|
srand(time(NULL));
|
||||||
|
int max_word_length;
|
||||||
|
|
||||||
|
// Prompt user for maximum word length for the puzzle
|
||||||
|
printf("Enter the maximum word length to guess: ");
|
||||||
|
scanf("%d", &max_word_length);
|
||||||
|
|
||||||
|
// Select a word within the specified length
|
||||||
|
char chosen_word[max_word_length];
|
||||||
|
choose_words(chosen_word, max_word_length);
|
||||||
|
|
||||||
|
// Calculate total letters to guess (excluding spaces)
|
||||||
|
int word_length = strlen(chosen_word), letters_to_guess = 0;
|
||||||
|
for (int i = 0; i < word_length; i++)
|
||||||
|
if (chosen_word[i] != ' ') letters_to_guess++;
|
||||||
|
|
||||||
|
// Start the game with the chosen word
|
||||||
|
play_game(chosen_word, word_length, letters_to_guess);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user