Hangman程序字符串问题

时间:2014-07-19 04:12:53

标签: c arrays string

我正在进行C编程入门课程,我们被分配编写了一个Hangman程序。

在游戏中,计算机随机选择一个单词并显示它有多少个字母。用户必须通过输入他们认为可能在单词中的字母来猜测单词。此外,用户只有六次机会才能正确理解这个词。每次错误的猜测都会完成被绞死的图片。该程序需要有一个主菜单,其中包含三个选项(新游戏得分退出)。该计划还需要具备以下三个功能:

  1. selectWord随机选择单词(我在这个函数中创建了一串单词)。
  2. drawMan画出刽子手。
  3. checkWord检查输入是否正确并用正确的字母替换破折号。
  4. 当我运行游戏而不是向破折号显示虚线应该只是说(null)的行时,会出现问题。但图片仍然显示。

    我对可能造成这种情况的原因感到困惑。以下是我到目前为止的情况:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    char selectWord(char []);
    int drawMan(int);
    void checkWord(char, char [], char [], int, int);
    
    int main()
    {
        int menuSelect;
        int chances = 0;
        char word[13];
        int length;
        int score;
    
        do
        {
            printf("\n\t\t\tThe Hangman Game v1.0 has booted\n\n");
            printf("\t\t[1]\t Create new game\n");
            printf("\t\t[2]\t View Score\n");
            printf("\t\t[3]\t Exit game\n");
    
            printf("Please enter a number from the menu: ");
            scanf("%d", &menuSelect);
    
            switch(menuSelect)
            {
                case 1:
    
                    selectWord(word);
                    length = strlen(word);
    
                    char dash[20];    //Will create dashes considering the length of the selected word
                    int dashCount;
                    int letterTry; 
                    int wordMatch = 0;
    
                    for(dashCount = 0; dashCount < length; dashCount++)
                    {
                        dash[dashCount] = '-';
                    }
                    dash[dashCount] = '\0';
    
                    while(wordMatch != 1)
                    {
    
                        drawMan(chances);
    
                        printf("\n%s", dash[dashCount]);
                        printf("\n\nPlease enter a letter: ");
                        fflush(NULL);
    
                        while(letterTry != '\n')
                        {
                            letterTry = getchar();
                        }
                        letterTry = getchar();
    
                        if(strcmp(dash, word) == 0)
                        {
                            printf("\nThat is correct!\n");
                            wordMatch = 1;
                            score++;
                        }
                    }   
    
                    break;
    
                case 2:
    
                    printf("The score is: %d", score); 
                    break;
    
    
                case 3:
    
                    printf("Thank you for playing!");
                    break;
    
            }       
        }while(menuSelect != 3);    
    }
    
    char selectWord(char word[])
    {
        int index;
        char list[65][13]={{"sailboat"}, {"school"}, {"rug"}, {"chalk"}, {"tusk"}, {"camera"}, {"pajamas"}, {"cello"}, {"cake"}, {"beehive"}, {"gate"}, {"swing"}, {"lighthouse"}, {"toe"}, {"newspaper"}, {"stingray"}, {"brain"}, {"french"}, {"purse"}, {"soda"}, {"money"}, {"dryer"}, {"scale"}, {"clam"}, {"knee"}, {"artist"}, {"stomach"}, {"ticket"}, {"face"}, {"TV"}, {"can"}, {"blowfish"}, {"popsicle"}, {"beaver"}, {"snowball"}, {"dollar"}, {"sprinkler"}, {"hair"}, {"bathroom"}, {"rain"}, {"skirt"}, {"beach"}, {"suitcase"}, {"garden"}, {"doormat"}, {"pretzel"}, {"watering"}, {"fur"}, {"birthday"}, {"fries"}, {"owl"}, {"chin"}, {"spool"}, {"mushroom"}, {"peach"}, {"pelican"}, {"pineapple"}, {"flagpole"}, {"doghouse"}, {"boot"}, {"flute"}, {"quilt"}, {"hook"}, {"rainbow"}, {"happy"}};
    
        srand(time(NULL));
        index = rand()%65;
    
        strcpy(word, list[index]);
    
        return word;
    }
    
    int drawMan(int chances)
    {
        if(chances == 6)
        {
            printf("\n\n");
            printf("\n         +-------+");
            printf("\n         |       |");
            printf("\n         |        ");
            printf("\n         |        ");
            printf("\n         |");
            printf("\n         |");
            printf("\n        /|\\\n\n");
        }
        else if(chances == 5)
        {
            printf("\n\n");
            printf("\n         +-------+");
            printf("\n         |       |");
            printf("\n         |       O");
            printf("\n         |       |");
            printf("\n         |");
            printf("\n         |");
            printf("\n        /|\\\n\n");
        }
        else if(chances == 4)
        {
            printf("\n\n");
            printf("\n         +-------+");
            printf("\n         |       |");
            printf("\n         |       O");
            printf("\n         |      /|");
            printf("\n         |        ");
            printf("\n         |        ");
            printf("\n        /|\\\n\n");
        }
        else if(chances == 3)
        {
            printf("\n\n");
            printf("\n         +-------+");
            printf("\n         |       |");
            printf("\n         |       O");
            printf("\n         |      /|\\");
            printf("\n         |        ");
            printf("\n         |        ");
            printf("\n        /|\\\n\n");
        }
        else if(chances == 2)
        {
            printf("\n\n");
            printf("\n         +-------+");
            printf("\n         |       |");
            printf("\n         |       O");
            printf("\n         |      /|\\");
            printf("\n         |        ");
            printf("\n         |        ");
            printf("\n        /|\\\n\n");
        }
        else if(chances == 1)
        {
            printf("\n\n");
            printf("\n         +-------+");
            printf("\n         |       |");
            printf("\n         |       O");
            printf("\n         |      /|\\");
            printf("\n         |      /");
            printf("\n         |        ");
            printf("\n        /|\\\n\n");
        }
        else if(chances == 0)
        {
            printf("\n\n");
            printf("\n         +-------+");
            printf("\n         |       |");
            printf("\n         |       O");
            printf("\n         |      /|\\");
            printf("\n         |      / \\");
            printf("\n         |        ");
            printf("\n        /|\\\n\n");
            printf("\n\n\t You have lost!");
        }
    }
    void checkWord(char ltrTry, char word[], char dash[], int length, int chances)
    {
        int count;
        int correct = 0;    // 0 is incorrect 1 is correct
    
        for(count = 0; count < length; count++)
        {
            if(ltrTry == word[count])
            {
                dash[count] = word[count];
                correct = 1;            
            }
        }
    }
    

    更新#1:谢谢大家对破折号字符串的修复。在破折号数组中添加空字符可修复破折号问题。我在我的主函数中添加了一个新变量到案例1,名为&#34; wordMatch&#34;并使其成为while循环的控制变量,因为它可以使单词正确并退出循环而不会耗尽所有机会。但似乎出现了一个新的问题。当选择新游戏时,刽子手会显示两次,输入错误的字母时,机会的数量不会改变,刽子手的形象也不会改变(给我无限的玩法)。但是,一旦我正确猜出这个词,循环就会正确退出。为什么会发生这种情况?

    更新#2:我已更正代码并使其正确执行。现在唯一的问题似乎是案例1没有中断,因为它在letter环中陷入了while循环。

1 个答案:

答案 0 :(得分:3)

实际上有两个问题。分配短划线后,请确保使用\0终止字符串,如下所示:

for(dashCount = 0; dashCount < length; dashCount++)
{
   dash[dashCount] = '-';
}
dash[dashCount] = '\0';

在while循环中,打印:

printf("\n%s", dash);

而不是:

printf("\n%s", dash[dashCount]);

此外,每次尝试后都不会更新chances值。您可以通过使checkWord函数返回correct并根据该值更新机会计数来执行此操作,如下所示:

int checkWord(char ltrTry, char word[], char dash[], int length, int trys)
{
   ...

   return correct;
}

在循环中,不要只是调用函数,而是执行以下操作:

if(!checkWord(letterTry, word, dash, length, chances))
{
   chances++;
}

我能看到的另一个问题是在读取letterTry值时。在使用letterTry函数之后读取字符(在本例中为scanf)时,\n字符将存储在变量中。然后程序不会提示您输入其他内容。在你的情况下,玩家将无缘无故地失去一次机会。解决此问题的最简单方法是执行以下操作:

while(letterTry != '\n')
   letterTry = getchar();

并且,一旦玩家得到正确答案,就从主循环中断开。

if(strcmp(dash, word) == 0)
   {
      printf("You Won!");
      score++;
      break;
   }

正确理解上述解决方案后,请仔细阅读以下解决方案:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char selectWord(char[]);
int drawMan(int);
int checkWord(char, char[], char[], int);

int main()
{
  int menuSelect;
  int chances = 0;
  char word[13];
  int length;
  int score;

  do
    {
      printf("\n\t\t\tThe Hangman Game v1.0 has booted\n\n");
      printf("\t\t[1]\t Create new game\n");
      printf("\t\t[2]\t View Score\n");
      printf("\t\t[3]\t Exit game\n");

      printf("Please enter a number from the menu: ");
      scanf("%d", &menuSelect);

      switch(menuSelect)
        {
        case 1:
          selectWord(word);
          length = strlen(word);

          char dash[20];    //Will create dashes considering the length of the selected word
          int dashCount;
          int letterTry; 

          for(dashCount = 0; dashCount < length; dashCount++)
            {
              dash[dashCount] = '-';
            }
          dash[dashCount] = '\0';

          chances = 0;
          while(chances != 6)
            {
              drawMan(chances);
              printf("\n%s\n", dash);
              printf("chances = %d\n", chances);
              printf("\n\nPlease enter a letter: ");
              fflush(NULL);
              //scanf("%c%c", &letterTry, &letterTry);
              while(letterTry != '\n')
                letterTry = getchar();
              letterTry = getchar();
              if(!checkWord(letterTry, word, dash, length))
                {
                  chances++;
                }
              if(strcmp(dash, word) == 0)
                {
                  printf("You Won!");
                  score++;
                  break;
                }
            }   
          break;
        case 2:
          printf("The score is: %d", score); 
          break;
        case 3:
          printf("Thank you for playing!");
          break;
        }       
    }while(menuSelect != 3);    
}

char selectWord(char word[])
{
  int index;
  char list[65][13]={{"sailboat"}, {"school"}, {"rug"}, {"chalk"}, {"tusk"}, {"camera"}, {"pajamas"}, {"cello"}, {"cake"}, {"beehive"}, {"gate"}, {"swing"}, {"lighthouse"}, {"toe"}, {"newspaper"}, {"stingray"}, {"brain"}, {"french"}, {"purse"}, {"soda"}, {"money"}, {"dryer"}, {"scale"}, {"clam"}, {"knee"}, {"artist"}, {"stomach"}, {"ticket"}, {"face"}, {"TV"}, {"can"}, {"blowfish"}, {"popsicle"}, {"beaver"}, {"snowball"}, {"dollar"}, {"sprinkler"}, {"hair"}, {"bathroom"}, {"rain"}, {"skirt"}, {"beach"}, {"suitcase"}, {"garden"}, {"doormat"}, {"pretzel"}, {"watering"}, {"fur"}, {"birthday"}, {"fries"}, {"owl"}, {"chin"}, {"spool"}, {"mushroom"}, {"peach"}, {"pelican"}, {"pineapple"}, {"flagpole"}, {"doghouse"}, {"boot"}, {"flute"}, {"quilt"}, {"hook"}, {"rainbow"}, {"happy"}};

  srand(time(NULL));
  index = rand()%65;

  strcpy(word, list[index]);

  return word;
}

int drawMan(int chances)
{
  if(chances == 0)
    {
      printf("\n\n");
      printf("\n         +-------+");
      printf("\n         |       |");
      printf("\n         |        ");
      printf("\n         |        ");
      printf("\n         |");
      printf("\n         |");
      printf("\n        /|\\\n\n");
    }
  else if(chances == 1)
    {
      printf("\n\n");
      printf("\n         +-------+");
      printf("\n         |       |");
      printf("\n         |       O");
      printf("\n         |       |");
      printf("\n         |");
      printf("\n         |");
      printf("\n        /|\\\n\n");
    }
  else if(chances == 2)
    {
      printf("\n\n");
      printf("\n         +-------+");
      printf("\n         |       |");
      printf("\n         |       O");
      printf("\n         |      /|");
      printf("\n         |        ");
      printf("\n         |        ");
      printf("\n        /|\\\n\n");
    }
  else if(chances == 3)
    {
      printf("\n\n");
      printf("\n         +-------+");
      printf("\n         |       |");
      printf("\n         |       O");
      printf("\n         |      /|\\");
      printf("\n         |        ");
      printf("\n         |        ");
      printf("\n        /|\\\n\n");
    }
  else if(chances == 4)
    {
      printf("\n\n");
      printf("\n         +-------+");
      printf("\n         |       |");
      printf("\n         |       O");
      printf("\n         |      /|\\");
      printf("\n         |        ");
      printf("\n         |        ");
      printf("\n        /|\\\n\n");
    }
  else if(chances == 5)
    {
      printf("\n\n");
      printf("\n         +-------+");
      printf("\n         |       |");
      printf("\n         |       O");
      printf("\n         |      /|\\");
      printf("\n         |      /");
      printf("\n         |        ");
      printf("\n        /|\\\n\n");
    }
  else if(chances == 6)
    {
      printf("\n\n");
      printf("\n         +-------+");
      printf("\n         |       |");
      printf("\n         |       O");
      printf("\n         |      /|\\");
      printf("\n         |      / \\");
      printf("\n         |        ");
      printf("\n        /|\\\n\n");
      printf("\n\n\t You have lost!");
    }
  printf("print complete; exiting successfully");
}

int checkWord(char ltrTry, char word[], char dash[], int length)
{
  int count;
  int correct = 0;    // 0 is incorrect 1 is correct

  for(count = 0; count < length; count++)
    {
      if(ltrTry == word[count])
        {
          dash[count] = word[count];
          correct = 1;            
        }
    }

  /*  if(correct == 0)
      {
      trys--;
      } */
  return correct;
}
相关问题