2013-11-04 153 views
0

我已经开始使用C++,我正在创建一个hang子手游戏,我的代码运行良好,直到我选择了三个不同的难度级别,我的游戏询问用户他们想玩的难度级别,而不是实际玩游戏,它会直接跳到结尾说明用户已经正确猜测了这个单词。任何帮助感谢! 代码如下:为什么我的游戏循环没有执行

#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; 
} 
+1

您的代码不编译。 test.cpp:99:13:错误:'incorrectGuesses'未在此范围内声明 while((incorrectGuesses

+0

好的,摆脱函数中的所有'using namespace std;'。你会输入的比你要的'std ::'更多。如果你必须拥有它(我建议不要),把它放在顶部。 – crashmstr

+3

另外,你有没有试过使用调试器? – DogDog

回答

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) 
{ 
    ... 
} 

soFarTHE_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(), '*'); 
+0

非常感谢您的帮助!当我做出这些改变时,它仍然会做同样的事情 - 跳过实际的游戏循环本身!你能提出其他改变吗?谢谢 – iAsk

+0

@Ranan:你对另一个变量有同样的问题,[THIS](http://ideone.com/ULhigq)是一个工作代码。复制/粘贴并享受它。 – deepmax

+0

非常感谢你的男人! – iAsk

0

您声明incorrectGuesses超出范围。它从不声明或分配一个值。在函数的开头声明它,并在其他作用域中赋值。

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