2016-10-01 113 views
1

我得到的錯誤是遊戲的數量沒有正確顯示。例如,我選擇玩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; 
    } 
} 
+3

這是學習如何使用調試器的完美機會。使用調試器,您可以逐行瀏覽代碼,查看發生了什麼以及程序在做什麼。您還可以監視變量及其值,並查看它們在程序過程中的變化。知道如何使用調試器是任何程序員甚至愛好者程序員的基本技能。 –

+0

這是一個很好的建議。我並沒有真正熟悉調試器。謝謝! – user6866732

回答

1

問題是你增加了兩次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.這些安全警告是非常重要的。

+1

謝謝你的幫助!我一定會嘗試修復它。 :) – user6866732