Compéxité
This commit is contained in:
parent
67b7016376
commit
76bc9356bf
25
complexité.txt
Normal file
25
complexité.txt
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
================================================
|
||||||
|
NLOC CCN token PARAM length location
|
||||||
|
------------------------------------------------
|
||||||
|
12 1 40 1 12 display_hangman@20-31@allPendu.c
|
||||||
|
10 3 74 1 10 choose_or_construct_word@34-43@allPendu.c
|
||||||
|
19 6 126 1 21 construct_word@46-66@allPendu.c
|
||||||
|
15 5 118 6 17 process_guess@69-85@allPendu.c
|
||||||
|
16 3 138 7 16 add_to_logs@88-103@allPendu.c
|
||||||
|
7 3 62 2 7 initialize_guessed_word@106-112@allPendu.c
|
||||||
|
28 4 200 0 36 main@115-150@allPendu.c
|
||||||
|
3 1 23 2 3 check_timeout@153-155@allPendu.c
|
||||||
|
10 2 73 5 11 play_turn@158-168@allPendu.c
|
||||||
|
17 4 126 7 17 display_game_result@171-187@allPendu.c
|
||||||
|
1 file analyzed.
|
||||||
|
==============================================================
|
||||||
|
NLOC Avg.NLOC AvgCCN Avg.token function_cnt file
|
||||||
|
--------------------------------------------------------------
|
||||||
|
149 13.7 3.2 98.0 10 allPendu.c
|
||||||
|
|
||||||
|
===============================================================================================================
|
||||||
|
No thresholds exceeded (cyclomatic_complexity > 15 or length > 1000 or nloc > 1000000 or parameter_count > 100)
|
||||||
|
==========================================================================================
|
||||||
|
Total nloc Avg.NLOC AvgCCN Avg.token Fun Cnt Warning cnt Fun Rt nloc Rt
|
||||||
|
------------------------------------------------------------------------------------------
|
||||||
|
149 13.7 3.2 98.0 10 0 0.00 0.00
|
97
pendu.c
97
pendu.c
@ -18,20 +18,20 @@ const char *words[MAX_WORDS] = {
|
|||||||
|
|
||||||
// Display hangman based on the number of incorrect tries
|
// Display hangman based on the number of incorrect tries
|
||||||
void display_hangman(int tries) {
|
void display_hangman(int tries) {
|
||||||
switch (tries) {
|
const char *stages[] = {
|
||||||
case 0: printf(" ----\n | |\n |\n |\n |\n |\n--------\n"); break;
|
" ----\n | |\n |\n |\n |\n |\n--------\n",
|
||||||
case 1: printf(" ----\n | |\n | O\n |\n |\n |\n--------\n"); break;
|
" ----\n | |\n | O\n |\n |\n |\n--------\n",
|
||||||
case 2: printf(" ----\n | |\n | O\n | |\n |\n |\n--------\n"); break;
|
" ----\n | |\n | O\n | |\n |\n |\n--------\n",
|
||||||
case 3: printf(" ----\n | |\n | O\n | /|\n |\n |\n--------\n"); break;
|
" ----\n | |\n | O\n | /|\n |\n |\n--------\n",
|
||||||
case 4: printf(" ----\n | |\n | O\n | /|\\\n |\n |\n--------\n"); break;
|
" ----\n | |\n | O\n | /|\\\n |\n |\n--------\n",
|
||||||
case 5: printf(" ----\n | |\n | O\n | /|\\\n | /\n |\n--------\n"); break;
|
" ----\n | |\n | O\n | /|\\\n | /\n |\n--------\n",
|
||||||
case 6: printf(" ----\n | |\n | O\n | /|\\\n | / \\\n |\n--------\n"); break;
|
" ----\n | |\n | O\n | /|\\\n | / \\\n |\n--------\n"
|
||||||
}
|
};
|
||||||
|
printf("%s", stages[tries]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate a word of the specified length or construct a word if not found
|
// Choose or construct a word of the specified length
|
||||||
char *generate_word(int target_length) {
|
char *choose_or_construct_word(int target_length) {
|
||||||
srand(time(NULL));
|
|
||||||
for (int i = 0; i < MAX_WORDS; i++) {
|
for (int i = 0; i < MAX_WORDS; i++) {
|
||||||
if (strlen(words[i]) == target_length) {
|
if (strlen(words[i]) == target_length) {
|
||||||
char *selected_word = malloc((target_length + 1) * sizeof(char));
|
char *selected_word = malloc((target_length + 1) * sizeof(char));
|
||||||
@ -39,6 +39,11 @@ char *generate_word(int target_length) {
|
|||||||
return selected_word;
|
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));
|
char *constructed_word = malloc((target_length + 1) * sizeof(char));
|
||||||
constructed_word[0] = '\0';
|
constructed_word[0] = '\0';
|
||||||
int current_length = 0, attempts = 0;
|
int current_length = 0, attempts = 0;
|
||||||
@ -57,13 +62,7 @@ char *generate_word(int target_length) {
|
|||||||
}
|
}
|
||||||
attempts++;
|
attempts++;
|
||||||
}
|
}
|
||||||
|
return attempts < 100 ? constructed_word : NULL;
|
||||||
if (attempts >= 100) {
|
|
||||||
printf("Error: Unable to create a word of specified length.\n");
|
|
||||||
free(constructed_word);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
return constructed_word;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process guessed letter and update game status
|
// Process guessed letter and update game status
|
||||||
@ -103,6 +102,15 @@ void add_to_logs(const char *word, int tries, int guessed_correctly, bool won, c
|
|||||||
fclose(log_file);
|
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
|
// Main function for hangman game
|
||||||
int main() {
|
int main() {
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
@ -111,17 +119,12 @@ int main() {
|
|||||||
printf("Enter the difficulty (Maximum word length to guess): ");
|
printf("Enter the difficulty (Maximum word length to guess): ");
|
||||||
scanf("%d", &length_choice);
|
scanf("%d", &length_choice);
|
||||||
|
|
||||||
char *word = generate_word(length_choice);
|
char *word = choose_or_construct_word(length_choice);
|
||||||
int word_length = strlen(word);
|
int word_length = strlen(word);
|
||||||
|
|
||||||
char guessed[word_length + 1];
|
char guessed[word_length + 1];
|
||||||
int tries = 0, guessed_correctly = 0, score = 0, timeout = 0;
|
int tries = 0, guessed_correctly = 0, score = 0, timeout = 0;
|
||||||
char guess;
|
initialize_guessed_word(guessed, word);
|
||||||
|
|
||||||
for (int i = 0; i < word_length; i++) {
|
|
||||||
guessed[i] = (word[i] == ' ') ? ' ' : '_';
|
|
||||||
}
|
|
||||||
guessed[word_length] = '\0';
|
|
||||||
|
|
||||||
time_t game_start_time = time(NULL);
|
time_t game_start_time = time(NULL);
|
||||||
|
|
||||||
@ -129,7 +132,7 @@ int main() {
|
|||||||
printf("\nWord to guess: %s\n", guessed);
|
printf("\nWord to guess: %s\n", guessed);
|
||||||
display_hangman(tries);
|
display_hangman(tries);
|
||||||
|
|
||||||
if (difftime(time(NULL), game_start_time) > TIME_LIMIT) {
|
if (check_timeout(game_start_time, TIME_LIMIT)) {
|
||||||
printf("Time's up! You exceeded %d seconds.\n", TIME_LIMIT);
|
printf("Time's up! You exceeded %d seconds.\n", TIME_LIMIT);
|
||||||
printf("Sorry, you lost. The word was: %s\n", word);
|
printf("Sorry, you lost. The word was: %s\n", word);
|
||||||
display_hangman(MAX_TRIES);
|
display_hangman(MAX_TRIES);
|
||||||
@ -137,21 +140,37 @@ int main() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Enter a letter: ");
|
play_turn(word, guessed, &guessed_correctly, &score, &tries);
|
||||||
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);
|
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 (!timeout) {
|
||||||
if (guessed_correctly == word_length) {
|
if (guessed_correctly == strlen(word)) {
|
||||||
printf("Congratulations! You've guessed the word: %s\n", word);
|
printf("Congratulations! You've guessed the word: %s\n", word);
|
||||||
if (tries < 3) {
|
if (tries < 3) {
|
||||||
score += 20;
|
score += 20;
|
||||||
@ -164,9 +183,5 @@ int main() {
|
|||||||
add_to_logs(word, tries, guessed_correctly, false, guessed, time_spent, score);
|
add_to_logs(word, tries, guessed_correctly, false, guessed, time_spent, score);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Final score: %d\n", score);
|
printf("Final score: %d\n", score);
|
||||||
|
|
||||||
free(word);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user