2013-11-04 5 views
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; 
} 
+1

귀하의 코드는 컴파일되지 않습니다. test.cpp : 99 : 13 : error : 'wrongGuesses'가이 스코프에 선언되지 않았습니다. (정확하지 않은 && (soFar! = THE_WORD)) ^ test.cpp : 145 : 9 : error : ' wrongGuesses '가이 범위에서 선언되지 않았습니다. if (incorrectGuesses == MAX_WRONG) –

+0

Ok, 함수에서'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; 
    ... 
} 

soFar, THE_WORDwordLength에 대한

int incorrectGuesses = 0; 

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

같은 문제이어야한다. 코드 부분은 다음과 같아야합니다.

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

@ 로난 : 당신은 다른 변수에 대해 동일한 이슈가 있습니다, [이] (http://ideone.com/ULhigq)는 작동 코드입니다. 복사/붙여 넣기를 즐기십시오. – deepmax

+0

정말 고마워요! – iAsk

0

incorrectGuesses은 범위를 벗어났습니다. 절대로 선언되거나 값이 지정되지 않습니다. 함수의 시작 부분에 선언하고 다른 범위에서 값을 지정하십시오.

1

M입니다. 맞습니다. 변수를 다시 선언했습니다. 작은 발언. 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

관련 문제