首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hangman程序帮助(C编程入门)

Hangman程序帮助(C编程入门)
EN

Stack Overflow用户
提问于 2010-11-23 14:22:17
回答 3查看 1.5K关注 0票数 0

我花了很多时间在这个程序上,这个程序是用来玩绞刑者游戏的。

任务如下:在这个修改后的Hangman游戏中,计算机选择一个秘密单词,玩家必须猜测单词中的字母。秘密单词显示为一系列的*(显示的*的数量表示单词中的字母数)。每次玩家猜测单词中的一个字母时,对应的*将被替换为到目前为止正确猜测的字母。当玩家正确猜测整个单词(玩家获胜)或玩家用完所有回合(玩家失败)时,游戏结束。玩家将被允许最多7次错误猜测。

我已经用它走了很长一段路,但我觉得我在几个基本的地方搞砸了。我正在尝试调试它,但每次传递函数'findChars‘时,我都会在主函数中遇到错误,因为它“在参数2中没有强制转换的情况下从整数中生成指针”。

我为所有的阅读道歉,但任何帮助都是很好的。谢谢。

<

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include <time.h> /* contains prototype for function time */
#define MAX 10


int findChars(char* gameWord[], char secretWord[], int length);

int main (void) {
const int numberOfWords = 20;
int length;
srand((unsigned)time(NULL)); //generate a random seed based on time so it's different every time
int ran = rand()% numberOfWords; //Generate a random number between 0 to numberOfWords - 1
char* dictionary[] = {"who", "lives", "in","a", "pineapple", "under","the", "sea", "absorbant",
            "and", "yellow", "porous","is", "he", "sponge","bob", "square","pants","crabby","patties"}; //array of word strings

printf("%s\n", dictionary[ran]);

printf("Welcome to HANGMAN.\n\n You will be asked to guess the computer's secret word. The word will be displayed as a number of *'s.\n Every time you guess a letter correctly, that letter will be shown in its correct position in the word.  \nIf you guess incorrectly, the number of tries you have left will be decremented.  \nYou will be given a maximum of 7 incorrect guesses.\n");

length=strlen(dictionary[ran]);
printf("%d\n", length);
char secretWord[MAX];
secretWord[length]=*dictionary[ran];

char* gameWord[length];

int i;
for (i=0; i<length; i++){
    gameWord[i]= "*";
}

for (i=0; i<length; i++){
    printf("%s", gameWord[i]);
}
printf("\n");
printf("7 turns left \nEnter a letter:    \n");

while(findChars(&gameWord[length], secretWord[length], length)!=0) {
    (findChars(&gameWord[length], secretWord[length], length));
}
    printf("6 turns left \nEnter a letter:    \n");

while(findChars(&gameWord[length], secretWord[length], length)!=0) {
    (findChars(&gameWord[length], secretWord[length], length));
}
    printf("5 turns left \nEnter a letter:    \n");

while(findChars(&gameWord[length], secretWord[length], length)!=0) {
    (findChars(&gameWord[length], secretWord[length], length));
}
    printf("4 turns left \nEnter a letter:    \n");

while(findChars(&gameWord[length], secretWord[length], length)!=0) {
    (findChars(&gameWord[length], secretWord[length], length));
}
    printf("3 turns left \nEnter a letter:    \n");

while(findChars(&gameWord[length], secretWord[length], length)!=0) {
    (findChars(&gameWord[length], secretWord[length], length));
}
    printf("2 turns left \nEnter a letter:    \n");

while(findChars(&gameWord[length], secretWord[length], length)!=0) {
    (findChars(&gameWord[length], secretWord[length], length));
}
    printf("1 turns left \nEnter a letter:    \n");

while(findChars(&gameWord[length], secretWord[length], length)!=0) {
    (findChars(&gameWord[length], secretWord[length], length));
}
    printf("Sorry, no more turns left. The secret word was ???");

return 0;
}

//PRE: findChar inputs the character we are looking for, the string we are looking in.
//POST: the function outputs the number of occurances of the said character in the said array

int findChars(char* gameWord[],char secretWord[], int length) {
int i;
char character[MAX];
    while((getchar()) != '\n'){
        character[0]=getchar();
        for (i=0; i<length; i++){
            if (secretWord[i]==character[0]){
                strncpy(gameWord[i],secretWord[i],1);
                for (i=0; i<length; i++){
                    printf("%s", gameWord[i]);
                    return 1;
                }
            }
            else
                return 0;
        }
    return 0;
    }
return 0;
}

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-11-23 14:48:37

尝试更改:

代码语言:javascript
复制
char secretWord[MAX];
secretWord[length]=*dictionary[ran];

代码语言:javascript
复制
char* gameWord[length];

代码语言:javascript
复制
char* secretWord = dictionary[ran];

代码语言:javascript
复制
char gameWord[length];

现在,您只需将dictionary[ran]的第一个字符赋值给secretWord中length位置的字符。

(如果要打印gameWord,还必须分配空间并为其设置一个空终止符)。

然后改变

代码语言:javascript
复制
findChars(&gameWord[length], secretWord[length], length)

至:

代码语言:javascript
复制
findChars(gameWord, secretWord, length)

因为您现在正在向一个需要char*的函数传递一个char。您还需要将findChars的签名更改为:

代码语言:javascript
复制
int findChars(char* gameWord, char* secretWord, int length)

还有很多其他的东西要反对,但这应该会让你开始使用。注意事项:

  • 将七个字符放入循环中不要使用strncpy(gameWord[i],secretWord[i],1);将一个字符串中的一个字符分配给formatting
  • The

-loop in findChars在第一次迭代中返回

票数 2
EN

Stack Overflow用户

发布于 2010-11-23 14:48:57

我认为在为findChars指定参数的方式上存在问题。

基本上,如果你有像findChars(&gameWord[length], secretWord[length], length)这样的调用,我认为你需要像findChars(gameWord, secretWord, length)这样的调用。

原型应该是..。

代码语言:javascript
复制
int findChars(char gameWord[], char secretWord[], int length);

或者..。

代码语言:javascript
复制
int findChars(char* gameWord, char* secretWord, int length);

而不是。

代码语言:javascript
复制
int findChars(char* gameWord[], char secretWord[], int length);

也就是说,gameWordsecretWord都应该作为数组或指针传递,但不能作为指针数组传递。

类似地,当您在main中声明gameWord时,它应该是一个字符数组--而不是指向字符的指针数组……

代码语言:javascript
复制
char gameWord [length+1];

我有点担心这一点,因为length是一个变量,但我认为这没问题-我的C语言有点过时,我的本能做法是使用编译时常量表达式来声明数组大小(这是您可能需要的最大大小)。

顺便说一句-注意+1。一个C字符串有一个额外的字符--一个空的结束符--它不会被strlen计算在内--但是在声明char-array变量时,你必须考虑到这一点。

票数 0
EN

Stack Overflow用户

发布于 2010-11-23 14:52:17

尝试将函数的声明方式与函数的调用方式进行比较,以便理解错误消息。

findChars()函数的声明方式如下:

代码语言:javascript
复制
int findChars(char* gameWord[], char secretWord[], int length);

并且像这样从main()中被调用:

代码语言:javascript
复制
findChars(&gameWord[length], secretWord[length], length)

特别要注意的是,第二个参数被声明为一个char数组(一个字符串),而您向它传递的是单个字符。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4253320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档