C语言猜测游戏

时间:2014-09-30 18:10:23

标签: c

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

#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);
      }
   }
}

2 个答案:

答案 0 :(得分:2)

您的代码存在多个问题。我会试着指出它们

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语句将字符变量与数组的基址

进行比较

答案 1 :(得分:0)

这是您希望程序拥有的基本框架。它不能完美地工作(我甚至不能保证它能编译),但是它应该为你提供如何改进代码的想法。

#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.");
}