gpt4 book ai didi

c - 刽子手 : ending the game if player guesses all letters

转载 作者:行者123 更新时间:2023-11-30 15:42:30 25 4
gpt4 key购买 nike

我正在为我的 C 课编程编写一个刽子手程序。我似乎不知道如何执行最后一步,即在玩家猜出所有正确的字母后停止游戏。我感觉问题出在播放功能上。

#include    <stdio.h>
#include <string.h>
#include <ctype.h>
// MAXWORD, which will be the max word length
#define MAXWORD 20
// 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 );

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( i == INCORRECT_GUESSES-1)
{
printf( "\nSorry, you're out of guesses\nBetter luck next time!\n" );
}

if(get_letter>0)
{
printf( "\nYou have %d guesses left\n", guess);
}
else
{
printf( "\nYou have won!" );
}
}
}

最佳答案

一个人获胜意味着你已经玩完了。因此,您需要从 play 函数返回一次,而不是打印“\nYou have won”玩家获胜:

// ...
if( i == INCORRECT_GUESSES-1)
{
printf( "\nSorry, you're out of guesses\nBetter luck next time!\n" );
return; // <- player lost, return
}
// ...
else
{
printf( "\nYou have won!" );
return; // <- player won, return
}
}

关于c - 刽子手 : ending the game if player guesses all letters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20151303/

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