0

我从 C++ 开始,正在创建一个刽子手游戏,我的代码运行良好,直到我选择制作三个不同的难度级别,我的游戏询问用户他们想要玩的难度级别,然后改为在实际玩游戏时,它会直接跳到最后,说用户猜对了这个词。任何帮助表示赞赏!代码如下:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

void ClearScreen();
void DisplayMan0();
void DisplayMan1();
void DisplayMan2();
void DisplayMan3();
void DisplayMan4();
void DisplayMan5();
void DisplayMan6();
void DisplayMan7();


int main()
{

    const int MAX_WRONG = 7;  // incorrect guesses allowed
    void (*pfnaDisplayMan[])() = {DisplayMan0, DisplayMan1, DisplayMan2, DisplayMan3, DisplayMan4, DisplayMan5, DisplayMan6, DisplayMan7};

    vector<string> words;  // Level 1 
    words.push_back("GREEN");
    words.push_back("BANANA");
    words.push_back("LAPTOP");
    words.push_back("GIRAFFE");
    words.push_back("PENCIL");

    vector<string> wordsD1;  // Level 2
    wordsD1.push_back("DELICIOUS");
    wordsD1.push_back("COMPUTING");
    wordsD1.push_back("SOFTWARE");
    wordsD1.push_back("HARDWARE");
    wordsD1.push_back("TELEPHONE");

    vector<string> wordsD2; // Level 3
    wordsD2.push_back("BAMBOOZLED");
    wordsD2.push_back("DAYDREAMER");
    wordsD2.push_back("CANNIBALISM");
    wordsD2.push_back("NERVOUSLY");
    wordsD2.push_back("APPROACHING");


    srand((unsigned int)time(0));


    string THE_WORD;
    string soFar;
    int wordLength;
    string used;                         // letters already guessed


      cout << "\t\t HANGMAN\n";

    cout << "Please enter a difficulty level [1-3] ";
    int dif = 0;
    while(dif < 1 || dif > 3)
    {
        cin >> dif;
    }
    cout << "You have chosen difficulty level : "<< dif << endl;


    if(dif == 1)
    {
        random_shuffle(words.begin(), words.end());
        int incorrectGuesses = 0;                            // number of incorrect guesses
        string const THE_WORD = words[0];         // word to guess
        string soFar(THE_WORD.size(), '*');       // word guessed so far
        // count length of randomly chosen string and display it 
        wordLength = THE_WORD.length();
    }
    if(dif == 2)
    {
        random_shuffle(wordsD1.begin(), wordsD1.end());
        int incorrectGuesses = 0;                            // number of incorrect guesses
        string const THE_WORD = wordsD1[0];        
        string soFar(THE_WORD.size(), '*');  
        wordLength = THE_WORD.length();
    }
    if(dif == 3)
    {
        random_shuffle(wordsD2.begin(), wordsD2.end());
        int incorrectGuesses = 0;                            // number of incorrect guesses
        string const THE_WORD = wordsD2[0];        
        string soFar(THE_WORD.size(), '*');       
        wordLength = THE_WORD.length();
    }




    // main loop
    while ((incorrectGuesses < MAX_WRONG) && (soFar != THE_WORD))
    {
         cout << "\n- There are : "<< wordLength <<" letters in the word :\t" << soFar << endl;
        cout << "\n- You have guessed  " <<incorrectGuesses << " times wrong out of "<< MAX_WRONG  << " allowed wrong guesses.\n";
        cout << "\nLetters used : " << used << endl;

        cout << "=====================================================";

        char guess;
        cout << "\n\t\tEnter a letter :  ";
        cin >> guess;

        guess = toupper(guess); //make uppercase since secret word in uppercase

        while (used.find(guess) != string::npos)
        {
            cout << "\nYou've already guessed the letter " << guess << endl;
            cout << "Enter another letter / word: ";
            cin >> guess;

            guess = toupper(guess);
        }

        used += guess;

        if (THE_WORD.find(guess) != string::npos)
        {
            cout << "=====================================================\n";
            cout << "- Correct, The letter " << guess << " is in the word.\n";


            // update soFar to include newly guessed letter
            for (int i = 0; i < THE_WORD.length(); ++i)
                if (THE_WORD[i] == guess)
                    soFar[i] = guess;
        }
        else
        {
            cout << "Sorry, " << guess << " isn't in the word.\n";
            ++incorrectGuesses;
            pfnaDisplayMan[incorrectGuesses]();

        }
    }

    // shut down
    if (incorrectGuesses == MAX_WRONG)
        cout << "\nYou've been hanged!";
    else
        cout << "\nYou guessed it!";

    cout << "\nThe word was " << THE_WORD << endl;

    return 0;
}

    void DisplayMan0() 
{
    using namespace std;
    cout << "_______" << endl;
    cout << "|     |" << endl;
    cout << "|" << endl;
    cout << "|" << endl;
    cout << "|" << endl;
    cout << "|" << endl;
    cout << "|" << endl;
    cout << "________" << endl;
}

void DisplayMan1()
{
    using namespace std;
    cout << "_______" << endl;
    cout << "|     |" << endl;
    cout << "|     o" << endl;
    cout << "|" << endl;
    cout << "|" << endl;
    cout << "|" << endl;
    cout << "|" << endl;
    cout << "________" << endl;
}

void DisplayMan2() 
{
    using namespace std;
    cout << "_______" << endl;
    cout << "|     |" << endl;
    cout << "|     o" << endl;
    cout << "|    /" << endl;
    cout << "|" << endl;
    cout << "|" << endl;
    cout << "|" << endl;
    cout << "________" << endl;
}

void DisplayMan3() 
{
    using namespace std;
    cout << "_______" << endl;
    cout << "|     |" << endl;
    cout << "|     o" << endl;
    cout << "|    /X" << endl;
    cout << "|" << endl;
    cout << "|" << endl;
    cout << "|" << endl;
    cout << "________" << endl;
}

void DisplayMan4() 
{
    using namespace std;
    cout << "_______" << endl;
    cout << "|     |" << endl;
    cout << "|     o" << endl;
    cout << "|    /X\\" << endl;
    cout << "|" << endl;
    cout << "|" << endl;
    cout << "|" << endl;
    cout << "________" << endl;
}

void DisplayMan5() 
{
    using namespace std;
    cout << "_______" << endl;
    cout << "|     |" << endl;
    cout << "|     o" << endl;
    cout << "|    /X\\" << endl;
    cout << "|    /" << endl;
    cout << "|" << endl;
    cout << "|" << endl;
    cout << "________" << endl;
}

void DisplayMan6() 
{
    using namespace std;
    cout << "_______" << endl;
    cout << "|     |" << endl;
    cout << "|     o" << endl;
    cout << "|    /X\\" << endl;
    cout << "|    / \\" << endl;
    cout << "|" << endl;
    cout << "|" << endl;
    cout << "________" << endl;
}
void DisplayMan7() 
{
    using namespace std;
    cout << "\t\t_______" << endl;
    cout << "\t\t|DONT"   << endl;
    cout << "\t\t|HANG"   << endl;
    cout << "\t\t|THE"    << endl;
    cout << "\t\t|MAN"    << endl;
    cout << "\t\t|             O" << endl;
    cout << "\t\t| _______    /XL" << endl;
    cout << "\t\t__|_____|   / \\" << endl;
}
4

3 回答 3

4

退出incorrectGuesses这些范围。因为在这些范围之外,这个变量没有被声明。

if(dif == 1)
{
    int incorrectGuesses = 0; 
    ...
}
if(dif == 2)
{
    int incorrectGuesses = 0; 
    ...
}
if(dif == 3)
{
    int incorrectGuesses = 0;
    ...
}

应该

int incorrectGuesses = 0; 

if(dif == 1)
{
    ...
}
if(dif == 2)
{
    ...
}
if(dif == 3)
{
    ...
}

soFar的问题相同。那部分代码应该是这样的:THE_WORDwordLength

string THE_WORD;
string soFar;
int wordLength;
string used;

// cout ... cin ....


int incorrectGuesses = 0;


if(dif == 1)
{
    random_shuffle(words.begin(), words.end());

    THE_WORD = words[0];         // word to guess
    wordLength = THE_WORD.length();
}
if(dif == 2)
{
    random_shuffle(wordsD1.begin(), wordsD1.end());
    THE_WORD = wordsD1[0];
    wordLength = THE_WORD.length();
}
if(dif == 3)
{
    random_shuffle(wordsD2.begin(), wordsD2.end());
    THE_WORD = wordsD2[0];
    wordLength = THE_WORD.length();
}

soFar.assign(THE_WORD.size(), '*');
于 2013-11-04T19:27:26.743 回答
1

M M.是正确的。您重新声明变量。只是一个小评论。我会使用 Switch Case 而不是一组 if 语句。改变:

if(dif==1){}
if(dif==2){}
if(dif==3){}

进入

switch(dif){
   case(1):
      break;
   case(2):
      break;
   case(3):
      break;
}

不一定是为了可读性,而是更多地表明 dif 的值不会根据其值进行编辑。例如: 选项 1:

dif = 1;
if(dif==1){ dif = 3; }
if(dif==2){}
if(dif==3){ dif = 7; }

对比:选项 2

dif = 1;
switch(dif){
   case(1):
      dif = 3;
      break;
   case(2):
      break;
   case(3):
      dif = 7;
      break;
}

选项 1 输出:7
选项 2 输出:3

于 2013-11-04T19:40:45.970 回答
0

您声明incorrectGuesses超出范围。它永远不会被声明或赋值。在函数的开头声明它并在其他范围内为其赋值。

于 2013-11-04T19:27:57.520 回答