我正在尝试使用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("-");
}
}
}
答案 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[i]
。用户赢得“回合”。