【发布时间】: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% 确定你想问什么,是的,据我所知,这部分有效。
-
您不是将数组的索引传递给需要该数组的函数吗?您正在取消引用指针(因此得到一个“整数”)并将其传递给一个需要指针的函数。