在C中传递变量 - 制作刽子手游戏

时间:2015-07-06 06:15:20

标签: c

在C中制作一个简单的刽子手游戏。我有Java经验,我遇到了guessLetter方法的问题。 我正在尝试确定用户输入的字符是否是单词中的字符。虽然如果您发现任何其他问题或对我有任何建议,所有人都表示赞赏!!

    // Includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hangman.h"

//Function Declarations
void getRandomWord(char* word);

// Global Variable Declarations
int wordLength = 0;



//Main Function
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
   };





   srand( time(NULL));
   getRandomWord( word );
   displayWord( word, wordLength );
   guessLetter( word, guessedLetters);








   return EXIT_SUCCESS;
}


//Gets Random Word from Words Array
void getRandomWord(char* word)
{
   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"
   };


    int randomNumber = rand() % NUM_WORDS;
    printf("Random Number is: %d\n", randomNumber);
    wordLength = strlen(words[randomNumber]);
    printf("Word Length is: %d\nWord is: %s\n", wordLength, words[randomNumber]);
    word = words[randomNumber];
    return word;



}


//Prints Word Formatted with dashes
void displayWord(char* word, int* guessedLetters)
{

    int x = 0;
    printf("%s \n", word);


    for (x = 0; x <= wordLength * 2; x++)
    {
        printf("=");
    }
    printf("\n|");

    for(x = 0; x < wordLength; x++)
    {

        if ( word[x] != guessedLetters )
        {



            printf("_|");
        }
        else
        {
            printf(guessedLetters);
        }
    }
    printf("\n");

     for (x = 0; x <= wordLength * 2; x++)
    {
        printf("=");
    }


}


//User Makes Guess, Determines whether Guess is Correct or Incorrect
int guessLetter(char* word, int* guessedLetters)
{
    char firstGuess;
    int x;
    //puts(word); will print an @ and another strange symbol! This is where my problem is!

    printf("\nEnter Your Guess: ");
    scanf("%d\n", firstGuess);

    for(x = 0; x < wordLength; x++)
    {
        if( firstGuess == word[x])
        {
            printf("Correct Guess!!");
            return GOOD_GUESS;
        }
        else
        {
            printf("Incorrect");
            return BAD_GUESS;
        }
    }

// Once a correct Guess is made that character will be added to the guessedLetters Array


}

3 个答案:

答案 0 :(得分:1)

你有很多问题。

不太严重的问题:

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 guessedLetters[ALPHABET_SIZE] = {0};

严重问题:

  • 您使用word = words[randomNumber];。这只是更改了本地指针word,并使其指向words[randomNumber]的字符串文字指针。您可能需要按字符复制每个字符。这可以通过使用strcpy中的string.h函数来完成:

    strcpy(word, words[randomNumber]);
    
  • return word;来自getRandomWord的{​​{1}},这是一个返回void的函数(不返回任何内容)。这是错的。删除return word;
  • 您使用displayWord( word, wordLength );作为wordLength来调用int。但是

    void displayWord(char* word, int* guessedLetters)
    

    期望int*作为其第二个参数。由于wordLength是全局的,因此您无需传递它。 displayWord无法在guessedLetters中看到main,因为它是本地的displayWord。将其传递给wordLength,而不是传递displayWord( word, guessedLetters );

    if ( word[x] != guessedLetters )
  • char int*您将if ( guessedLetters[x] == 0 )printf(guessedLetters);进行比较。这毫无意义。它应该是int*
  • 您使用printf并将const char*作为第一个参数传递给printf("%c", word[x]);,其中scanf("%d\n", firstGuess);作为其第一个参数。它应该是scanf(" %c", &firstGuess);
  • 由于@SouravGhosh's answer中提到的原因,
  • GOOD_GUESS应为BAD_GUESS
  • 您从guessLetter返回guessLettersint guessLetter(char* word, int* guessedLetters) ,但从不使用返回值。您无需从此功能返回任何内容。只需在void guessLetter(char* word, int* guessedLetters) 中为该位置指定0以外的值。因此,删除两个return语句并更改

    if( firstGuess == word[x])
    {
        printf("Correct Guess!!");
        return GOOD_GUESS;
    }
    else
    {
        printf("Incorrect");
        return BAD_GUESS;
    }
    

    if( firstGuess == word[x])
    {
        printf("Correct Guess!!");
        guessedLetters[x] = 1;     //Change the value of the current position
    }
    else
    {
        printf("Incorrect");
    }
    

    void getRandomWord(char* word);
    

    void getRandomWord(char* word);
    void displayWord(char* word, int* guessedLetters)
    void guessLetter(char* word, int* guessedLetters)
    
  • 您只声明一个函数:

    // Includes
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "hangman.h"
    
    //Function Declarations
    void getRandomWord(char* word);
    void displayWord(char* word, int* guessedLetters)
    void guessLetter(char* word, int* guessedLetters)
    
    // Global Variable Declarations
    int wordLength = 0;
    
    int main(void)
    {
       char word[MAX_WORD_LEN + 1];
       unsigned wrongGuesses = 0;
       int guessedLetters[ALPHABET_SIZE] = {0};
    
       srand( time(NULL));
    
       getRandomWord( word );
       displayWord( word, guessedLetters );
       guessLetter( word, guessedLetters);
    
       return EXIT_SUCCESS;
    }
    
    
    //Gets Random Word from Words Array
    void getRandomWord(char* word)
    {
        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"
        };
    
        int randomNumber = rand() % NUM_WORDS;
        printf("Random Number is: %d\n", randomNumber);
        wordLength = strlen(words[randomNumber]);
        printf("Word Length is: %d\nWord is: %s\n", wordLength, words[randomNumber]);
        strcpy(word, words[randomNumber]);
    }
    
    
    //Prints Word Formatted with dashes
    void displayWord(char* word, int* guessedLetters)
    {
    
        int x = 0;
        //printf("%s \n", word); Why print it here? No need for this
    
    
        for (x = 0; x <= wordLength * 2; x++)
        {
            printf("=");
        }
        printf("\n|");
    
        for(x = 0; x < wordLength; x++)
        {
    
            if ( guessedLetters[x] == 0 )
            {
                printf("_|");
            }
            else
            {
                printf("%c", word[x]);
            }
        }
        printf("\n");
    
        for (x = 0; x <= wordLength * 2; x++)
        {
            printf("=");
        }
    }
    
    
    //User Makes Guess, Determines whether Guess is Correct or Incorrect
    void guessLetter(char* word, int* guessedLetters)
    {
        char firstGuess;
        int x;
    
        printf("\nEnter Your Guess: ");
        scanf(" %c", &firstGuess);
    
        for(x = 0; x < wordLength; x++)
        {
            if( firstGuess == word[x])
            {
                printf("Correct Guess!!");
                guessedLetters[x] = 1;     //Change the value of the current position
            }
            else
            {
                printf("Incorrect");
            }
        }
    }
    

    剩下的呢?使用:

    function numberToWords(value) {
      var numbers = value.split("");
      var decs =;["zero","one","two","three","four","five","six","seven","eight","nine"]
      var tens = ["ten","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"];
      var s="";
      for(var i=0;i<numbers.length;i++)
      {
        var ri=numbers.lenght-i; //reverse index
        if (ri==0) s+= dec[ri];
        else if (ri==1) s+= tens[ri];
        else if (ri==2) s+= dec[ri]+"-hundred";
        s+="-";
      }
      return s;
    }
    

固定代码:(未经测试):

numberToWords(28)

答案 1 :(得分:0)

在你的情况下,初看,

scanf("%d\n", firstGuess);

错了。您使用了错误的转换说明符。它应该是

scanf(" %c", &firstGuess);   //notice ALL the chnages
  • 第1点:您需要使用%c来阅读char
  • 第2点:您的输入应完全与提供的格式字符串匹配。我相信,您不需要格式字符串中有\n
  • 第3点:%c格式说明符期望相应的参数为指针char(因为字段宽度默认为1
  • 第4点:在之前注意空格%c,在预期的非空白字符之前,吃掉任何可能的空格都会有所帮助输入

答案 2 :(得分:0)

至于确定单词中是否存在字母/字符,您可以使用strchr() function

if(strchr (word,firstGuess)){
    printf("Correct Guess!!");
    flag_guess_correct = 1;     //flag is set when the guessed character is present in word
}