制作刽子手游戏时遇到问题? (c语言)
Get a problem when make a hangman game ? ( c language)
我对 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] = '[=10=]';
}
for (i = 0; i < 6; i++) {
wrongs[i] = '[=10=]';
}
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, wrongs, 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: warning: passing argument 2 of ‘iswordcomplete’ makes
pointer from integer without a cast [-Wint-conver sion]
main.c:55:5: note: expected ‘char *’ but argument is of type ‘char’
第一件事:编译器错误是由于您将 单个 字符传递给对 iswordcomplete()
的调用,而不是 [=37] =]array 个字符。因此,在 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 'wrong' 个字母 – 即使您 猜对了单词。因此,您需要向 while
条件添加一个 iswordcomplete()
检查(用 !
运算符 † 否定它),以保持 运行 循环 只有 如果单词 没有 完整。像这样:
while (strlen(wrongs) < 6 && !iswordcomplete(words[secretwordindex], rights)) { // Need to break loop if we win!!
printscreen(rights, wrongs, words[secretwordindex]);
//...
(2) 你的 iswordcomplete
函数的逻辑是有缺陷的,因为它会 return “真” 一旦找到 任意匹配。相反,您需要两个循环,如果在 'rights'.这是一种可能的版本:
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;
}
如有任何进一步的说明,请随时接受and/or解释。
† 如果您(还)不熟悉 !
运算符的这种用法,那么您可以显式比较函数的 return 值到 false
常量,如果您对此更满意,可以像这样:
while (strlen(wrongs) < 6 && iswordcomplete(words[secretwordindex], rights) == false) { // Break loop if we win!
我对 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] = '[=10=]';
}
for (i = 0; i < 6; i++) {
wrongs[i] = '[=10=]';
}
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, wrongs, 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: warning: passing argument 2 of ‘iswordcomplete’ makes pointer from integer without a cast [-Wint-conver sion]
main.c:55:5: note: expected ‘char *’ but argument is of type ‘char’
第一件事:编译器错误是由于您将 单个 字符传递给对 iswordcomplete()
的调用,而不是 [=37] =]array 个字符。因此,在 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 'wrong' 个字母 – 即使您 猜对了单词。因此,您需要向 while
条件添加一个 iswordcomplete()
检查(用 !
运算符 † 否定它),以保持 运行 循环 只有 如果单词 没有 完整。像这样:
while (strlen(wrongs) < 6 && !iswordcomplete(words[secretwordindex], rights)) { // Need to break loop if we win!!
printscreen(rights, wrongs, words[secretwordindex]);
//...
(2) 你的 iswordcomplete
函数的逻辑是有缺陷的,因为它会 return “真” 一旦找到 任意匹配。相反,您需要两个循环,如果在 'rights'.这是一种可能的版本:
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;
}
如有任何进一步的说明,请随时接受and/or解释。
† 如果您(还)不熟悉 !
运算符的这种用法,那么您可以显式比较函数的 return 值到 false
常量,如果您对此更满意,可以像这样:
while (strlen(wrongs) < 6 && iswordcomplete(words[secretwordindex], rights) == false) { // Break loop if we win!