gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:16:01 25 4
gpt4 key购买 nike

用 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


}

最佳答案

你有很多问题。

不太严重的问题:

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这是一个返回 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 )你比较charint* 。这毫无意义。可能应该是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_GUESSBAD_GUESS来自guessLetter ,但切勿使用返回值。您不需要从此函数返回任何内容。只需将 0 以外的某个值分配给 guessLetters 中的该位置即可。因此删除返回语句并更改

    int guessLetter(char* word, int* guessedLetters)

    void guessLetter(char* word, int* guessedLetters)

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

关于c - 在 C 中传递变量 - 制作一个刽子手游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31238832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com