2013-11-04 72 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