void Game()
{
char WORDS[20][20] = { "Apple","Banana","Cucumber","Fish","Garlic","Ham","Icecream","Jellies","Pig","Them","Media","Social","Refuge" ,"Headsman","Program",""};
//就是这个地方的单词库
int num;
printf("Enter the number of words to guess (More than 5 less than 20):");
scanf("%d", &num);
int count[20];
int i;
srand(time(NULL));
//The srand function is a time function, which is equivalent to setting a random time seed for random word selection.
for (i = 0; i < num; i++)
{
count[i] = strlen(WORDS[i]);
printf("%s\n", WORDS[i]);
}
printf("\n");
//Here, count counts the length of each word, while this section uses the previous two-dimensional array question bank output as a prompt.
char again = 'y';
//Set a loop condition to keep the player playing.
while (again == 'y') {
int x = rand() % (num);
int life = 5;
char guess[20];
printf(" +------------------+\n");
printf(" | |\n");
printf(" | |\n");
for (i = 0; i < 10; i++)
{
printf(" |\n");
}
printf("================================\n");
//Draw a gallows, personally feel, simple graphics can be directly printf output, for loop would complicate the problem.
printf("Word:");
char xiao[20];
for (i = 0; i < count[x]; i++)
{
xiao[i] = '_';
}
for (i = 0; i < count[x]; i++)
{
printf(" %c", xiao[i]);
}
printf("\n\n");
//Draw small bars according to the length of the words.
while (life > 0)
{
printf("Guess (%d lives): ", life);
scanf("%s", guess);
printf("\n");
if (strlen(guess) != 1)
//When the player has already guessed the word.
{
if (strlen(guess) == strlen(WORDS[x]))
{
int judge = 0;
for (i = 0; i < count[x]; i++)
{
if (guess[i] != WORDS[x][i])
{
break;
}
judge++;
}
if (judge == count[x])
{
printf("Correct!\n");
printf("Do you want to play again (y/n)? ");
scanf(" %c", &again);
break;
}
}
printf("Word:");
for (i = 0; i < count[x]; i++)
{
printf(" %c", xiao[i]);
}
printf("\n\n");
}
else
//When the player has to guess one by one
{
printf("Word:");
if (strstr(WORDS[x], guess) != NULL)
//The STRSTR function will determine if the next string is identical to the previous string and return NULL if it is not.
{
for (i = 0; i < count[x]; i++)
{
if (guess[0] == (char)WORDS[x][i])
{
xiao[i] = guess[0];
}
}
int correct = 0;
for (i = 0; i < count[x]; i++)
{
if (xiao[i] != '_')
{
correct++;
continue;
}
break;
}
for (i = 0; i < count[x]; i++)
{
printf(" %c", xiao[i]);
}
printf("\n\n");
if (correct == count[x]) {
printf("Correct!\n");
printf("Do you want to play again? ");
scanf("%c", &again);
break;
}
}
else
{
for (i = 0; i < count[x]; i++)
{
printf(" %c", xiao[i]);
}
printf("\n\n");
life--;
}
//The following is a step by step drawing of the little man.
if (life == 4)
{
printf(" +------------------+\n");
printf(" | |\n");
printf(" | |\n");
printf(" | O\n");
for (i = 0; i < 9; i++)
{
printf(" |\n");
}
printf("================================\n");
}
if (life == 3)
{
printf(" +------------------+\n");
printf(" | |\n");
printf(" | |\n");
printf(" | O\n");
printf(" | |\n");
for (i = 0; i < 8; i++)
{
printf(" |\n");
}
printf("================================\n");
}
if (life == 2)
{
printf(" +------------------+\n");
printf(" | |\n");
printf(" | |\n");
printf(" | O\n");
printf(" | /|\n");
for (i = 0; i < 8; i++)
{
printf(" |\n");
}
printf("================================\n");
}
if (life == 1)
{
printf(" +------------------+\n");
printf(" | |\n");
printf(" | |\n");
printf(" | O\n");
printf(" | /|\\ \n");
for (i = 0; i < 8; i++)
{
printf(" |\n");
}
printf("================================\n");
}
}
}
if (life == 0) {
printf(" +------------------+\n");
printf(" | |\n");
printf(" | |\n");
printf(" | O\n");
printf(" | /|\\ \n");
printf(" | / \\ \n");
for (i = 0; i < 7; i++)
{
printf(" |\n");
}
printf("================================\n");
printf("You have no more lives!\n");
printf("Do you want to play again (y/n)? ");
}
scanf(" %c", &again);
}
}