我得到的錯誤是遊戲的數量沒有正確顯示。例如,我選擇玩2場比賽。然後,我試圖正確猜測第一個數字。解決方案已顯示,然後跳到下一場比賽。然而,第二場比賽被表現爲第三場比賽,而不是第二場比賽。 我再次嘗試。這一次,我錯誤地猜出了1個字母,1個正確的字母。第二次猜測後,遊戲顯示瞭解決方案,然後停止了遊戲,儘管我選擇玩2場比賽,只玩了1場比賽。 在LetterList文件字母的順序是 d 乙 摹 W¯¯ Q 牛逼 [R Ÿ ü X 所以第一場比賽與「d」,然後「B」,然後開始等... 。 錯誤顯示爲程序本身擺脫偶數。 我不知道它有什麼問題。C中的猜字遊戲
#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;
}
}
這是學習如何使用調試器的完美機會。使用調試器,您可以逐行瀏覽代碼,查看發生了什麼以及程序在做什麼。您還可以監視變量及其值,並查看它們在程序過程中的變化。知道如何使用調試器是任何程序員甚至愛好者程序員的基本技能。 –
這是一個很好的建議。我並沒有真正熟悉調試器。謝謝! – user6866732