2013-04-23 49 views
0

正如標題狀態,我使用函數的第一次,我有困難,實現他們進入劊子手遊戲。從我有限的知識中,我假設我的變量的範圍遠離函數調用。鑑於我正在完全脫離一本書,我覺得我已經打了一堵牆。我在想可能要用全局變量,但是再一次,我正在研究的這一章只給了我對它們的有限理解。我也有srand(time(0))的問題;出於某種原因。任何幫助將不勝感激,並提前感謝。C++第1時間正與功能

// HangMan Game 
// With Functions 

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

using namespace std; 

char playerGuess(); 
char guessInWord(); 

int main() 
{ 
    //setup 
    const int MAX_WRONG = 8;//max wrong will be set at 8 

    vector<string> words; //collection of possible words to guess 
    words.push_back ("DAFTPUNK"); 
    words.push_back ("ELSALVADOR"); 
    words.push_back ("HIPHOP"); 

    srand(time(0)); 
    random_shuffle(words.begin(), words.end()); 
    const string THE_WORD = words[0];   //word to guess 
    int wrong = 0;        //number of incorrect guesses 
    string soFar(THE_WORD.size(), '-');   //word guessed so far 
    string used = "";       //letters already guessed 

    cout << "Welcome to Hangmam. Good luck!\n"; 

    //main loop 
    while ((wrong < MAX_WRONG) && (soFar != THE_WORD)) 
    { 
     cout << "\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n"; 
     cout << "\nYou've used the following letters:\n" << used << endl; 
     cout << "\nSo far, the word is:\n" << soFar << endl; 

     char guess = playerGuess(); //call to function 1 

     used += guess; 

     guessInWord();    //call to function 2 
    } 

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

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

    return 0; 
} 

//function Definitions 1 
char playerGuess() 
{ 
    char letter; 
    cout << "\n\nEnter your guess: "; 
    cin >> letter; 
    letter = toupper(letter); //makes uppercase of letter 
    while (used.find(letter) != string::npos) 
    { 
     cout <<"\nYou've already guessed " << letter << endl; 
     cout <<"Enter your guess: "; 
     cin >> letter; 
     letter = toupper(letter); 
    } 
    return letter; 
} 

//function Definitions 2 
void guessInWord() 
{ 
    if (THE_WORD.find(guess) != string::npos) 
    { 
     cout << "That's right! " << 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"; 
     ++wrong; 
    } 
} 
+0

您面臨的問題是什麼? – 2013-04-23 04:04:52

+0

你在哪裏撞牆? – 2013-04-23 04:05:14

+1

@ThuggedOutNerd:您應該在codereview.stackexchange.com代碼評論中發佈此代碼。 – refi64 2013-04-23 04:09:11

回答

1

你的函數原型不應該不同於它的實現:

void guessInWord(); ------> char guessInWord(); 
^^^^       ^^^^ 

讓這些全球性的(我個人不喜歡使全球),因爲你在其他功能使用起來:

vector<string> words; 
string used; 
string THE_WORD; 
int wrong; 
string soFar; 
char guess; 

當然你將有運行時錯誤或邏輯的問題,繼續前進......

char playerGuess(); 
char guessInWord(); 

vector<string> words; 
string used = ""; 
string THE_WORD; 
int wrong = 0; 
string soFar(THE_WORD.size(), '-'); 
char guess; 

int main() 
{ 
    const int MAX_WRONG = 8; 

    words.push_back("DAFTPUNK"); 
    words.push_back("ELSALVADOR"); 
    words.push_back("HIPHOP"); 

    THE_WORD = words[0]; 

    srand(time(0)); 
    random_shuffle(words.begin(), words.end()); 

    cout << "Welcome to Hangmam. Good luck!\n"; 

    while ((wrong < MAX_WRONG) && (soFar != THE_WORD)) 
    { 
     cout << "\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n"; 
     cout << "\nYou've used the following letters:\n" << used << endl; 
     cout << "\nSo far, the word is:\n" << soFar << endl; 

     guess = playerGuess(); 
     used += guess; 
     guessInWord(); 
    } 

    if (wrong == MAX_WRONG) 
     cout << "\nYou've been hanged!"; 
    else 
     cout << "\nYou guessed it!"; 

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

    return 0; 
} 

char playerGuess() 
{ 
    char letter; 
    cout << "\n\nEnter your guess: "; 
    cin >> letter; 
    letter = toupper(letter); //makes uppercase of letter 
    while (used.find(letter) != string::npos) 
    { 
     cout << "\nYou've already guessed " << letter << endl; 
     cout << "Enter your guess: "; 
     cin >> letter; 
     letter = toupper(letter); 
    } 
    return letter; 
} 

char guessInWord() 
{ 
    if (THE_WORD.find(guess) != string::npos) 
    { 
     cout << "That's right! " << guess << " is in the word.\n"; 

     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"; 
     ++wrong; 
    } 
} 
+0

其中一個是無效的,另一個字符是我嘗試不同的東西來讓它工作的不好之處......我一直在嘗試很多東西,我忘記了將它們切換回來。謝謝,我會給它一個。 – ThuggedOutNerd 2013-04-23 04:30:53