Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <time.h>
- #define TAILLE_MAX 30
- char lireCaractere();
- int ifTabEgal(char tab1[], char tab2[], int longeur);
- int testLetreInMot(char mot[], char mot2[], char lettre, int longueur);
- void findMotSecret(char tab[]);
- void suprSpaceEnd(char tab[]);
- int main()
- {
- char motSecret[TAILLE_MAX];
- char* motEnCoursDevinage = NULL;
- int nombreVies = 10, continuerPartie = 1;
- srand(time(NULL));
- while(continuerPartie == 1){
- findMotSecret(motSecret);
- suprSpaceEnd(motSecret);
- motEnCoursDevinage = NULL;
- nombreVies = 10;
- motEnCoursDevinage = malloc((strlen(motSecret)+1) * sizeof(char));
- if(motEnCoursDevinage == NULL){
- exit(0);
- }
- for(int i=0 ; i<strlen(motSecret) ; i++){
- motEnCoursDevinage[i] = '*';
- printf("%c ", motEnCoursDevinage[i]);
- }
- motEnCoursDevinage[strlen(motSecret)] = '\0';
- testLetreInMot(motSecret, motEnCoursDevinage, '\n', strlen(motSecret));
- while(ifTabEgal(motSecret, motEnCoursDevinage, strlen(motEnCoursDevinage)) != 1 && nombreVies > 0){
- printf("Voici le mot : ");
- for(int i=0 ; i<strlen(motSecret) ; i++){
- printf("%c ", motEnCoursDevinage[i]);
- }
- printf("\nQuelle lettre ? \n");
- switch(testLetreInMot(motSecret, motEnCoursDevinage, lireCaractere(), strlen(motSecret))){
- case 0:
- nombreVies--;
- printf("Faux\nVous avez perdu une vie. Vous n'avez plus que %d vies.\n", nombreVies);
- break;
- case 1:
- printf("Vrai\n");
- break;
- default :
- printf("Bug !\n");
- break;
- }
- }
- printf("Le mot etait ");
- for(int i=0 ; i<strlen(motSecret) ; i++){
- printf("%c ", motSecret[i]);
- }
- printf(".\n");
- if(ifTabEgal(motSecret, motEnCoursDevinage, strlen(motEnCoursDevinage)) == 1){
- printf("Bravo, vous avez trouve !\n\n");
- }
- else if(nombreVies == 0){
- printf("Vous etes bien con.\n\n");
- }
- else{
- printf("Tu fais chier a faire tout bugger.\n\n");
- }
- free(motEnCoursDevinage);
- 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");
- switch(lireCaractere()){
- case '1':
- printf("Bien, bien...\n\n");
- continuerPartie = 1;
- break;
- case '2':
- printf("Quoi ?\nJ'espere juste que ta raison est valable...\n\n");
- continuerPartie = 0;
- break;
- default:
- printf("T'est un peu con, il fallait mettre le chiffre qui correspondant a ton choix.\nJe te relance donc une partie.\n\n");
- continuerPartie = 1;
- break;
- }
- }
- return 0;
- }
- int ifTabEgal(char tab1[], char tab2[], int longeur){
- int bolIfTabEgal = 1;
- for(int i=0 ; i<longeur ; i++){
- if(tab1[i] != tab2[i]){
- bolIfTabEgal = 0;
- }
- }
- return bolIfTabEgal;
- }
- char lireCaractere(){
- char caractere = 0;
- caractere = getchar(); // On lit le premier caractère
- caractere = toupper(caractere); // On met la lettre en majuscule si elle ne l'est pas déjà
- // On lit les autres caractères mémorisés un à un jusqu'au \n (pour les effacer)
- while (getchar() != '\n') ;
- return caractere; // On retourne le premier caractère qu'on a lu
- }
- int testLetreInMot(char mot[], char mot2[], char lettre, int longueur){
- int bolLettreInMot = 0;
- for(int i=0 ; (i<longueur || lettre == '\0') ; i++){
- if(lettre == mot[i]){
- mot2[i] = mot[i];
- bolLettreInMot = 1;
- }
- }
- return bolLettreInMot;
- }
- void findMotSecret(char tab[]){
- int nbLignes = 0, nbPif = 0;
- FILE* fichier = NULL;
- fichier = fopen("mots.txt", "r");
- if (fichier != NULL){
- while (fgets(tab, TAILLE_MAX, fichier) != NULL){
- nbLignes++;
- }
- fseek(fichier, 0, SEEK_SET);
- nbPif = (rand() % nbLignes) + 1;
- for(int i=0 ; i<nbPif ; i++){
- fgets(tab, TAILLE_MAX, fichier);
- }
- fclose(fichier);
- }
- else{
- printf("Impossible d'ouvrir le fichier mots.txt");
- }
- }
- void suprSpaceEnd(char tab[]){
- for(int i=0 ; i<strlen(tab) ; i++){
- if(tab[i] == ' ' && tab[i] == '\n'){
- tab[i] = '\0';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement