【问题标题】:Word guessing game in CC语言猜字游戏
【发布时间】:2014-11-25 11:09:20
【问题描述】:

我在编写程序时遇到了一些问题。该程序本质上是猜词游戏刽子手。我有一个词,用户有很多机会猜到这个词。我无法让游戏正确循环并在猜测次数用完时结束。另外,我创建了第二个数组,其大小与我的单词数组相同,但这个数组填充了破折号。如果一个字母猜对了,我需要正确的字母出现在破折号周围。如果有人可以提供帮助,我将不胜感激。

#include <stdio.h>   // Input and output operations
#include <stdlib.h>  // General purpose functions

int main(void)
{
   FILE*fp;          // File pointer variable
   fp = fopen("Quiz 6", "w");

   int numTries, tries, i;  // Declaration of variables
   char guess;
   char myWord [6] = {'a','p','p','l','e','\0'};  // Initialization of variables
   char dashedArray [6] = {'-','-','-','-','-','-'};

   numTries = 0;
   i=0;
   tries = 0;

   printf("\nLet's play a game! The objective of the game is to guess my secret word. You will have
          five chances to guess.\n");
   printf("\nLet's get started!\n");
   printf("\nPlease enter a letter: ");
   scanf("%c", &guess);

   for (i = 0; i < 4; i++)
   {
      numTries = numTries +1;
      if (guess == myWord [0])
      {
         printf("\n %c----", myWord [0]);
         printf("\nGood Guess!\n");
         printf("\nPlease guess another letter: ");
         scanf(" %c", &guess);
      }

      if ( guess == myWord [1] && myWord [1] )
      {
         printf("\n-%c%c--", myWord [1], myWord [1]);
         printf("\nGood Guess!\n");
         printf("\nPlease guess another letter: ");
         scanf(" %c", &guess);
      }

      if ( guess == myWord [3] )
      {
         printf("\n---%c-", myWord [3]);
         printf("\nGood Guess!\n");
         printf("\nPlease guess another letter: ");
         scanf(" %c", &guess);
      }

      if ( guess == myWord [4] )
      {
         printf("\n----%c", myWord [4]);
         printf("\nGood Guess!\n");
         printf("\nPlease guess another letter: ");
         scanf(" %c", &guess);
      }

      if ( if word is completed and guessed correctly )
      {
         printf("\nCongrats! You guessed the secret word!\n");
      }

      else if ( guess != myWord )
      {
         printf("\nSorry. That is not a correct letter. Please guess again:\n");
         scanf(" %c", &guess);
      }
   }
}

【问题讨论】:

  • 你忘了问问题。您需要什么帮助?
  • 是的,此页面上唯一的问号 (?) 是 @DavidSchwartz 的 ...
  • @DavidSchwartz:我认为他向他的代码寻求帮助。他提到的工作不正常
  • 他可能想查看 ncurses 库以获得一些帮助,如果您猜对了,那么如果您不知道它是什么,请阅读 man invisible-island.net/ncurses/man/ncurses.3x.html
  • @ralph 对,但是什么有帮助吗?他需要帮助修复编译器错误吗?他需要帮助找出要使用的算法吗?他的代码做错了吗?如果是这样,他希望它做什么?他在观察什么?

标签: c


【解决方案1】:

您的代码有几个问题。我会尽量指出他们

numTries = numTries + 1;

你正在使用这个变量,在每次迭代中递增它,但没有在任何地方使用它。

if (guess == myWord [0])
{
   printf("\n %c----", myWord [0]);
   printf("\nGood Guess!\n");
   printf("\nPlease guess another letter: ");
   scanf(" %c", &guess);
}

您正在将猜测的字符与单词的不同字符进行比较,但无论如何都不记得正确猜测了哪些字母或多少个字母。 (可能为此使用标志变量)

if ( if word is completed and guessed correctly )
{
   printf("\nCongrats! You guessed the secret word!\n");
}

上面的 if 循环在 c 语言中没有意义

else if ( guess != myWord )
{
   printf("\nSorry. That is not a correct letter. Please guess again:\n");
   scanf(" %c", &guess);
}

上面的 elseif 语句将字符变量与数组的基地址进行比较

【讨论】:

    【解决方案2】:

    这是您希望程序拥有的基本框架。它不能完美运行(我什至不能保证它可以编译),但它应该可以为您提供有关如何改进代码的想法。

    #include <stdio.h>
    
    void main()
    {
        char guess, newline;
        char myWord [6] = {'a','p','p','l','e','\0'};               // Initialization of variables
        char dashedArray [6] = {'-','-','-','-','-','\0'};
        int totalTries = 5;
        int currentTries = 0;
        int i, j;
    
        printf("\nLet's play a game! The objective of the game is to guess my secret word. You will have five chances to guess.\n");
        printf("\nLet's get started!\n");
    
        for (i=0; i<totalTries; i++)
        {
            printf("\nPlease enter a letter: ");
            scanf("%c\n", &guess);
    
            for (j=0; j<6; j++)
            {
                if (guess == myWord[j])
                    dashedArray[j] = guess;
            }
    
            printf("Result: %s", dashedArray);
    
        }
    
        printf("You lose.");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      • 2018-02-07
      • 1970-01-01
      相关资源
      最近更新 更多