C中的猜字游戏

时间:2016-10-01 23:26:01

标签: c

我得到的错误是游戏数量没有正确显示。 例如,我选择玩2场比赛。然后,我试图正确猜出第一个数字。显示解决方案然后跳到下一个游戏。然而,第二场比赛被展示为第3场而不是第2场。 我再试一次。这一次,我猜错了1次错误的信和正确的1次。在第二次猜测之后,游戏显示了解决方案然后停止了游戏,尽管我选择玩2场比赛并且只玩了1场比赛。 LetterList文件中的字母顺序是 d 乙 G w ^ Q Ť [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;
    }
}

1 个答案:

答案 0 :(得分: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);
}

最后,我建议在学习C时不要使用#define _CRT_SECURE_NO_WARNINGS。这些安全警告很重要。