1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| #include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#pragma warning(disable : 4996)
// ========== HEADER ==========
char EnterChar(void);
int SizeCalculator(char *word);
char* SizeAllocatorTxt(const int sizeTab);
void WordHider(char mysteryWord[], char hiddenWord[]);
int WordComparator(char *compared, char comparator, char *hiddenWord);
void MysteryWordGame(char *mysteryWord);
// ========== MAIN ==========
int main()
{
char mysteryWord[25] = "TEST";
MysteryWordGame(mysteryWord);
return 0;
}
// ========== SECONDARY ==========
char EnterChar(void)
{
char character = 0;
while (1)
{
puts("Entrez un lettre: ");
char entry[50 + 1];
fgets(entry, 50 + 1, stdin);
if (sscanf(entry, "%c\n", &character) == 1)
{
character = toupper(character);
break;
}
puts("! Entree incorrecte - Veuillez entrer une lettre. Recommencez.\n");
}
return character;
}
int SizeCalculator(char *word)
{
int iteration = 0;
for (int i = 0; word[i] != '\0'; i++)
{
iteration++;
}
return iteration;
}
char* SizeAllocatorTxt(const int sizeTab)
{
return (char *)malloc(sizeof(char) * (sizeTab + 1));
}
void WordHider(char mysteryWord[], char hiddenWord[])
{
for (int i = 0; mysteryWord[i] != '\0'; i++)
{
hiddenWord[i] = '*';
}
}
int WordComparator(char *compared, char comparator, char *hiddenWord)
{
int similarity = 0;
for (int i = 0; compared[i] != '\0'; i++)
{
if (compared[i] == comparator)
{
hiddenWord[i] = comparator;
similarity = 1;
}
}
// if similarity is false (0), return false (0), else return true(1)
if (similarity = 0) return 0;
else return 1;
}
void MysteryWordGame(char *mysteryWord)
{
int numbLife = 10, sizeWord = 0;
char *hiddenWord = NULL;
//calculate the size of the mystery word
sizeWord = SizeCalculator(mysteryWord);
//allocate memory for hidden word
hiddenWord = SizeAllocatorTxt(sizeWord);
//hidde the mystery word
WordHider(mysteryWord, hiddenWord);
//gameloop
while (1)
{
printf("> Il vous reste %d coups a jouer.\n", numbLife);
printf("> Le mot secret est : %s \n", *hiddenWord);
char characterEnter = EnterChar();
if (WordComparator(mysteryWord, characterEnter, hiddenWord) == 0) numbLife--;
if (numbLife == 0)
{
puts("You Lose!\n");
break;
}
if (*mysteryWord == *hiddenWord)
{
puts("You win!\n");
break;
}
}
free(hiddenWord);
} |
Partager