gpt4 book ai didi

c - Hangman - 如何正确计算玩家的错误猜测次数?

转载 作者:行者123 更新时间:2023-11-30 21:30:20 29 4
gpt4 key购买 nike

现在我的游戏正确计数了我正确的字符数,但是如果不匹配字符的大约等于字符串的长度,则不会计数。我一直在尝试修改这段代码一段时间,但它仍然无法修复。这是我的代码:

/*includes and defines*/
#include <stdio.h>
#include <string.h>
#define SIZE 50

/*prototype definitions*/
int compareString(char *, char *);

int main(void){

char word[SIZE];
char input[SIZE];
char guess[SIZE];
int count = 0;
int wrong = 0;
int incorrect = 0;
int right = 0;
int len = 0;

printf("Please enter the word you would like to have to guess.\nThen hand your computer over to the person you would like to have play:");
fgets(word, SIZE, stdin);

len = strlen(word);

printf("Please guess one letter for the %d letter word!\n", len - 1);

do{

fgets(input, SIZE, stdin);

for(count = 0; count < len - 1; count++){
if(input[0] == word[count]){
printf("that letter is in the %d spot\n", count + 1);
++right;
}
/*I know the problem lies here but i'm not sure how to fix it I've tried not using len-1 and just using len, I've tried not resetting the amount wrong. Everything!*/
else if (input[0] != word[count]) {
++wrong;
if(wrong == len - 1){
++incorrect;
}
wrong = 0;
}
}

}while(incorrect < 6 && right < len - 1);


return 0;
}

我知道问题就出在这里,播放器的代码设置错误。但是我不知道如何解决它。我尝试过不使用 len-1 而只使用 len,我尝试过不重置错误的金额。

else if (input[0] != word[count]) {
++wrong;
if(wrong == len - 1){
++incorrect;
}
wrong = 0;
}

最佳答案

问题在于您尝试增加不正确的方式。只需使用 wrong 作为 bool 值,您将用它来决定是否需要增加in Correct

解决方案如下:

#include <stdio.h>
#include <string.h>
#define SIZE 50

/*prototype definitions*/
int compareString(char *, char *);

int main(void){

char word[SIZE];
char input[SIZE];
char guess[SIZE];
int count = 0;
int wrong = 1;
int incorrect = 0;
int right = 0;
int len = 0;

printf("Please enter the word you would like to have to guess.\nThen hand your computer over to the person you would like to have play:");
fgets(word, SIZE, stdin);

len = strlen(word);

printf("Please guess one letter for the %d letter word!\n", len - 1);

do{

fgets(input, SIZE, stdin);
wrong = 1;
for(count = 0; count < len - 1; count++){

if(input[0] == word[count]){
printf("that letter is in the %d spot\n", count + 1);
wrong = 0;
++right;
}
/*I know the problem lies here but i'm not sure how to fix it I've tried not using len-1 and just using len, I've tried not resetting the amount wrong. Everything!*/
}
if(wrong) {
incorrect++;
}

}while(incorrect < 6 && right < len - 1);


return 0;
}

关于c - Hangman - 如何正确计算玩家的错误猜测次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26770296/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com