Compare commits

..

4 Commits

Author SHA1 Message Date
44fe1a0f9a Readme 2024-10-25 19:31:16 +02:00
76bc9356bf Compéxité 2024-10-25 19:30:03 +02:00
67b7016376 Pendu 2024-10-25 18:18:43 +02:00
416e22c986 rendu du gprof sur le pendu. 2024-10-25 18:11:45 +02:00
4 changed files with 379 additions and 82 deletions

11
Readme.md Normal file
View File

@ -0,0 +1,11 @@
### É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 na été détecté, ce qui indique une bonne organisation des fonctions.

25
complexité.txt Normal file
View 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

269
pendu.c
View File

@ -1,82 +1,187 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_WORDS 14
#define MAX_TRIES 6
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 main() {
srand(time(NULL));
const char *word = words[rand() % MAX_WORDS];
int word_length = strlen(word);
char guessed[word_length];
int tries = 0;
int guessed_correctly = 0;
for (int i = 0; i < word_length; i++) {
guessed[i] = '_';
}
guessed[word_length] = '\0';
while (tries < MAX_TRIES && guessed_correctly < word_length) {
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_length; i++) {
if (word[i] == guess) {
if (guessed[i] == '_') {
guessed[i] = guess;
guessed_correctly++;
}
found = 1;
}
}
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);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <stdbool.h>
#define MAX_WORDS 17
#define MAX_TRIES 6
#define TIME_LIMIT 30 // Time limit for the game in seconds
const char *words[MAX_WORDS] = {
"programming", "computer", "language", "game", "algorithm",
"fontainebleau", "koala", "anticonstitutionally", "code",
"duck", "gyroscope", "endangerment", "whisper", "ecclesiastic",
"test", "yes", "no"
};
// Display hangman based on the number of incorrect tries
void display_hangman(int tries) {
const char *stages[] = {
" ----\n | |\n |\n |\n |\n |\n--------\n",
" ----\n | |\n | O\n |\n |\n |\n--------\n",
" ----\n | |\n | O\n | |\n |\n |\n--------\n",
" ----\n | |\n | O\n | /|\n |\n |\n--------\n",
" ----\n | |\n | O\n | /|\\\n |\n |\n--------\n",
" ----\n | |\n | O\n | /|\\\n | /\n |\n--------\n",
" ----\n | |\n | O\n | /|\\\n | / \\\n |\n--------\n"
};
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);
}
// 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));
constructed_word[0] = '\0';
int current_length = 0, attempts = 0;
while (current_length < target_length && attempts < 100) {
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) {
strcat(constructed_word, " ");
current_length++;
}
strcat(constructed_word, random_word);
current_length += word_length;
}
attempts++;
}
return attempts < 100 ? constructed_word : NULL;
}
// Process guessed letter and update game status
void process_guess(const char *word, char *guessed, char guess, int *guessed_correctly, int *score, int *tries) {
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) {
(*tries)++;
(*score) -= 5;
}
}
// Log the game's statistics to a file
void add_to_logs(const char *word, int tries, int guessed_correctly, bool won, char guessed[], double time_spent, int score) {
FILE *log_file = fopen("hangman.log", "a");
if (log_file == NULL) {
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
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
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) {
printf("\nWord to guess: %s\n", guessed);
display_hangman(tries);
if (check_timeout(game_start_time, TIME_LIMIT)) {
printf("Time's up! You exceeded %d seconds.\n", TIME_LIMIT);
printf("Sorry, you lost. The word was: %s\n", word);
display_hangman(MAX_TRIES);
timeout = 1;
break;
}
play_turn(word, guessed, &guessed_correctly, &score, &tries);
}
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 (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 Normal file
View File

@ -0,0 +1,156 @@
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