我的刽子手游戏似乎不能在C中运行吗



我用C做了一个刽子手游戏,其中两个函数似乎不起作用。代码如下,麻烦制造者是int check_guessint hidden_word其他人似乎自己工作得很好。由于某些原因,check_guess函数无法确定猜测是否正确,而hidden_word函数几乎不起作用。

int main(int argc, char **argv)
{char wordfile[256], option, temp[256] ;
char wordlist[MAX_WORDS][MAX_WORD_LENGTH] ;
int num_words, result ;
Game g ;`
// seed the rand() function first
srand ( time(NULL));
// check to see if command line argument was given, if so use it as the filename for the words
if ( argc > 1 ){
strcpy ( wordfile, argv[1] ) ;
} else {
strcpy ( wordfile, "wordlist.txt" ) ;
}
// now read word file
num_words = read_words ( wordfile, wordlist ) ;
if ( num_words == 0 ){
printf ( "No words were read from file, exitingn") ;
exit ( -1 ) ;
}
printf ( "Read %d words from filen", num_words ) ;
do {
setup_game ( &g, wordlist, num_words ) ;
result = play_game ( &g ) ;
printf ( "Would you like to play again (y/n)? " ) ;
scanf ( " %c", &option ) ;
fgets ( temp, 256, stdin ) ; // read the rest of the line to get rid of it from stdin
} while ( option == 'y' || option == 'Y' ) ;
return 0 ;
}

int draw_man ( int misses ){

char *platform[]={

"      ===n",
"        |n"
"        |n"
"        |n"
"       ===n",
"   =====|n"
"        |n"
"        |n"
"        |n"
"       ===n",
"  |=====|n"
"        |n"
"        |n"
"        |n"
"       ===n",
"  |=====|n"
"  O     |n"
"        |n"
"        |n"
"       ===n",
"  |=====|n"
"  O     |n"
"  |     |n"
"        |n"
"       ===n",
"  |=====|n"
"  O     |n"
"  |-    |n"
"        |n"
"       ===n",
"  |=====|n"
"  O     |n"
" -|-    |n"
"        |n"
"       ===n",
"  |=====|n"
"  O     |n"
" -|-    |n"
"  |     |n"
"       ===n",
"   |=====|n"
"   O     |n"
"  -|-    |n"
"  //     |n"
"       ===n"
};
switch(misses)
{
case 0:
printf("nn%sn", platform[0]);
break;
case 1:
printf("nn%sn", platform[1]);
break;
case 2:
printf("nn%sn", platform[2]);
break;
case 3:
printf("nn%sn", platform[3]);
break;
case 4:
printf("nn%sn", platform[4]);
break;
case 5:
printf("nn%sn", platform[5]);
break;
case 6:
printf("nn%sn", platform[6]);
break;
case 7:
printf("nn%sn", platform[7]);
break;
case 8:
printf("nn%sn", platform[8]);
break;
case 9:
printf("nn%sn", platform[9]);
break;
}
}
int display_guesses( unsigned char guesses[] ){

for(int j=0;j<26;j++){
int i = j + 'A';
char c = (char)i;
if(guesses[j] == 1)
//printf("%d ", j);
printf("%c ", c);
}
printf("n");
}

char read_guess ( unsigned char guesses[] ){
char c;
while(1){
printf("Enter a valid character: ");
scanf("n%c",&c);
// printf("hi");
if(c>='a'&& c<='z'&& guesses[(int)(c-'a')] ==0)
return (char)(c-'a') +'A' ;
if(c>='A'&& c<='Z'&& guesses[(int)(c-'A')] ==0)
return c;

printf("n Invalid Character!!n");
}
}

// WEEK 2 FUNCTIONS
// add_guess()
// Adds the given guess to the guesses array, making the relevant entry 1. For exmpale, if guess is 'a' or 'A',
// element 0 of the guesses array is set to 1. If 'z' or 'Z' is input, then element 25 is set to 1. Your function
// should check that the character is a valid letter and return -1 if it is not.
// Returns 0 if successful, or -1 if an invalid character is entered.
int add_guess ( char guess, unsigned char guesses[] ){
if ((guess >= 'a' && guess <= 'z') || (guess >= 'A' && guess <= 'Z')) {
if (guess >= 'a' && guess <= 'z') {
guesses[guess - 'a'] = 1;
} else {
guesses[guess - 'A'] = 1;
}
return 0;
}
return -1;
}

// check_guess()
// Checks if the given character 'guess' is contained within the string 'word'.
// Returns 1 if it is, 0 otherwise
int check_guess ( char word[], char guess ){


int length = strlen(word); 
int i;
for (i = 0; i < length; ++i) {
if (guess == word[i])
return 1;
}
return 0;
}


// hidden_word()
// Creates the word that is displayed to the user, with all the correctly guessed letters
// shown, and the rest displayed as underscores.  Any non-letters (punctuation, etc) are displayed.
// The function takes two strings as inputs.  word[] is the word that the player is trying to guess,
// and display_word[] is the output string to be displayed to the player.  The guesses array is a binary
// array of size 26 indicating whether each letter (a-z) has been guessed yet or not.
// Returns 0 if successful, -1 otherwise.
int hidden_word ( char display_word[], char word[], unsigned char guesses[26] ){
int l = strlen(display_word);
if (l != strlen(word))
return -1;
//printf("%d", l);
int i;
for (i = 0; i < l; ++i) {
char c1 = (display_word[i] >= 'A' && display_word[i] <= 'Z') ? display_word[i] + 32 : display_word[i];
char c2 = (word[i] >= 'A' && word[i] <= 'Z') ? word[i] + 32 : word[i];
if (c1 == '_') {
if (guesses[c2 - 97] == 1)
return -1;
else {
guesses[c2 - 97] = 1;
}
}
else if ((c1 >= 'a' && c1 <= 'z') || (c1 >= 'A' && c1 <= 'Z')) {
if (c2 != c1)
return -1;
}
}
return 0;
}`

您的函数check_guess似乎工作正常。我不明白你在hidden_word做什么。

我为这些函数编写了一个小的测试程序,并修改了(工作的(函数hidden_word_2来替换hidden_word中的非工作实现。

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int check_guess ( char word[], char guess );
int hidden_word ( char display_word[], char word[], unsigned char guesses[26] );
int hidden_word_2 ( char display_word[], char word[], unsigned char guesses[26] );
int main()
{
char word[] = "foobarBAZ";
char display_word[] = "_________";
char guess[] = "abcefgzdABCDZ* or";
char guesses[26];

memset(guesses, 0, sizeof(guesses));

printf("ntest check_guess:nn");
for(int i = 0; guess[i] != ''; i++)
{
int check = check_guess(word, guess[i]);
printf("<%c> %s %dn", guess[i], word, check);
}
printf("ntest hidden_word:nn");
for(int i = 0; guess[i] != ''; i++)
{
if(isalpha(guess[i]))
{
guesses[tolower(guess[i]) - 'a'] = 1;
}
int result = hidden_word ( display_word, word, guesses ); // does not work
printf("<%c> %s %dn", guess[i], display_word, result);
}
printf("ntest hidden_word_2:nn");
memset(guesses, 0, sizeof(guesses));
for(int i = 0; guess[i] != ''; i++)
{
if(isalpha(guess[i]))
{
guesses[tolower(guess[i]) - 'a'] = 1;
}
int result = hidden_word_2 ( display_word, word, guesses );
printf("<%c> %s %dn", guess[i], display_word, result);
}
return 0;
}
int check_guess ( char word[], char guess ){


int length = strlen(word); 
int i;
for (i = 0; i < length; ++i) {
if (guess == word[i])
return 1;
}
return 0;
}
// hidden_word()
// Creates the word that is displayed to the user, with all the correctly guessed letters
// shown, and the rest displayed as underscores.  Any non-letters (punctuation, etc) are displayed.
// The function takes two strings as inputs.  word[] is the word that the player is trying to guess,
// and display_word[] is the output string to be displayed to the player.  The guesses array is a binary
// array of size 26 indicating whether each letter (a-z) has been guessed yet or not.
// Returns 0 if successful, -1 otherwise.
int hidden_word ( char display_word[], char word[], unsigned char guesses[26] ){
int l = strlen(display_word);
if (l != strlen(word))
return -1;
//printf("%d", l);
int i;
for (i = 0; i < l; ++i) {
char c1 = (display_word[i] >= 'A' && display_word[i] <= 'Z') ? display_word[i] + 32 : display_word[i];
char c2 = (word[i] >= 'A' && word[i] <= 'Z') ? word[i] + 32 : word[i];
if (c1 == '_') {
if (guesses[c2 - 97] == 1)
return -1;
else {
guesses[c2 - 97] = 1;
}
}
else if ((c1 >= 'a' && c1 <= 'z') || (c1 >= 'A' && c1 <= 'Z')) {
if (c2 != c1)
return -1;
}
}
return 0;
}

int hidden_word_2 ( char display_word[], char word[], unsigned char guesses[26] ){
int result = 0;

int l = strlen(display_word);
if (l != strlen(word))
return -1;
//printf("%d", l);
int i;
for (i = 0; i < l; ++i) {
char c2 = (word[i] >= 'A' && word[i] <= 'Z') ? word[i] + 32 : word[i];
if (guesses[c2 - 97] == 1)
{
display_word[i] = word[i];
}
else
{
display_word[i] = '_';
result = -1;
}
}
return result;
}

您可以在此处运行或调试它:https://onlinegdb.com/r1hDWe_Ew

结果是

test check_guess:
<a> foobarBAZ 1
<b> foobarBAZ 1
<c> foobarBAZ 0
<e> foobarBAZ 0
<f> foobarBAZ 1
<g> foobarBAZ 0
<z> foobarBAZ 0
<d> foobarBAZ 0
<A> foobarBAZ 1
<B> foobarBAZ 1
<C> foobarBAZ 0
<D> foobarBAZ 0
<Z> foobarBAZ 1
<*> foobarBAZ 0
< > foobarBAZ 0
<o> foobarBAZ 1
<r> foobarBAZ 1
test hidden_word:
<a> _________ -1
<b> _________ -1
<c> _________ -1
<e> _________ -1
<f> _________ -1
<g> _________ -1
<z> _________ -1
<d> _________ -1
<A> _________ -1
<B> _________ -1
<C> _________ -1
<D> _________ -1
<Z> _________ -1
<*> _________ -1
< > _________ -1
<o> _________ -1
<r> _________ -1
test hidden_word_2:
<a> ____a__A_ -1
<b> ___ba_BA_ -1
<c> ___ba_BA_ -1
<e> ___ba_BA_ -1
<f> f__ba_BA_ -1
<g> f__ba_BA_ -1
<z> f__ba_BAZ -1
<d> f__ba_BAZ -1
<A> f__ba_BAZ -1
<B> f__ba_BAZ -1
<C> f__ba_BAZ -1
<D> f__ba_BAZ -1
<Z> f__ba_BAZ -1
<*> f__ba_BAZ -1
< > f__ba_BAZ -1
<o> fooba_BAZ -1
<r> foobarBAZ 0

您可以使用#include <ctype.h>中的函数isuppertolower等简化对大写字母的检查和对小写字母的转换,例如在hidden_word中,您可以使用:

char c2 = tolower(word[i]);

最新更新