我的程序目前运行良好,除了它应该显示逐渐悬挂的人的部分。
#include <stdio.h>
#include <string.h>
#define SIZE 30
void game(char w[], char g[], int len);
void hangman(int m);
main() {
char word[SIZE] = "vocal";
int len1 = 0, i;
char guessed[SIZE];
len1 = strlen(word);
guessed[len1];
for(i = 0; i < len1; i++)
guessed[i] = '*';
guessed[len1] = '\0';
printf("%s\n", guessed);
game(word, guessed, len1);
}
void game(char w[], char g[], int len) {
char buffer[100];
char letter = '\0';
int j;
int misses = 0, hits = 0;
while(g != w && misses != 8)
{
printf("Enter a guess: ");
fgets(buffer, 100, stdin);
sscanf(buffer, "%c", &letter);
for(j = 0; j < len; j++)
{
if(letter == w[j])
{
g[j] = letter;
hits++;
printf("%s\n", g);
if(hits == len) {
printf("Congrats, you won!\n");
misses = 8;
}
我认为其他的逻辑是错的,但我不知道如何解决它。
else
{
if( j == len && letter != w[j])
{
misses++;
hangman(misses);
}
}
}
}
}
}
void hangman(int m) {
switch(m) {
case 1:
{
printf(" ----------\n");
printf(" |/ |\n");
printf(" | (_)\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("____|____\n");
break;
}
case 2:
{
printf(" ----------\n");
printf(" |/ |\n");
printf(" | (_)\n");
printf(" | |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("____|____\n");
break;
}
case 3:
{
printf(" ----------\n");
printf(" |/ |\n");
printf(" | (_)\n");
printf(" | \\|\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("____|____\n");
break;
}
case 4:
{
printf(" ----------\n");
printf(" |/ |\n");
printf(" | (_)\n");
printf(" | \\|/\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("____|____\n");
break;
}
case 5:
{
printf(" ----------\n");
printf(" |/ |\n");
printf(" | (_)\n");
printf(" | \\|/\n");
printf(" | |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("____|____\n");
break;
}
case 6:
{
printf(" ----------\n");
printf(" |/ |\n");
printf(" | (_)\n");
printf(" | \\|/\n");
printf(" | |\n");
printf(" | /\n");
printf(" |\n");
printf(" |\n");
printf("____|____\n");
break;
}
case 7:
{
printf(" ----------\n");
printf(" |/ |\n");
printf(" | (_)\n");
printf(" | \\|/\n");
printf(" | |\n");
printf(" | / \\ \n");
printf(" |\n");
printf(" |\n");
printf("____|____\n");
break;
}
case 8:
{
printf(" ----------\n");
printf(" |/ |\n");
printf(" | (_)\n");
printf(" | \\|/\n");
printf(" | |\n");
printf(" | / \\ \n");
printf(" |\n");
printf(" |\n");
printf("____|____\n");
printf("You lost.");
break;
}
}
}
如果有人可以帮助我,我会非常感激!
答案 0 :(得分:0)
好的,你有一些逻辑错误。我试图做最小的改变。现在它可以工作但你肯定可以改进它。例如,游戏应该告诉用户他/她是否想再玩一次。
(include also stdlib.h for the function exit())
void game(char w[], char g[], int len)
{
char buffer[100];
char letter = '\0';
int j;
int misses = 0, hits = 0;
int global_miss = 0;
while(g != w && misses != 8)
{
printf("Enter a guess: ");
fgets(buffer, 100, stdin);
sscanf(buffer, "%c", &letter);
misses = 0;
for(j = 0; j < len; j++)
{
if(letter == w[j])
{
misses = 0;
hits++;
g[j] = letter;
printf("%s\n", g);
if(hits == len) {printf("Congrats, you won!\n"); exit(111);}
} else {
misses++;
fprintf(stdout, "misses = %d\n", misses);
if (misses == len) {
fprintf(stdout, "This letter is wrong! Try again");
global_miss += 1;
hangman(global_miss);
if (global_miss == 8) {
printf("Sorry you lose!\n");
exit(111);
}
}
}
}
}
}
您还应该删除main中未使用的表达式:
guessed[len1];
并且,请定义int main(如果您对辩论int vs void main感兴趣,请参阅:What should main() return in C and C++?)