我得到的错误是游戏数量没有正确显示。 例如,我选择玩2场比赛。然后,我试图正确猜出第一个数字。显示了解决方案,然后跳转到下一个游戏。但是,第二场比赛显示为第三场比赛而不是第二场比赛。 我又试了一次。这次,我猜错了 1 次,猜对了 1 次。第二次猜测后,游戏显示了解决方案,然后尽管我选择玩 2 场比赛并且只进行了 1 场比赛,但比赛停止了。 LetterList 文件中字母的顺序是 d 乙 G w 问 吨 r 是 你 X 所以第一场比赛以“d”开始,然后是“B”,然后等等...... 该错误显示为好像程序本身摆脱了偶数。 我不知道它出了什么问题。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
#define MAXGUESSES 5
void LetterGuessRules();
void GuessTheLetter(char);
char GetTheGuess();
int CompareLetters(char, char);
int main()
{
FILE *inPtr;
int numGames, i = 0;
char letter;
//display the game rule
LetterGuessRules();
printf("\nHow many games do you want to play? (Max 10) >> ");
scanf("%d", &numGames);
printf("\n\n************************************************\n");
inPtr = fopen("letterList.txt", "r");
for (i = 0; i < numGames; i++)
{
//get a solution letter from file - use fscanf
fscanf(inPtr," %c", &letter);
//change the solution to lowercase
letter = tolower(letter);
//print the solution back onto the screen to test
//Close this when play the game to hide the foreseen solution
printf("\nThe letter is %c\n", letter);
//Number of match
printf("\t\tGame %d\n", i += 1);
//call the GuessTheLetter function and pass it the solution
GuessTheLetter(letter);
}
fclose(inPtr);
return 0;
}
void GuessTheLetter(char letter)
{
int win = 0;
int numGuesses = 0;
char myGuess;
while (numGuesses < MAXGUESSES && win == 0)
{
//get a guess from the user by calling the GetTheGuess function
myGuess = GetTheGuess();
//change the guess to lowercase
myGuess = tolower(myGuess);
//win = call the function to compare the guess with the solution
win = CompareLetters(letter, myGuess);
numGuesses++;//count the number of guesses so far
//use conditions to let the user know if they won or lost the round of the game
if (win == 0)
{
printf("\nOops its wrong.\n");
if (myGuess < letter)
{
printf("Your guessed letter -%c- comes before the solution\n", myGuess);
printf("Please guess again :)\n");
}
else if (myGuess > letter)
{
printf("Your guessed letter -%c- comes after the solution\n", myGuess);
printf("Please guess again :)\n");
}
if (numGuesses == MAXGUESSES && win == 0)
printf("Aw you have lost this game!");
printf("\n");
}
else if (win == 1)
{
printf("\nYou have guessed it right!\n");
printf("Wonderful! You ACE'd this match!\n");
printf("\n");
printf("**** If you play more than 1 game, new match will automatically start ****\n");
printf("\tYou only need to keep guessing for the next letter\n");
printf("------------------------------------------------------------------------------");
printf("\n");
}
}
}
char GetTheGuess()
{
char myGuess;
printf("\t_______________________");
printf("\n\t|What's your guess? >> ");
scanf(" %c", &myGuess);
return myGuess;
}
void LetterGuessRules()
{
printf("\n*** Instruction: ");
printf("\nYou will have 5 attempts to guess the right answer");
printf("\nIf you guess it right, the game will end with your victory.");
printf("\nOtherwise, you will have to guess again.");
printf("\nPlease have fun!");
}
int CompareLetters(char letter, char myGuess)
{
if (letter == myGuess)
{
return 1;
}
else
{
return 0;
}
}
最佳答案
问题是你增加了i
两次。首先在for
循环,并再次在这里:
//Number of match
printf("\t\tGame %d\n", i += 1);
这可能是因为你得到了 Game 0
没有它。简单的解决方法是从 1 而不是 0 开始循环。
自 i
意味着比“循环迭代器”更多的东西我称之为更具描述性的东西 gameNum
.
/* from 1 to numGames */
int gameNum;
for( gameNum = 1; gameNum <= numGames; gameNum++ ) {
...
}
请注意,我检查了 gameNum <= numGames
而不是 gameNum < numGames
因为我们现在从 1 开始。
另外你需要检查文件是否打开,否则如果 letterList.txt 不存在它会崩溃。
#include <errno.h> /* for errno */
#include <string.h> /* for strerror() */
#include <stdlib.h> /* for exit() */
inPtr = fopen("letterList.txt", "r");
if( inPtr == NULL ) {
fprintf(stderr, "Could not open letterList.txt: %s\n", strerror(errno));
exit(1);
}
最后,我建议不要使用 #define _CRT_SECURE_NO_WARNINGS
在学习 C 的同时。那些安全警告很重要。
关于c - C 中的字母猜谜游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39812419/