现在,我试图让我的计分卡为子手工作。这是一个我可以玩多次的游戏,当我在游戏菜单上按2时,我希望它显示最低分数。由于某种原因,我的逻辑不起作用。有人可以帮忙吗?我已附上了3个代码部分,这些代码才能起作用。

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>

using namespace std;

int maxAttempts = 10; //max attempts possible
void poorStiff(int);
int wordFill(char, string, string&);


int main()
{


    int intro;
    int bodyPart = 0;
    ifstream wordIn; //file stream read in
    wordIn.open("WordList.txt"); //list of words used in this game
    //if (!wordIn.good()) { std::cerr << "open failed.\n"; exit(1); }
    char letter; //letter guessed
    int numWrongGuesses = 0; //counts the number of wrong guesses
    string theWord;
    string words[13];
    int play = 0;
    bool run = true;
    int makeYourSelection = 0;
    int bestScore = 11;

    while (run == true)
    {
        int attemptsGame = 0;
        // put a game menu to loop that asks if you want to keep playing or exit and go to scorecard
        cout << "            WELCOME TO THE GAME OF HANGMAN\n\n\n\n";
        cout << "    Press 1 to play the game, press 2 to exit to the scorecard \n\n";
        cout << "   You have 10 attempts to guess the words before you get hung \n\n";
        cin >> play;


        while (play == 1)//loops while user has entered 1, 0 exits the game
        {
            for (int i = 0; i < 13; i++) //labeled 13 and not words because 13 is a constant
            {
                wordIn >> words[i];//replaces the file words and puts them in an array and counts them out of the file
                cout << words[i] << endl;
            }
            srand(time(NULL));//to get a random word
            int n = rand() % 12;
            theWord = words[n]; //pulls word from file
            wordIn.close();

            string mystery(theWord.length(), '*'); //replaces word letters with asterisks



            while (numWrongGuesses < maxAttempts) // while the amount of guesses is less than the max wrong guesses
            {
                cout << mystery << endl << endl;
                cout << "You now have the length of the word represented by the *'s. \n\n";
                cout << "Guess a letter \n\n";
                cin >> letter;


                if (wordFill(letter, theWord, mystery) == 0) //fuction call
                {
                    bodyPart++;
                    poorStiff(bodyPart);
                    cout << "You have entered a letter that isn't in the word, guess again. \n\n";
                    numWrongGuesses++;
                    attemptsGame++;
                }
                else
                {
                    for (int i = 0; i < mystery.length(); i++)
                    {
                        if (theWord[i] == letter)
                        {
                            mystery[i] = letter;
                        }
                    }
                    cout << "You have found one of the letters. Congratulations! \n\n";
                    cout << "You have: " << maxAttempts - numWrongGuesses;
                    cout << " guesses left \n\n" << endl;
                }

                if (theWord == mystery) // the word is the same as mystery
                {
                    cout << theWord << endl;
                    cout << "\n\n Awesome, you guessed it. \n\n";
                    break;
                    if (attemptsGame < bestScore)
                    {
                        bestScore = attemptsGame;

                    }
                }
            }
            if (numWrongGuesses == maxAttempts) //when you run out of guesses
            {
                cout << "Too bad, you ran out of guesses and have been hung at the gallows. \n\n";
                cout << "The word you were trying to guess was " << theWord << endl;
                poorStiff(bodyPart);
            }
            cin.ignore();
            cin.get();

            break;
        }
            while (play == 2)
            {
                cout << "Best Scores: \n\n";
                cout << bestScore << endl;
                system("pause");
                return 0;
                break;
            }
        }
        system("pause");
        return 0;
    }


    int wordFill(char guess, string theWordSecret, string&guessWord) //function for determing if you guess a letter contained
    {
        int i;
        int hits = 0; //letter hits within the word
        int many = theWordSecret.length();

        for (i = 0; i < many; i++)
        {
            if (guess == guessWord[i])
                return 0;
            if (guess == theWordSecret[i])
            {
                guessWord[i] == guess;
                hits++;
            }
        }
        return hits;
    }

    void poorStiff(int bodyPart)
    {

        if (bodyPart == 1)
        {
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "_______________" << endl;

        }
        else if (bodyPart == 2)
        {
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|      |" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "_______________" << endl;

        }
        else if (bodyPart == 3)
        {
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|     /|" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "_______________" << endl;

        }
        else if (bodyPart == 4)
        {
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|     /|" << endl;
            cout << "|    / " << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "_______________" << endl;

        }
        else if (bodyPart == 5)
        {
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|     /|" << endl;
            cout << "|    / |" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "_______________" << endl;

        }
        else if (bodyPart == 6)
        {
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|     /|\." << endl;
            cout << "|    / | \." << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "_______________" << endl;

        }
        else if (bodyPart == 7)
        {
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|     /|\." << endl;
            cout << "|    / | \." << endl;
            cout << "|     /" << endl;
            cout << "|" << endl;
            cout << "_______________" << endl;

        }
        else if (bodyPart == 8)
        {
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|     /|\." << endl;
            cout << "|    / | \." << endl;
            cout << "|     / \." << endl;
            cout << "|" << endl;
            cout << "_______________" << endl;

        }
        else if (bodyPart == 9){
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|     /|\." << endl;
            cout << "|    / | \." << endl;
            cout << "|     / \." << endl;
            cout << "|    / " << endl;
            cout << "_______________" << endl;

        }
        else if (bodyPart == 10)
        {
            cout << "_______" << endl;
            cout << "|      }" << endl;
            cout << "|      O" << endl;
            cout << "|     /|\." << endl;
            cout << "|    / | \." << endl;
            cout << "|     / \." << endl;
            cout << "|    /   \." << endl;
            cout << "_______________" << endl;
        }
    }

最佳答案

您尝试调试了吗?如果单步执行代码,您将立即发现问题,在这里:

            if (theWord == mystery) // the word is the same as mystery
            {
                cout << theWord << endl;
                cout << "\n\n Awesome, you guessed it. \n\n";
                break;    // problem here
                if (attemptsGame < bestScore)
                {
                    bestScore = attemptsGame;

                }
                // break should be here
            }


在检查逻辑之前,您要摆脱循环。因此,请在检查后将break语句移至,因为该代码永远不会执行。

08-27 01:03