在C中使用函数进行Hangman游戏时遇到麻烦

时间:2015-07-02 05:31:57

标签: c function

我正在尝试使用C函数制作一个刽子手。我能够在不使用功能的情况下制作游戏,但现在当我尝试使用功能时,我遇到了一些问题。

我遇到的主要问题是我的displayWord()函数。我需要这个功能打印出一个字到屏幕,所有字母都没有被正确猜到用下划线消隐。

~Header FiIles

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

/* Constants. */
#define NUM_WORDS 50
#define ALPHABET_SIZE 26
#define GOOD_GUESS 0
#define BAD_GUESS 1
#define GAME_OVER 1
#define GAME_CONTINUE 0
#define MAX_GUESS 10
#define MAX_WORD_LEN 10

/* Function prototypes. */
int init(char* word);
void displayWord(char* word, int* guessedLetters, int* length);
int guessLetter(char* word, int* guessedLetters);
void displayHangman(unsigned wrongGuesses);
int isGameOver(char* word, int* guessedLetters, unsigned wrongGuesses);
void readRestOfLine();

〜主要代码

#include "hangman.h"

/****************************************************************************
* Function main() is the entry point for the program.
****************************************************************************/
int main(void)
{
   char word[MAX_WORD_LEN + 1];
   unsigned wrongGuesses = 0;
   int guessedLetters[ALPHABET_SIZE] = {
      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
   };
   int length = 0;

   int numLives = 10;
   int numCorrect = 0;
   int oldCorrect = 0;
   int lengthOfWord = 0;
   length = init(word);
   printf("length: %d\n", length);
   displayWord(word, guessedLetters, length);



   return EXIT_SUCCESS;
}


int init(char* word)
{
   srand(time(NULL));
   int randomIndex = rand() % 50;

   const char* words[NUM_WORDS] = {
      "array",      "auto",       "break",      "case",       "cast",
      "character",  "comment",    "compiler",   "constant",   "continue",
      "default",    "double",     "dynamic",    "else",       "enum",
      "expression", "extern",     "file",       "float",      "function",
      "goto",       "heap",       "identifier", "library",    "linker",
      "long",       "macro",      "operand",    "operator",   "pointer",
      "prototype",  "recursion",  "register",   "return",     "short",
      "signed",     "sizeof",     "stack",      "statement",  "static",
      "string",     "struct",     "switch",     "typedef",    "union",
      "unsigned",   "variable",   "void",       "volatile",   "while"
   };

    word = words[randomIndex];
    int lengthOfWord = strlen(words[randomIndex]);
    printf("Word: %s\n", words[randomIndex]);

    return lengthOfWord;



}

void displayWord(char* word, int* guessedLetters, int* length)
{

    int loopIndex;
    char gameWord[][1] = word;


    for ( loopIndex = 0; loopIndex < length; loopIndex++ ) {

        if(letterGuessed[loopIndex] == 1) {
            printf("%c",gameWord[0][loopIndex]);
        } else {
            printf("-");
        }

    }
}

我不知道如何扫描我的随机单词以打印出已经猜到的字母。我尝试了几种不同的方式来阅读网络和观看视频,但我真的不确定。请帮忙。

问题代码

void displayWord(char* word, int* guessedLetters, int* length)
{

    int loopIndex;
    char gameWord[][1] = word;


    for ( loopIndex = 0; loopIndex < length; loopIndex++ ) {

        if(letterGuessed[loopIndex] == 1) {
            printf("%c",gameWord[0][loopIndex]);
        } else {
            printf("-");
        }

    }
}

2 个答案:

答案 0 :(得分:1)

要注意的第一件事:在void displayWord(char* word, int* guessedLetters, int* length)中,您接受了一个名为int的{​​{1}}指针,但在您的主要代码中,您传递的是正常的lenght {1}}。

第二件事,在int中你有一个长度为displayWord()字符串长度的循环。所以当长度为8时,你的循环会变为8次,因此它不会检查整个word数组。

你的guessedLetters[]函数中的

第三件事,你正在传递一个指针。在此函数的范围内,变量init(char *word)原始指针的副本,因此当您将其指向其他位置时,它没有任何效果。所以你可能想要改变

word

 word = words[randomIndex];

现在给你一个解决方案的例子,你可以改变你的代码:

 strcpy(word, words[randomIndex]);

<强> SEE DEMO

我在这里做的是查看void displayWord(char *word, int *guessedLetters, int length) { int i; for (i = 0; i < length; i++ ){ if(guessedLetters[word[i] - 97] == 1){ printf("%c", word[i]); }else{ printf("-"); } } } 数组以检查当前字符是否被猜到。我从当前字符中减去了97,因为这是guessedLetters的十进制ASCII值(因此,当前字符为'a'时,它将查看索引'a')。

答案 1 :(得分:0)

重写这样的代码:

  • 有一个字符串“guess”,与答案字符串一样大。
  • 最初,用空格填充猜测字符串。
  • 当用户猜出一个字母时,遍历答案字符串并在那里查找字母。
  • 如果找到该信件,请用找到的信件更新guess[i]。用户赢得“回合”。
  • 如果在答案字符串中找不到该字母,则该用户将失去“圆形”。
  • 当猜测比较等于答案时,游戏就会解决。
  • 您不一定需要保存所有用户猜测的字母,除非您出于其他目的需要它们,例如显示猜测或防止相同的猜测两次。
相关问题