如何解决制作刽子手游戏时遇到问题? (c语言)
我对 C 语言仍然很陌生,我正在尝试制作一个刽子手游戏,但我一直无法在我获胜时结束游戏。
代码如下:
const int true = 1;
const int false = 0;
char words[][20] = {
"hangman","computer","programming","microsoft","visual","studio","express","learning"
};
int isletterinword(char word[],char letter)
{
int i;
for (i = 0; i < strlen(word); i++) {
if (word[i] == letter) {
return true;
}
}
return false;
}
int iswordcomplete(char secretword[],char rights[])
{
int i;
for (i = 0; i < strlen(secretword); i++) {
if (rights[i] == secretword[i] ) {
return true;
}
}
return false;
}
void printhangman(int numofwrongs)
{
// Line 1
printf("\t ______\n");
// Line 2
printf("\t | |\n");
// Line 3
printf("\t | +\n");
// Line 4 - left arm,head and right arm
printf("\t |");
if (numofwrongs > 0) printf(" \\");
if (numofwrongs > 1) printf("O");
if (numofwrongs > 2) printf("/");
printf("\n");
// Line 5 - body
printf("\t |");
if (numofwrongs > 3) printf(" |");
printf("\n");
// Line 6 - left leg and right leg
printf("\t |");
if (numofwrongs > 4) printf(" /");
if (numofwrongs > 5) printf(" \\");
printf("\n");
// Line 7
printf("\t |\n");
// Line 8
printf("\t__|__\n");
}
void printletters(char letters[])
{
int i;
for (i = 0; i < strlen(letters); i++) {
printf("%c ",letters[i]);
}
}
void printscreen(char rights[],char wrongs[],char secretword[])
{
int i;
for (i = 0; i < 25; i++)
printf("\n");
printhangman(strlen(wrongs));
printf("\n");
printf("Correct guesses: ");
printletters(rights);
printf("\n");
printf("Wrong guesses: ");
printletters(wrongs);
printf("\n\n\n");
printf("\t");
for (i = 0; i < strlen(secretword); i++) {
if (isletterinword(rights,secretword[i])) {
printf("%c ",secretword[i]);
}
else {
printf("_ ");
}
}
printf("\n\n");
}
int main()
{
int i;
int secretwordindex;
char rights[20];
char wrongs[7];
char guess;
secretwordindex = 0;
srand(time(0));
secretwordindex = rand() % 8;
for (i = 0; i < 20; i++) {
rights[i] = '\0';
}
for (i = 0; i < 6; i++) {
wrongs[i] = '\0';
}
while (strlen(wrongs) < 6) {
printscreen(rights,wrongs,words[secretwordindex]);
printf("\nPlease enter your guess: ");
scanf(" %c",&guess);
if (isletterinword(words[secretwordindex],guess)) {
rights[strlen(rights)] = guess;
}
else {
wrongs[strlen(wrongs)] = guess;
}
}
printscreen(rights,words[secretwordindex]);
if ( iswordcomplete(words[secretwordindex],rights[20])==true && strlen(wrongs) <= 6 ) { // The if condition here might be problematic.
printf("You have won!\n");
}
else {
printf("You have lost!\n");
}
}
错误信息如下:
main.c:197:48: 警告:传递‘iswordcomplete’的参数 2 使得
没有强制转换的整数指针 [-Wint-conversion]
main.c:55:5:注意:应为“char *”,但参数的类型为“char”
解决方法
第一件事:编译器错误是由于您将一个单个字符传递给您对iswordcomplete()
的调用,而不是一个数组的字符。因此,在 main
函数末尾附近的检查中,您需要传递 rights
(未修饰的)作为参数,代替 rights[20]
(顺便说一句,这是一个 out-数组的越界元素)。此外,在那个阶段,您不需要第二次检查(计算错误的数量 - 见下文)。以下是该部分代码的修复:
// if (iswordcomplete(words[secretwordindex],rights[20]) == true && strlen(wrongs) <= 6) { // The if condition here might be problematic.
if (iswordcomplete(words[secretwordindex],rights)){// && strlen(wrongs) <= 6) { // Needs the whole string as an argument
printf("You have won!\n");
}
现在要解决一些会阻止您的代码正常工作的其他问题......
(1)您的主 while
循环不会停止运行直到您输入了 6 个“错误”字母 - 即使您这样做 正确猜出这个词。因此,您需要向 iswordcomplete()
条件添加 while
检查(使用 !
operator† 否定它),以继续运行循环仅,如果单词不完整。像这样:
while (strlen(wrongs) < 6 && !iswordcomplete(words[secretwordindex],rights)) { // Need to break loop if we win!!
printscreen(rights,wrongs,words[secretwordindex]);
//...
(2) iswordcomplete
函数的逻辑有缺陷,因为它会在找到任何匹配项后立即返回“true”。相反,您需要两个循环,如果在“权限”列表中找不到任何单词的字母,则返回 false。这是一种可能的版本:
int iswordcomplete(char secretword[],char rights[])
{
int i,j;
for (i = 0; i < strlen(secretword); i++) {
for (j = 0; j < strlen(rights); j++) {
if (secretword[i] == rights[j]) break;
}
if (j >= strlen(rights)) return false; // Didn't find this letter
}
return true;
}
请随时提供任何进一步的澄清和/或解释。
† 如果您(还)不熟悉 !
运算符的这种用法,那么您可以明确地将函数的返回值与 false
常量进行比较,如果你对此更满意,就像这样:
while (strlen(wrongs) < 6 && iswordcomplete(words[secretwordindex],rights) == false) { // Break loop if we win!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。