【问题标题】:Hangman Program Help (Intro to C Programming)Hangman 程序帮助(C 编程简介)
【发布时间】:2011-05-14 06:59:00
【问题描述】:

我花了很多时间开发这个应该用来玩刽子手游戏的程序。

作业如下: 在这个经过修改的刽子手游戏中,计算机选择一个秘密单词,玩家必须猜出单词中的字母。秘密词显示为一系列 * (显示的 * 数表示单词中的字母数)。每次玩家猜出单词中的一个字母时,对应的 * 都会被替换为目前为止猜对的字母。当玩家正确猜出整个单词(玩家获胜)或玩家用完所有回合(玩家失败)时,游戏结束。玩家最多可以猜错 7 次。

我已经走了很远,但感觉我在几个基本的地方搞砸了。我正在尝试调试它,但是每次我传递函数“findChars”时都无法超越主函数中出现错误的部分,说它“从整数中生成指针,而没有在参数 2 中进行强制转换。”

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

#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;
}

>

【问题讨论】:

  • 如果你想让random()函数工作,那么你必须给它一个种子。此外,secretWord[length]=*dictionary[ran];有用吗(!?)
  • @gcc - 这就是他对srand((unsigned)time(NULL)); 行所做的事情吗?
  • 不是 100% 确定你想问什么,是的,据我所知,这部分有效。
  • 您不是将数组的索引传递给需要该数组的函数吗?您正在取消引用指针(因此得到一个“整数”)并将其传递给一个需要指针的函数。

标签: c debugging pointers


【解决方案1】:

尝试改变:

char secretWord[MAX];
secretWord[length]=*dictionary[ran];

char* gameWord[length];

char* secretWord = dictionary[ran];

char gameWord[length];

现在你唯一将dictionary[ran]的第一个字符分配给secretWord中length位置的字符。

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

然后改变

findChars(&gameWord[length], secretWord[length], length)

到:

findChars(gameWord, secretWord, length)

正如您现在将char 传递给期望char* 的函数。您还需要将findChars 的签名更改为:

int findChars(char* gameWord, char* secretWord, int length)

还有很多要反对的其他事情,但这应该可以帮助您入门。看点:

  • 将七个while(findChars... 放入一个循环中
  • 不要使用strncpy(gameWord[i],secretWord[i],1); 将一个字符从一个字符串分配给另一个
  • printf 格式不正确
  • findChars 中的 for (i=0; i&lt;length; i++){ 循环在第一次迭代中返回

【讨论】:

    【解决方案2】:

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

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

    原型是……

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

    或者...

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

    而不是……

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

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

    同样,当您在main 中声明gameWord 时,它应该是一个字符数组——而不是一个指向字符的指针数组...

    char gameWord [length+1];
    

    我有点担心,因为length 是一个变量,但我认为没关系 - 我的 C 有点过时了,我的自动操作是使用编译时常量表达式(最大您可能需要的尺寸)。

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

    【讨论】:

      【解决方案3】:

      尝试比较函数的声明方式和函数的调用方式,以了解错误消息。

      findChars() 函数是这样声明的:

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

      并像这样从 main() 调用:

      findChars(&gameWord[length], secretWord[length], length)
      

      请特别注意,第二个参数被声明为 char 数组(字符串),而您将单个字符传递给它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多