forked from menault/TD3_DEV51_Qualite_Algo
Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
51b14f6e15 | |||
3e92ebda4a |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
*.out
|
11
Readme.md
11
Readme.md
@@ -1,11 +0,0 @@
|
|||||||
### Évaluation de la qualité du code (README)
|
|
||||||
|
|
||||||
Le code présente une **complexité cyclomatique moyenne de 3.2**, ce qui est raisonnable et montre une structure assez simple dans l'ensemble. Voici les points clés :
|
|
||||||
|
|
||||||
- **Fonctions bien structurées** : La majorité des fonctions ont une complexité cyclomatique faible (1-6), indiquant qu'elles sont relativement simples et devraient être faciles à maintenir et comprendre.
|
|
||||||
- **Fonctions avec complexité modérée** : `construct_word` (CCN = 6) et `process_guess` (CCN = 5) sont les plus complexes. Elles pourraient bénéficier de petites optimisations pour améliorer leur lisibilité, bien que cela reste acceptable.
|
|
||||||
- **Fonction `main` (CCN = 4)** : La fonction principale reste gérable mais pourrait être divisée en sous-fonctions pour améliorer la clarté du code.
|
|
||||||
|
|
||||||
### Conclusion
|
|
||||||
|
|
||||||
Le code est globalement bien structuré et facile à maintenir, avec une complexité maîtrisée. Aucun dépassement de seuil n’a été détecté, ce qui indique une bonne organisation des fonctions.
|
|
@@ -1,25 +0,0 @@
|
|||||||
================================================
|
|
||||||
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
|
|
226
pendu.c
226
pendu.c
@@ -2,186 +2,118 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <ctype.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
#define MAX_WORDS 17
|
#define MAX_WORDS 14
|
||||||
#define MAX_TRIES 6
|
#define MAX_TRIES 6
|
||||||
#define TIME_LIMIT 30 // Time limit for the game in seconds
|
|
||||||
|
|
||||||
const char *words[MAX_WORDS] = {
|
const char *words[MAX_WORDS] = {
|
||||||
"programming", "computer", "language", "game", "algorithm",
|
"programmation",
|
||||||
"fontainebleau", "koala", "anticonstitutionally", "code",
|
"ordinateur",
|
||||||
"duck", "gyroscope", "endangerment", "whisper", "ecclesiastic",
|
"langage",
|
||||||
"test", "yes", "no"
|
"jeu",
|
||||||
|
"algorithmique",
|
||||||
|
"fontainebleau",
|
||||||
|
"koala",
|
||||||
|
"anticonstitutionnellement",
|
||||||
|
"code",
|
||||||
|
"canard",
|
||||||
|
"gyroscope",
|
||||||
|
"periclitation",
|
||||||
|
"susurrer",
|
||||||
|
"ecclesiastique",
|
||||||
|
"test",
|
||||||
|
"oui",
|
||||||
|
"non"
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Display hangman based on the number of incorrect tries
|
|
||||||
void display_hangman(int tries) {
|
void display_hangman(int tries) {
|
||||||
const char *stages[] = {
|
switch (tries) {
|
||||||
" ----\n | |\n |\n |\n |\n |\n--------\n",
|
case 0: printf(" ----\n | |\n |\n |\n |\n |\n--------\n"); break;
|
||||||
" ----\n | |\n | O\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;
|
||||||
};
|
|
||||||
printf("%s", stages[tries]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Choose or construct a word of the specified length
|
|
||||||
char *choose_or_construct_word(int target_length) {
|
|
||||||
for (int i = 0; i < MAX_WORDS; i++) {
|
|
||||||
if (strlen(words[i]) == target_length) {
|
|
||||||
char *selected_word = malloc((target_length + 1) * sizeof(char));
|
|
||||||
strcpy(selected_word, words[i]);
|
|
||||||
return selected_word;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return construct_word(target_length);
|
|
||||||
}
|
}
|
||||||
|
// Fonction pour choisir des mots jusqu'à atteindre la longueur demandée
|
||||||
|
char *hidden_word(int length_choice) {
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
// Helper function to construct a word if none of the exact length is found
|
char *total_word = malloc((length_choice + 1) * sizeof(char));
|
||||||
char *construct_word(int target_length) {
|
total_word[0] = '\0';
|
||||||
char *constructed_word = malloc((target_length + 1) * sizeof(char));
|
|
||||||
constructed_word[0] = '\0';
|
|
||||||
int current_length = 0, attempts = 0;
|
|
||||||
|
|
||||||
while (current_length < target_length && attempts < 100) {
|
int total_length = 0;
|
||||||
const char *random_word = words[rand() % MAX_WORDS];
|
|
||||||
int word_length = strlen(random_word);
|
|
||||||
|
|
||||||
if (current_length + word_length <= target_length) {
|
|
||||||
if (current_length > 0) {
|
while (total_length < length_choice) {
|
||||||
strcat(constructed_word, " ");
|
const char *word = words[rand() % MAX_WORDS];
|
||||||
current_length++;
|
int word_length = strlen(word);
|
||||||
|
|
||||||
|
if (total_length + word_length <= length_choice) {
|
||||||
|
if (total_length > 0) {
|
||||||
|
strcat(total_word, " ");
|
||||||
|
total_length++;
|
||||||
}
|
}
|
||||||
strcat(constructed_word, random_word);
|
|
||||||
current_length += word_length;
|
|
||||||
}
|
|
||||||
attempts++;
|
|
||||||
}
|
|
||||||
return attempts < 100 ? constructed_word : NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process guessed letter and update game status
|
strcat(total_word, word);
|
||||||
void process_guess(const char *word, char *guessed, char guess, int *guessed_correctly, int *score, int *tries) {
|
total_length += word_length;
|
||||||
int found = 0, word_length = strlen(word);
|
|
||||||
|
|
||||||
for (int i = 0; i < word_length; i++) {
|
|
||||||
if (word[i] == guess && guessed[i] == '_') {
|
|
||||||
guessed[i] = guess;
|
|
||||||
(*guessed_correctly)++;
|
|
||||||
(*score) += 10;
|
|
||||||
found = 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!found) {
|
return total_word;
|
||||||
(*tries)++;
|
|
||||||
(*score) -= 5;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log the game's statistics to a file
|
int main() {
|
||||||
void add_to_logs(const char *word, int tries, int guessed_correctly, bool won, char guessed[], double time_spent, int score) {
|
int length_choice;
|
||||||
FILE *log_file = fopen("hangman.log", "a");
|
printf("Entrez la difficulté (Taille maximum du mot à deviner) : ");
|
||||||
if (log_file == NULL) {
|
scanf("%d", &length_choice);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize guessed word state with underscores
|
char *word = hidden_word(length_choice);
|
||||||
void initialize_guessed_word(char *guessed, const char *word) {
|
|
||||||
int word_length = strlen(word);
|
int word_length = strlen(word);
|
||||||
|
|
||||||
|
char guessed[word_length + 1];
|
||||||
|
int tries = 0;
|
||||||
|
int guessed_correctly = 0;
|
||||||
|
|
||||||
|
|
||||||
for (int i = 0; i < word_length; i++) {
|
for (int i = 0; i < word_length; i++) {
|
||||||
guessed[i] = (word[i] == ' ') ? ' ' : '_';
|
guessed[i] = (word[i] == ' ') ? ' ' : '_';
|
||||||
}
|
}
|
||||||
guessed[word_length] = '\0';
|
guessed[word_length] = '\0';
|
||||||
}
|
|
||||||
|
|
||||||
// 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 = choose_or_construct_word(length_choice);
|
|
||||||
int word_length = strlen(word);
|
|
||||||
|
|
||||||
char guessed[word_length + 1];
|
|
||||||
int tries = 0, guessed_correctly = 0, score = 0, timeout = 0;
|
|
||||||
initialize_guessed_word(guessed, word);
|
|
||||||
|
|
||||||
time_t game_start_time = time(NULL);
|
|
||||||
|
|
||||||
while (tries < MAX_TRIES && guessed_correctly < word_length) {
|
while (tries < MAX_TRIES && guessed_correctly < word_length) {
|
||||||
printf("\nWord to guess: %s\n", guessed);
|
printf("\nMot à deviner : %s\n", guessed);
|
||||||
display_hangman(tries);
|
display_hangman(tries);
|
||||||
|
|
||||||
if (check_timeout(game_start_time, TIME_LIMIT)) {
|
char guess;
|
||||||
printf("Time's up! You exceeded %d seconds.\n", TIME_LIMIT);
|
printf("Entrez une lettre : ");
|
||||||
printf("Sorry, you lost. The word was: %s\n", word);
|
scanf(" %c", &guess);
|
||||||
display_hangman(MAX_TRIES);
|
|
||||||
timeout = 1;
|
int found = 0;
|
||||||
break;
|
for (int i = 0; i < word_length; i++) {
|
||||||
|
if (word[i] == guess && guessed[i] == '_') {
|
||||||
|
guessed[i] = guess;
|
||||||
|
guessed_correctly++;
|
||||||
|
found = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
play_turn(word, guessed, &guessed_correctly, &score, &tries);
|
|
||||||
|
if (!found) {
|
||||||
|
tries++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (guessed_correctly == word_length) {
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
double time_spent = difftime(time(NULL), game_start_time);
|
|
||||||
display_game_result(word, guessed, guessed_correctly, tries, timeout, score, time_spent);
|
|
||||||
free(word);
|
free(word);
|
||||||
return 0;
|
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 (guessed_correctly == strlen(word)) {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
156
rapportGprof.txt
156
rapportGprof.txt
@@ -1,156 +0,0 @@
|
|||||||
Flat profile:
|
|
||||||
|
|
||||||
Each sample counts as 0.01 seconds.
|
|
||||||
no time accumulated
|
|
||||||
|
|
||||||
% cumulative self self total
|
|
||||||
time seconds seconds calls Ts/call Ts/call name
|
|
||||||
0.00 0.00 0.00 6 0.00 0.00 display_hangman
|
|
||||||
0.00 0.00 0.00 6 0.00 0.00 process_guess
|
|
||||||
0.00 0.00 0.00 1 0.00 0.00 add_to_logs
|
|
||||||
0.00 0.00 0.00 1 0.00 0.00 generate_word
|
|
||||||
|
|
||||||
% the percentage of the total running time of the
|
|
||||||
time program used by this function.
|
|
||||||
|
|
||||||
cumulative a running sum of the number of seconds accounted
|
|
||||||
seconds for by this function and those listed above it.
|
|
||||||
|
|
||||||
self the number of seconds accounted for by this
|
|
||||||
seconds function alone. This is the major sort for this
|
|
||||||
listing.
|
|
||||||
|
|
||||||
calls the number of times this function was invoked, if
|
|
||||||
this function is profiled, else blank.
|
|
||||||
|
|
||||||
self the average number of milliseconds spent in this
|
|
||||||
ms/call function per call, if this function is profiled,
|
|
||||||
else blank.
|
|
||||||
|
|
||||||
total the average number of milliseconds spent in this
|
|
||||||
ms/call function and its descendents per call, if this
|
|
||||||
function is profiled, else blank.
|
|
||||||
|
|
||||||
name the name of the function. This is the minor sort
|
|
||||||
for this listing. The index shows the location of
|
|
||||||
the function in the gprof listing. If the index is
|
|
||||||
in parenthesis it shows where it would appear in
|
|
||||||
the gprof listing if it were to be printed.
|
|
||||||
|
|
||||||
Copyright (C) 2012-2024 Free Software Foundation, Inc.
|
|
||||||
|
|
||||||
Copying and distribution of this file, with or without modification,
|
|
||||||
are permitted in any medium without royalty provided the copyright
|
|
||||||
notice and this notice are preserved.
|
|
||||||
|
|
||||||
Call graph (explanation follows)
|
|
||||||
|
|
||||||
|
|
||||||
granularity: each sample hit covers 2 byte(s) no time propagated
|
|
||||||
|
|
||||||
index % time self children called name
|
|
||||||
0.00 0.00 6/6 main [10]
|
|
||||||
[1] 0.0 0.00 0.00 6 display_hangman [1]
|
|
||||||
-----------------------------------------------
|
|
||||||
0.00 0.00 6/6 main [10]
|
|
||||||
[2] 0.0 0.00 0.00 6 process_guess [2]
|
|
||||||
-----------------------------------------------
|
|
||||||
0.00 0.00 1/1 main [10]
|
|
||||||
[3] 0.0 0.00 0.00 1 add_to_logs [3]
|
|
||||||
-----------------------------------------------
|
|
||||||
0.00 0.00 1/1 main [10]
|
|
||||||
[4] 0.0 0.00 0.00 1 generate_word [4]
|
|
||||||
-----------------------------------------------
|
|
||||||
|
|
||||||
This table describes the call tree of the program, and was sorted by
|
|
||||||
the total amount of time spent in each function and its children.
|
|
||||||
|
|
||||||
Each entry in this table consists of several lines. The line with the
|
|
||||||
index number at the left hand margin lists the current function.
|
|
||||||
The lines above it list the functions that called this function,
|
|
||||||
and the lines below it list the functions this one called.
|
|
||||||
This line lists:
|
|
||||||
index A unique number given to each element of the table.
|
|
||||||
Index numbers are sorted numerically.
|
|
||||||
The index number is printed next to every function name so
|
|
||||||
it is easier to look up where the function is in the table.
|
|
||||||
|
|
||||||
% time This is the percentage of the `total' time that was spent
|
|
||||||
in this function and its children. Note that due to
|
|
||||||
different viewpoints, functions excluded by options, etc,
|
|
||||||
these numbers will NOT add up to 100%.
|
|
||||||
|
|
||||||
self This is the total amount of time spent in this function.
|
|
||||||
|
|
||||||
children This is the total amount of time propagated into this
|
|
||||||
function by its children.
|
|
||||||
|
|
||||||
called This is the number of times the function was called.
|
|
||||||
If the function called itself recursively, the number
|
|
||||||
only includes non-recursive calls, and is followed by
|
|
||||||
a `+' and the number of recursive calls.
|
|
||||||
|
|
||||||
name The name of the current function. The index number is
|
|
||||||
printed after it. If the function is a member of a
|
|
||||||
cycle, the cycle number is printed between the
|
|
||||||
function's name and the index number.
|
|
||||||
|
|
||||||
|
|
||||||
For the function's parents, the fields have the following meanings:
|
|
||||||
|
|
||||||
self This is the amount of time that was propagated directly
|
|
||||||
from the function into this parent.
|
|
||||||
|
|
||||||
children This is the amount of time that was propagated from
|
|
||||||
the function's children into this parent.
|
|
||||||
|
|
||||||
called This is the number of times this parent called the
|
|
||||||
function `/' the total number of times the function
|
|
||||||
was called. Recursive calls to the function are not
|
|
||||||
included in the number after the `/'.
|
|
||||||
|
|
||||||
name This is the name of the parent. The parent's index
|
|
||||||
number is printed after it. If the parent is a
|
|
||||||
member of a cycle, the cycle number is printed between
|
|
||||||
the name and the index number.
|
|
||||||
|
|
||||||
If the parents of the function cannot be determined, the word
|
|
||||||
`<spontaneous>' is printed in the `name' field, and all the other
|
|
||||||
fields are blank.
|
|
||||||
|
|
||||||
For the function's children, the fields have the following meanings:
|
|
||||||
|
|
||||||
self This is the amount of time that was propagated directly
|
|
||||||
from the child into the function.
|
|
||||||
|
|
||||||
children This is the amount of time that was propagated from the
|
|
||||||
child's children to the function.
|
|
||||||
|
|
||||||
called This is the number of times the function called
|
|
||||||
this child `/' the total number of times the child
|
|
||||||
was called. Recursive calls by the child are not
|
|
||||||
listed in the number after the `/'.
|
|
||||||
|
|
||||||
name This is the name of the child. The child's index
|
|
||||||
number is printed after it. If the child is a
|
|
||||||
member of a cycle, the cycle number is printed
|
|
||||||
between the name and the index number.
|
|
||||||
|
|
||||||
If there are any cycles (circles) in the call graph, there is an
|
|
||||||
entry for the cycle-as-a-whole. This entry shows who called the
|
|
||||||
cycle (as parents) and the members of the cycle (as children.)
|
|
||||||
The `+' recursive calls entry shows the number of function calls that
|
|
||||||
were internal to the cycle, and the calls entry for each member shows,
|
|
||||||
for that member, how many times it was called from other members of
|
|
||||||
the cycle.
|
|
||||||
|
|
||||||
Copyright (C) 2012-2024 Free Software Foundation, Inc.
|
|
||||||
|
|
||||||
Copying and distribution of this file, with or without modification,
|
|
||||||
are permitted in any medium without royalty provided the copyright
|
|
||||||
notice and this notice are preserved.
|
|
||||||
|
|
||||||
Index by function name
|
|
||||||
|
|
||||||
[3] add_to_logs [4] generate_word
|
|
||||||
[1] display_hangman [2] process_guess
|
|
Reference in New Issue
Block a user