我正在尝试使用 C 函数制作一个刽子手。我能够在不使用函数的情况下制作游戏,但现在当我尝试使用函数时遇到了一些问题。
我遇到的主要问题是我的displayWord()函数。我需要这个函数来打印出要屏幕的单词,并将所有未正确猜测的字母用下划线删除。
~头文件
#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("-");
}
}
}
最佳答案
首先要注意的事情:在 void displayWord(char* word, int* suggestedLetters, int* length)
中,您接受的是 int
名为 lenght
的指针,但在主代码中您传递的是普通的 int
。
第二件事,在 displayWord()
中,您有一个循环,其长度为 word
字符串的长度。因此,当长度为 8 时,循环会执行 8 次,因此它不会检查整个 guessedLetters[]
数组。
第三件事在 init(char *word)
函数中,您正在传递一个指针。在此函数的范围内,变量 word
是原始指针的副本,因此当您将其指向其他位置时,它不会产生任何效果。所以你可能想改变
word = words[randomIndex];
至
strcpy(word, words[randomIndex]);
现在给您一个解决方案的示例,您可以像这样更改代码:
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("-");
}
}
}
<强> SEE DEMO
我在这里所做的是查看“guessedLetters”数组以检查当前字符是否被猜到。我从当前字符中减去 97,因为这是十进制 ASCII 'a'
的值(因此当当前字符为 'a'
时,它将查看索引 0
)。
关于c - 在 C 语言中使用 Hangman 游戏函数时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31176267/