【问题标题】:Can't terminate the for loop when winning a word guessing game [closed]赢得猜字游戏时无法终止for循环[关闭]
【发布时间】:2019-01-06 20:21:21
【问题描述】:

我为一个猜字游戏编写了代码;

  1. 代码从单词文件中随机抽取一个单词,在文件中,每行有“1”个单词。

  2. 然后代码将该单词转换为星号并在代码运行时显示。

  3. 它提示运行程序的人输入一个字母。如果字母正确,则对应于该字母的星号将成为该字母。如果不是,程序会提示您再次猜测。

尝试次数由用户在程序运行时设置。该程序称为 wordguess,因此在命令提示符下使用 "wordguess filename.txt n" 运行。

filename.txt 是指包含可供选择的单词的文件。 n 是指在输掉游戏之前允许用户尝试的次数。如果这个人在选择的尝试次数内没有猜出单词,我可以让游戏结束,但如果这个人赢了,我似乎无法让游戏结束。如果单词被正确猜到,循环仍将继续,直到该人猜完为止。我不确定在循环上放置什么条件以促进胜利。我尝试了各种 if 语句,但是当我尝试时代码没有运行。

    //taking random word from file
    srand(time(0));
    f = fopen(argv[1], "r");
    while (!feof(f))
    {
        fgets(word, 1024, f);
        word[strcspn(word, "\n")] = '\0';
        nL++;
    }
    randomline = rand() % nL;
    fseek(f, 0, SEEK_SET);
    for (i = 0; !feof(f) && i <= randomline; i++)
        fgets(word, 1024, f);
        word[strcspn(word, "\n")] = '\0';
    strcpy(astword, word); //copies word to another char
    for (i = 0; i < strlen(astword); i++)
    {
        astword[i] = '*'; //copy is converted to asterisks
    }
    int counter = 0;
    for (i = 0; i < tries; i++)
    {
        printf("%s \n", astword);
        printf("guess a letter\n");
        scanf(" %c", &guess);
        for (i = 0; i < tries; i++)
{
    printf("%s \n", astword);
    printf("guess a letter\n");
    scanf(" %c", &guess);

    for (j = 0; j < tries; j++)
    {
        if (guess == word[j])
            astword[j] = guess;
    }   
}

我是否遗漏了一些明显的东西?

编辑:根据 Ozan 的回答,我已经修改了我的循环,但它似乎不起作用;

        for (j = 0; j < tries; j++)
        {

            if (guess == word[j]) 
            {
                astword[j] = guess;
                counter++; 
            }

            if (counter == strlen(astword))
            {
                break;
            }
        }

【问题讨论】:

  • “我是否遗漏了一些明显的东西?”不幸的是,是的。但你并不孤单。绝大多数学习编程的人都遇到与您完全相同的问题:您不知道如何调试。调试对于编程至关重要。没有它你就无法前进。您必须花时间学习如何调试。然后它会节省你无数小时盯着你的程序想知道它为什么不工作。您需要调试自己的程序,至少可以显着缩小问题范围,并且可以提出比“我的程序不起作用”更具体的问题
  • 另外,您的段落是一堵文字墙。请注意这一点并组织您的陈述。
  • 以防万一我没被理解:您显然缺少的是您需要调试程序。
  • 我同意,在阅读了您的评论后,我明白需要留出时间来学习如何调试。谢谢。
  • fseek 在具有可变长度单词的文本文件中是可疑的。建议你把整篇文章读入记忆,然后分解成碎片?

标签: c loops break


【解决方案1】:

如果我正确理解了您的问题,您只想找到正确的逻辑来终止您的 for 循环。如果您想在用户猜对所有字母时终止循环,您可以通过计算您的程序做了多少 asterisk->letter 替换以及当计数达到您的单词的字母,那么是时候终止循环了,因为这意味着没有更多的星号可以猜测,并且单词的所有缺失字母都被正确猜测了..

在您程序的以下代码块中(发生替换的地方);

for (j = 0; j < tries; j++){
    if (guess == word[j])
      astword[j] = guess;
}   

只需添加一个计数器来计算星号(*)字母替换的次数;

for (j = 0; j < tries; j++){
  if (guess == word[j]){
    astword[j] = guess;
    counter++; // int variable, initialized to 0 before for loop starts
  }
}

并且在外层for循环的开始处,添加一个if语句来检查counter是否等于strlen(astword)

if (counter == strlen(astword))
      break; // if the replacement action is occured with the same number of word's lenght, break the loop.

所以作为上述代码的组合,你的for循环必须是这样处理中断情况的;

for (i = 0; i < tries; i++){
    if (counter == strlen(astword))
      break;
    printf("%s \n", astword);
    printf("guess a letter\n");
    scanf(" %c", &guess);

    for (j = 0; j < tries; j++)
    {
        if (guess == word[j]){
            astword[j] = guess;
            counter++;
        }
    }   
}

【讨论】:

  • 我已经尝试了您的建议并修改了问题中的代码,但我仍然无法打破循环。我是否错误地听从了您的建议?
  • 你有 2 个错误,你应该为if (guess == word[j]) 开括号。而且if (counter == strlen(astword)) 必须在 for 循环内部,而不是外部。检查我答案的最后一部分,有代码的工作形式。
  • 我再次尝试并再次编辑了问题。我还是发现循环没有坏。
  • 哦,我的错,我读错了你的代码,请检查我答案的最后一部分,我编辑了它。
猜你喜欢
  • 1970-01-01
  • 2021-05-24
  • 1970-01-01
  • 2021-04-29
  • 1970-01-01
  • 1970-01-01
  • 2019-09-09
  • 2021-11-02
  • 2021-08-14
相关资源
最近更新 更多