From 8309e24cc7d6ca2886d427ce1015f282196a9bd4 Mon Sep 17 00:00:00 2001 From: nelet Date: Tue, 15 Oct 2024 10:36:25 +0200 Subject: [PATCH 1/2] chrono --- a.out | Bin 0 -> 16160 bytes pendu.c | 11 +++++++++++ 2 files changed, 11 insertions(+) create mode 100755 a.out diff --git a/a.out b/a.out new file mode 100755 index 0000000000000000000000000000000000000000..91c1674351b8fc8fd88fdb07debd21dc6233e7dc GIT binary patch literal 16160 zcmeHOeQX@X6`wmhU_ug`C}{!|vH?;hZGHK`gg}~d`M9t~2?>x;RV`gMcWZl_^PRcd zOC6e&2p5PkmRyi%K&VPvsWhk`DO4c>)Hrb(LKIbos;!Zz>VBZw<)dk#G*FuB@6Eoq zUN2`r{8OpDv3Gy-e)Ha&nYXjMbGx&5q}HvC$6`uSjry!2X}G~>l3cL&O3nb1YMGjf z{l)4cbr$4W&H3d?A5iM2q4;>LW4;DRcC#r{0YC23N>ExzNOlvYQ|I_4f{Ob=c9W=x zkCpHlx}+2%Y2mjHnQE&CBC01HuEYS$uc zG;FTDezS`2QvJk|MC(1RD}J@}iH=+& z=})GsXez$z|We1e*wIKEt--bcu0>cS`MjFy{e<+6_sTbJv+VCO1EvbT5LC~ zoKDwMp4;xIqF2Z|IcQ)Yrb(^fS?#u)Q^kUv%YaXJ+E$C3v$O8)j_N45Ij=<(-Bu7N zbc^})RaaTXw4G~FcGfPmEAm6*cd5E|UGu8d){?}PL9QvtElFGvmnf zt!~kC3Y*ri&gOH@CVNZPK`pKA`5ZUR@>MG;DuvOHkzRw5OMChwc@iU?12L2pfO6i> za^0zPcI{wphL&s9-7J?baB-!tQGG1`^cxRi$i>wokSWRaR<3h{=lT2)Ur&YK#e7@R zFBe|qT@k*{@bz{__>VE)7vbkJzbC>kV193eZ)E=a5q>%I`y+fa^9LjR2Il)CyvzLY z2%lqqFv6FZABymIFh3mOdzl}J@DDQIe~r?Kt`Brgk#s!7$AttAhWN=LekjCG3-QAt zzCOgOq%WKe;9H$0>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TQN>}4J-DDz&Z=oF3rZD47RDfTk2 z6ugA(r2V2N_%)Q%oK<9*w+e1#`)K=_R=vnjOvPU2&4My-minXp|0dh7Vuvyh7nFHB z+0Vijwf{Caii!9S?k9X-k>4j!(J6ZT9|0S-H`q?F-!}{Pa=Q|IG-5x@1%eAA6^flW z{9(ji=1qd~`%CmA?i9_RsQnUuWw0jWZ-N4!m-w-kdK#Iqz05lWn?#W@TK{tq`-V$> zMZsqx_R;!(iE=D1)aL-H%I`|~9y6*hvf%GvQy;RI-oGh!zg zhjG~c)}*g@>mncfdi-=P38JoJtz|U==mr1VT=q> z?W19_?@I0ibRDMSB<+#w=Q-e{?Y@P--^~N}j8w#h5fO9u%Jz5DjS1;v5rc@R{sUWS Blg|JE literal 0 HcmV?d00001 diff --git a/pendu.c b/pendu.c index 1b0cfd6..f06acbb 100644 --- a/pendu.c +++ b/pendu.c @@ -2,6 +2,8 @@ #include #include #include +#include +#include #define MAX_WORDS 14 #define MAX_TRIES 6 @@ -35,6 +37,11 @@ void display_hangman(int tries) { } } +void alarm_handler(int sig) { + printf("\nTrop tard ! Vous avez dépassé les 30 secondes.\n"); + exit(1); +} + int main() { srand(time(NULL)); const char *word = words[rand() % MAX_WORDS]; @@ -48,12 +55,16 @@ int main() { } guessed[word_length] = '\0'; + signal(SIGALRM, alarm_handler); + while (tries < MAX_TRIES && guessed_correctly < word_length) { printf("\nMot à deviner : %s\n", guessed); display_hangman(tries); char guess; + alarm(30); printf("Entrez une lettre : "); scanf(" %c", &guess); + alarm(0); int found = 0; for (int i = 0; i < word_length; i++) { From abd96239301205c648274a8ef74229158e991c80 Mon Sep 17 00:00:00 2001 From: nelet Date: Tue, 15 Oct 2024 11:19:32 +0200 Subject: [PATCH 2/2] ajout score --- a.out | Bin 16160 -> 16264 bytes pendu.c | 32 ++++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/a.out b/a.out index 91c1674351b8fc8fd88fdb07debd21dc6233e7dc..d7219b6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>XM9;?Xy3U>r;cqtwNRq$|$1b`>W%j6jjoQDU_M>QH2fsd-Zf7#AFyzu{#?c07G1#eFWkZGq z&~Nw|j;D$1DbbSy7y(b6*gc+1LJZ^}=dx8f}Kt&ioU9TVd) zE9NzRiUm`0&N~U2d&OzVe_Nd+ch_BV5h8aRrWvfd-IgN<4ez6o{7$6Jq{J=xUCeNl zBii?=_LU#PHtPbq7dotqN_HYe+RK|{U)=_rO60(`woEx#H->_evl9En4nAgR zB+=0Gs9~$hNrRe?9gnI`pi0Yq3y#L63oPJqX@bFZTz7({$nvIBmI2W+APuhkvVEBr z_JrJH8PE&0&F$HiflY&Q(95z!)}JBxq+yEQSoM9<1m-)z^6mF^ULfsypEN{$okIq1 zMo=0?-g0CCo-J+MzECSO%l0e$1+m~-w!L?7_m@0W#vWDrP&tye3|CA1j(MmvAiU zs|n51x!K*tZaw)oMcIvLyn&{O^d=rc#B+%Kh~Fbd5%D^!Dk@yqkLFJ@>DhFV!4ue+ ztJsX9M4-K>hMZe)ur`xj1&guuU=z^gHUS4Io#fN$wh9^PM!X+x znlOvJ2X{^vzk68e*>?bv%kyaln4HbD13b=rIs*Y`JvG8bXTCMTucma(n8e^O)cIkR zE1$N)OD+Wl1u?vPg-B@(-Y9j^ccHJeo~A>xv>YnRvK{Z?M4MnC89CeohhFs}4y*k) zYJXMj|4@4a+$ytE8+?KN5-2FQJ1Q_zCr;W_g(ZXy#kDT$|6X(OAS)JWE>k!h i?yIe*bU!5P6g+XsXR6J9{_fwWP*Pt-4?#=)Q~v-`Q;M4a diff --git a/pendu.c b/pendu.c index f06acbb..244a89c 100644 --- a/pendu.c +++ b/pendu.c @@ -36,19 +36,36 @@ void display_hangman(int tries) { case 6: printf(" ----\n | |\n | O\n | /|\\\n | / \\\n |\n--------\n"); break; } } - +// Fct qui fait sortir du prog après avoir passé 30sec void alarm_handler(int sig) { printf("\nTrop tard ! Vous avez dépassé les 30 secondes.\n"); exit(1); } +//FCT pour calculer le score en fonction du temps mis et des erreurs obtenus +int calculate_score(int mistakes, time_t start_time) { + time_t end_time = time(NULL); + int time_taken = (int)difftime(end_time, start_time); + int base_score = 1000 - (mistakes * 100) - time_taken; + return (base_score < 0) ? 0 : base_score; +} + +//FCT pour ajouter un bonus si le joueur trouve en moins de 3 essais +void apply_bonus(int *score, int tries) { + if (tries <= 3) { + *score += 500; + } +} + int main() { srand(time(NULL)); const char *word = words[rand() % MAX_WORDS]; int word_length = strlen(word); - char guessed[word_length]; + char guessed[word_length + 1]; int tries = 0; int guessed_correctly = 0; + int mistakes = 0; + int score = 0; for (int i = 0; i < word_length; i++) { guessed[i] = '_'; @@ -56,15 +73,16 @@ int main() { guessed[word_length] = '\0'; signal(SIGALRM, alarm_handler); + time_t start_time = time(NULL); // Début du chronomètre pour le score while (tries < MAX_TRIES && guessed_correctly < word_length) { printf("\nMot à deviner : %s\n", guessed); display_hangman(tries); char guess; - alarm(30); + alarm(30); //début du compteur des 30 sec a chaque tour printf("Entrez une lettre : "); scanf(" %c", &guess); - alarm(0); + alarm(0); //fin du compteur et sortie du programme via le signal int found = 0; for (int i = 0; i < word_length; i++) { @@ -79,15 +97,21 @@ int main() { if (!found) { tries++; + mistakes++; } } + score = calculate_score(mistakes, start_time); //calcul le score en fonction des erreurs et du temps écoulé + if (guessed_correctly == word_length) { + apply_bonus(&score, tries); //aplique le bonus de score si le nb d'essais <=3 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); } + printf("Votre score final est : %d\n", score); + return 0; }