gpt4 book ai didi

c - (希望)我的刽子手游戏的最后一个循环,

转载 作者:行者123 更新时间:2023-11-30 20:49:16 29 4
gpt4 key购买 nike

对于我不断地用刽子手问题来打扰这个论坛,我深表歉意。我希望很快就能完成这件事。确实在这方面一直在努力。这是到目前为止我的代码:

#include <stdio.h>
#include <time.h>
#include <string.h>
#define MAX_WORD_LEN 20
#define MAX_TRIES 6
char* GetWordFromFile();
int HideWord(char* randomWordfromFile);
void PlayAgain();
void GameLogic(char *randomWordfromFile);
void clear_stdin();
void PlayGame();

int main()
{
printf("Welcome to Hangman. Would you like play? Please answer Y/N. \n");
int PlayorNot = getchar();
if (PlayorNot == 'Y' || PlayorNot == 'y')
PlayGame();
else
system("cls");
printf("Goodbye");

return 0;
}

void PlayGame()
{
char* word = GetWordFromFile();
GameLogic(word);
PlayAgain();
}





char* GetWordFromFile()
{
FILE* pointertoFile = NULL;
char randomWordfromFile[MAX_WORD_LEN];
int i = 0 , ran = 0;
srand(time(NULL));
pointertoFile = fopen("C:\\Users\\Aditya\\Dropbox\\Hangman\\hangman.txt" , "r+");
for(; fgets(randomWordfromFile , MAX_WORD_LEN, pointertoFile) ; i++)
;
ran = rand() % i;
rewind(pointertoFile);
for(i = 0 ; i < ran ; i++)
fgets(randomWordfromFile , MAX_WORD_LEN , pointertoFile);
return randomWordfromFile;
}



void GameLogic(char *randomWordfromFile)
{
int UserGuess;
int i, x;
int WrongGuesses;
size_t j;
size_t len = strlen(word);
char HiddenWord[len];
for (j = 0; j < len; ++j)
{
HiddenWord[i] = "_ ";
}
printf("%s", HiddenWord);


for(i = 0; i < MAX_TRIES; i++ )
{
printf("Go ahead and guess a letter\n");
clear_stdin();
scanf("%d", &UserGuess);


for (x = 0; x < len; x++)
{
if (randomWordfromFile[x] == UserGuess)
UserGuess = HiddenWord[x];
else
break;
}
}
printf("%s", randomWordfromFile);

}




void PlayAgain()
{
printf("Do you want to play again? Please answer with Yes/No?");
char PlayAgainAnswer = getchar();
if (PlayAgainAnswer == 'Y' || PlayAgainAnswer == 'y')
PlayGame();
else if (PlayAgainAnswer == 'N"' || PlayAgainAnswer == 'n')
printf("Thanks for playing");
else
printf("Thanks for playing");
}


void clear_stdin()
{
while( getchar() != '\n' ){;}
}

经过几天的工作和调试后,它终于编译了。由于某种原因,当我运行它打印出字母时,它不应该是这样。我相信它进入第一个循环询问用户是否想玩。回答"is"后,它会要求您猜一个字母。但在询问之前,它应该显示 _ _ _ 或类似内容,具体取决于单词中的字母数量。

我的猜测是问题出在以下循环中:

for (j = 0; j < len; ++j)
{
HiddenWord[i] = "_ ";
}
printf("%s", HiddenWord);

但是,我似乎无法在这个循环中找到问题,因为它是一个非常简单的循环。

最佳答案

您在循环中使用 j 并使用 i 作为索引,您希望它们相同

关于c - (希望)我的刽子手游戏的最后一个循环,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20998061/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com