我花了很多时间在这个程序上,这个程序是用来玩绞刑者游戏的。
任务如下:在这个修改后的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;
}发布于 2010-11-23 14:48:37
尝试更改:
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)还有很多其他的东西要反对,但这应该会让你开始使用。注意事项:
strncpy(gameWord[i],secretWord[i],1);将一个字符串中的一个字符分配给formatting-loop in findChars在第一次迭代中返回
发布于 2010-11-23 14:48:57
我认为在为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);也就是说,gameWord和secretWord都应该作为数组或指针传递,但不能作为指针数组传递。
类似地,当您在main中声明gameWord时,它应该是一个字符数组--而不是指向字符的指针数组……
char gameWord [length+1];我有点担心这一点,因为length是一个变量,但我认为这没问题-我的C语言有点过时,我的本能做法是使用编译时常量表达式来声明数组大小(这是您可能需要的最大大小)。
顺便说一句-注意+1。一个C字符串有一个额外的字符--一个空的结束符--它不会被strlen计算在内--但是在声明char-array变量时,你必须考虑到这一点。
发布于 2010-11-23 14:52:17
尝试将函数的声明方式与函数的调用方式进行比较,以便理解错误消息。
findChars()函数的声明方式如下:
int findChars(char* gameWord[], char secretWord[], int length);并且像这样从main()中被调用:
findChars(&gameWord[length], secretWord[length], length)特别要注意的是,第二个参数被声明为一个char数组(一个字符串),而您向它传递的是单个字符。
https://stackoverflow.com/questions/4253320
复制相似问题