Hangman程序帮助(C编程简介)

时间:2010-11-23 06:22:17

标签: c debugging pointers

我花了很多时间研究这个应该玩刽子手游戏的程序。

作业如下: 在这个改良的Hangman游戏中,计算机选择一个秘密词,玩家必须猜出单词中的字母。秘密字显示为一系列*(显示*的数字表示单词中的字母数)。每次玩家猜到单词中的一个字母时,相应的*将被正确猜到的字母替换。当玩家正确猜出整个单词(玩家获胜)或玩家用完所有轮次(玩家输了)时,游戏结束。玩家将被允许最多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;
}

&GT;

3 个答案:

答案 0 :(得分:2)

尝试更改:

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格式错误
  • for (i=0; i<length; i++){中的findChars - 循环在第一次迭代中返回

答案 1 :(得分:0)

我认为如何为findChars指定参数存在问题。

基本上,如果你有findChars(&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都应该作为数组或指针传递,而不是作为 指针的数组传递。

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

char gameWord [length+1];

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

BTW - 请注意+1。 C字符串有一个额外的字符 - 一个空的终止符 - 它不被strlen计算 - 但是在声明char-array变量时你必须允许这样做。

答案 2 :(得分:0)

尝试比较如何声明函数以及如何调用函数,以便理解错误消息。

findChars()函数的声明如下:

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

从main()调用,如下所示:

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

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