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

标签 c

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

#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/

相关文章:

c - 获取相邻设备的接收信号强度

c - 查找字符串中整数的长度

c - 在同一个程序中启动 TCP 客户端和服务器(在 c 中)

c - 输入字母字符时,Tic-Tac-Toe 用户输入无休止地循环

c - 出现错误时安全退出到特定状态

c - 立即结束模拟 C

检查两个数组中的共同值并将不同的值放入第三个数组中

c - 访问内存区域时出现段错误

c - 以太网帧中字段的原始 C 类型

c - 从堆栈中弹出,程序无限循环