Advertisement
Guest User

Pendu

a guest
Jan 24th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <time.h>
  6. #define TAILLE_MAX 30
  7.  
  8. char lireCaractere();
  9. int ifTabEgal(char tab1[], char tab2[], int longeur);
  10. int testLetreInMot(char mot[], char mot2[], char lettre, int longueur);
  11. void findMotSecret(char tab[]);
  12. void suprSpaceEnd(char tab[]);
  13.  
  14. int main()
  15. {
  16. char motSecret[TAILLE_MAX];
  17. char* motEnCoursDevinage = NULL;
  18. int nombreVies = 10, continuerPartie = 1;
  19. srand(time(NULL));
  20.  
  21. while(continuerPartie == 1){
  22.  
  23. findMotSecret(motSecret);
  24.  
  25. suprSpaceEnd(motSecret);
  26.  
  27. motEnCoursDevinage = NULL;
  28. nombreVies = 10;
  29.  
  30. motEnCoursDevinage = malloc((strlen(motSecret)+1) * sizeof(char));
  31. if(motEnCoursDevinage == NULL){
  32.  
  33. exit(0);
  34. }
  35.  
  36.  
  37. for(int i=0 ; i<strlen(motSecret) ; i++){
  38. motEnCoursDevinage[i] = '*';
  39. printf("%c ", motEnCoursDevinage[i]);
  40. }
  41. motEnCoursDevinage[strlen(motSecret)] = '\0';
  42.  
  43. testLetreInMot(motSecret, motEnCoursDevinage, '\n', strlen(motSecret));
  44.  
  45. while(ifTabEgal(motSecret, motEnCoursDevinage, strlen(motEnCoursDevinage)) != 1 && nombreVies > 0){
  46. printf("Voici le mot : ");
  47. for(int i=0 ; i<strlen(motSecret) ; i++){
  48. printf("%c ", motEnCoursDevinage[i]);
  49. }
  50. printf("\nQuelle lettre ? \n");
  51.  
  52. switch(testLetreInMot(motSecret, motEnCoursDevinage, lireCaractere(), strlen(motSecret))){
  53. case 0:
  54. nombreVies--;
  55. printf("Faux\nVous avez perdu une vie. Vous n'avez plus que %d vies.\n", nombreVies);
  56. break;
  57. case 1:
  58. printf("Vrai\n");
  59. break;
  60. default :
  61. printf("Bug !\n");
  62. break;
  63. }
  64. }
  65.  
  66. printf("Le mot etait ");
  67. for(int i=0 ; i<strlen(motSecret) ; i++){
  68. printf("%c ", motSecret[i]);
  69. }
  70. printf(".\n");
  71.  
  72. if(ifTabEgal(motSecret, motEnCoursDevinage, strlen(motEnCoursDevinage)) == 1){
  73. printf("Bravo, vous avez trouve !\n\n");
  74. }
  75. else if(nombreVies == 0){
  76. printf("Vous etes bien con.\n\n");
  77. }
  78. else{
  79. printf("Tu fais chier a faire tout bugger.\n\n");
  80. }
  81. free(motEnCoursDevinage);
  82.  
  83. printf("Veux tu faire une autre partie de ce super jeu ? \n(ca boostera un peu ta matiere grise, toi qui en a besoin)\n1 ) Oui\n2 ) Non\n");
  84. switch(lireCaractere()){
  85. case '1':
  86. printf("Bien, bien...\n\n");
  87. continuerPartie = 1;
  88. break;
  89. case '2':
  90. printf("Quoi ?\nJ'espere juste que ta raison est valable...\n\n");
  91. continuerPartie = 0;
  92. break;
  93. default:
  94. printf("T'est un peu con, il fallait mettre le chiffre qui correspondant a ton choix.\nJe te relance donc une partie.\n\n");
  95. continuerPartie = 1;
  96. break;
  97. }
  98.  
  99. }
  100.  
  101. return 0;
  102. }
  103.  
  104. int ifTabEgal(char tab1[], char tab2[], int longeur){
  105. int bolIfTabEgal = 1;
  106.  
  107. for(int i=0 ; i<longeur ; i++){
  108. if(tab1[i] != tab2[i]){
  109. bolIfTabEgal = 0;
  110. }
  111. }
  112. return bolIfTabEgal;
  113. }
  114.  
  115. char lireCaractere(){
  116. char caractere = 0;
  117.  
  118. caractere = getchar(); // On lit le premier caractère
  119. caractere = toupper(caractere); // On met la lettre en majuscule si elle ne l'est pas déjà
  120.  
  121. // On lit les autres caractères mémorisés un à un jusqu'au \n (pour les effacer)
  122. while (getchar() != '\n') ;
  123.  
  124. return caractere; // On retourne le premier caractère qu'on a lu
  125. }
  126.  
  127. int testLetreInMot(char mot[], char mot2[], char lettre, int longueur){
  128. int bolLettreInMot = 0;
  129.  
  130. for(int i=0 ; (i<longueur || lettre == '\0') ; i++){
  131. if(lettre == mot[i]){
  132. mot2[i] = mot[i];
  133. bolLettreInMot = 1;
  134. }
  135. }
  136. return bolLettreInMot;
  137. }
  138.  
  139. void findMotSecret(char tab[]){
  140. int nbLignes = 0, nbPif = 0;
  141. FILE* fichier = NULL;
  142.  
  143. fichier = fopen("mots.txt", "r");
  144.  
  145. if (fichier != NULL){
  146.  
  147. while (fgets(tab, TAILLE_MAX, fichier) != NULL){
  148. nbLignes++;
  149. }
  150.  
  151. fseek(fichier, 0, SEEK_SET);
  152.  
  153. nbPif = (rand() % nbLignes) + 1;
  154.  
  155. for(int i=0 ; i<nbPif ; i++){
  156. fgets(tab, TAILLE_MAX, fichier);
  157. }
  158.  
  159. fclose(fichier);
  160.  
  161.  
  162. }
  163. else{
  164. printf("Impossible d'ouvrir le fichier mots.txt");
  165. }
  166. }
  167.  
  168. void suprSpaceEnd(char tab[]){
  169. for(int i=0 ; i<strlen(tab) ; i++){
  170. if(tab[i] == ' ' && tab[i] == '\n'){
  171. tab[i] = '\0';
  172. }
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement