gpt4 book ai didi

c - 刽子手 : beginner in C

转载 作者:太空宇宙 更新时间:2023-11-04 08:49:36 25 4
gpt4 key购买 nike

<分区>

我似乎无法弄清楚为什么当用户猜完所有字母时我的程序不会结束游戏。我知道问题出在 play() 函数中。关于在播放功能中更改什么或导致此问题的任何内容的任何指示?

#include    <stdio.h>
#include <string.h>
#include <ctype.h>
// MAXWORD, which will be the max word length
#define MAXWORD 10
// INCORRECT_GUESSES, which will be the max guesses
#define INCORRECT_GUESSES 5

/* Prototypes */

// Fills theArray with howMany copies of theLetter
void fill_array( char *theArray, int howMany, char theLetter );

// Get char from player, checks the letter, shows progress so far
int get_letter( char *theWord, char *soFar );

// Check if letter is in word, updates progress so far
int letter_in_word( char *theWord, char *soFar, char theLetter );

// Convert the word to lowercase
void lower_string( char *someWord );

// Play one game
void play( char *theWord );

/* Function definitions */

int main( )
{
char theWord [ MAXWORD ];
FILE* word;
word = fopen( "guesswords.txt", "r" );

if ( word == NULL )
{
printf( "No input file found..........\n" );
return -1;
}
printf("Want to play a game?\n");
fscanf( word, "%s", theWord );

lower_string( theWord );

play( theWord );
fprintf( word, "%s", theWord);
fclose( word );
return 0;
}

// Get char from player, checks the letter, shows progress so far
int get_letter( char *theWord, char *soFar )
{
char theLetter;
printf("\nPlease enter a letter: ");
scanf( " %c", &theLetter );
theLetter = tolower(theLetter);

letter_in_word( theWord, soFar, theLetter );

return theLetter;
}

// Fills theArray with howMany copies of theLetter
void fill_array( char *theArray, int howMany, char theLetter )
{
int i;
for( i=0; i<howMany; i++ )
{
theArray[i]= theLetter;
*(theArray + i) = theLetter;
*theArray = theLetter;
}
theArray[howMany] = '\0';
}

// Check if letter is in word, updates progress so far
int letter_in_word( char *theWord, char *soFar, char theLetter )
{
int i;
int num=0;
int len = strlen(theWord);

for( i=0; i<len; i++)
{
if (theWord[i] == theLetter )
{
soFar[i] = theLetter;
num++;
}
}
if (num == 0)
{
printf( "\nSORRY! your letter is not in the word\n" );
printf("%s\n", soFar);
return 0;

}
else if (num>0)
{
printf( "\nCongratz! your letter was in the word\n" );
printf("%s\n", soFar);
return 1;
}
}

// Convert the word to lowercase
void lower_string( char *someWord )
{
int i, cha;
int len = strlen( someWord );
for( i=0; i<len; i++ )
{
cha = someWord[i];
cha = tolower(cha);
someWord[i] = cha;
}
}

// Play one game
void play( char *theWord )
{
int i;
int len = strlen(theWord);
int guess = INCORRECT_GUESSES;
int result = 0;
char soFar[MAXWORD];
fill_array( soFar, len, '*');
printf( "Guess this word: %s\n", soFar );

for( i=0; i<INCORRECT_GUESSES; i++ )
{
guess = INCORRECT_GUESSES - (i+1);
get_letter( theWord, soFar );

if(get_letter<0)
{
printf( "\nYou have %d guesses left\n", guess);
}
else
{
printf( "\nYou have guessed all the letters! You have won!" );
return;
}

if( i == INCORRECT_GUESSES-1)
{
printf( "\nSorry, you're out of guesses\nBetter luck next time!\n" );
}
}
}

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