2014-11-07 84 views
0

現在我正試圖讓我的記分卡爲hang子手工作。這是一款我可以隨心所欲玩的遊戲,當我在遊戲菜單中按2時,我希望它能顯示我的最低分數。出於某種原因,我的邏輯不起作用。誰能幫忙?我附上了這部分工作所需的3部分代碼。記分卡Hang子手。 C++

#include <iostream> 
#include <cstdlib> 
#include <ctime> 
#include <string> 
#include <fstream> 

using namespace std; 

int maxAttempts = 10; //max attempts possible 
void poorStiff(int); 
int wordFill(char, string, string&); 


int main() 
{ 


    int intro; 
    int bodyPart = 0; 
    ifstream wordIn; //file stream read in 
    wordIn.open("WordList.txt"); //list of words used in this game 
    //if (!wordIn.good()) { std::cerr << "open failed.\n"; exit(1); } 
    char letter; //letter guessed 
    int numWrongGuesses = 0; //counts the number of wrong guesses 
    string theWord; 
    string words[13]; 
    int play = 0; 
    bool run = true; 
    int makeYourSelection = 0; 
    int bestScore = 11; 

    while (run == true) 
    { 
     int attemptsGame = 0; 
     // put a game menu to loop that asks if you want to keep playing or exit and go to scorecard 
     cout << "   WELCOME TO THE GAME OF HANGMAN\n\n\n\n"; 
     cout << " Press 1 to play the game, press 2 to exit to the scorecard \n\n"; 
     cout << " You have 10 attempts to guess the words before you get hung \n\n"; 
     cin >> play; 


     while (play == 1)//loops while user has entered 1, 0 exits the game 
     { 
      for (int i = 0; i < 13; i++) //labeled 13 and not words because 13 is a constant 
      { 
       wordIn >> words[i];//replaces the file words and puts them in an array and counts them out of the file 
       cout << words[i] << endl; 
      } 
      srand(time(NULL));//to get a random word 
      int n = rand() % 12; 
      theWord = words[n]; //pulls word from file 
      wordIn.close(); 

      string mystery(theWord.length(), '*'); //replaces word letters with asterisks 



      while (numWrongGuesses < maxAttempts) // while the amount of guesses is less than the max wrong guesses 
      { 
       cout << mystery << endl << endl; 
       cout << "You now have the length of the word represented by the *'s. \n\n"; 
       cout << "Guess a letter \n\n"; 
       cin >> letter; 


       if (wordFill(letter, theWord, mystery) == 0) //fuction call 
       { 
        bodyPart++; 
        poorStiff(bodyPart); 
        cout << "You have entered a letter that isn't in the word, guess again. \n\n"; 
        numWrongGuesses++; 
        attemptsGame++; 
       } 
       else 
       { 
        for (int i = 0; i < mystery.length(); i++) 
        { 
         if (theWord[i] == letter) 
         { 
          mystery[i] = letter; 
         } 
        } 
        cout << "You have found one of the letters. Congratulations! \n\n"; 
        cout << "You have: " << maxAttempts - numWrongGuesses; 
        cout << " guesses left \n\n" << endl; 
       } 

       if (theWord == mystery) // the word is the same as mystery 
       { 
        cout << theWord << endl; 
        cout << "\n\n Awesome, you guessed it. \n\n"; 
        break; 
        if (attemptsGame < bestScore) 
        { 
         bestScore = attemptsGame; 

        } 
       } 
      } 
      if (numWrongGuesses == maxAttempts) //when you run out of guesses 
      { 
       cout << "Too bad, you ran out of guesses and have been hung at the gallows. \n\n"; 
       cout << "The word you were trying to guess was " << theWord << endl; 
       poorStiff(bodyPart); 
      } 
      cin.ignore(); 
      cin.get(); 

      break; 
     } 
      while (play == 2) 
      { 
       cout << "Best Scores: \n\n"; 
       cout << bestScore << endl; 
       system("pause"); 
       return 0; 
       break; 
      } 
     } 
     system("pause"); 
     return 0; 
    } 


    int wordFill(char guess, string theWordSecret, string&guessWord) //function for determing if you guess a letter contained 
    { 
     int i; 
     int hits = 0; //letter hits within the word 
     int many = theWordSecret.length(); 

     for (i = 0; i < many; i++) 
     { 
      if (guess == guessWord[i]) 
       return 0; 
      if (guess == theWordSecret[i]) 
      { 
       guessWord[i] == guess; 
       hits++; 
      } 
     } 
     return hits; 
    } 

    void poorStiff(int bodyPart) 
    { 

     if (bodyPart == 1) 
     { 
      cout << "_______" << endl; 
      cout << "|  }" << endl; 
      cout << "|  O" << endl; 
      cout << "|" << endl; 
      cout << "|" << endl; 
      cout << "|" << endl; 
      cout << "|" << endl; 
      cout << "_______________" << endl; 

     } 
     else if (bodyPart == 2) 
     { 
      cout << "_______" << endl; 
      cout << "|  }" << endl; 
      cout << "|  O" << endl; 
      cout << "|  |" << endl; 
      cout << "|" << endl; 
      cout << "|" << endl; 
      cout << "|" << endl; 
      cout << "_______________" << endl; 

     } 
     else if (bodyPart == 3) 
     { 
      cout << "_______" << endl; 
      cout << "|  }" << endl; 
      cout << "|  O" << endl; 
      cout << "|  /|" << endl; 
      cout << "|" << endl; 
      cout << "|" << endl; 
      cout << "|" << endl; 
      cout << "_______________" << endl; 

     } 
     else if (bodyPart == 4) 
     { 
      cout << "_______" << endl; 
      cout << "|  }" << endl; 
      cout << "|  O" << endl; 
      cout << "|  /|" << endl; 
      cout << "| /" << endl; 
      cout << "|" << endl; 
      cout << "|" << endl; 
      cout << "_______________" << endl; 

     } 
     else if (bodyPart == 5) 
     { 
      cout << "_______" << endl; 
      cout << "|  }" << endl; 
      cout << "|  O" << endl; 
      cout << "|  /|" << endl; 
      cout << "| /|" << endl; 
      cout << "|" << endl; 
      cout << "|" << endl; 
      cout << "_______________" << endl; 

     } 
     else if (bodyPart == 6) 
     { 
      cout << "_______" << endl; 
      cout << "|  }" << endl; 
      cout << "|  O" << endl; 
      cout << "|  /|\." << endl; 
      cout << "| /| \." << endl; 
      cout << "|" << endl; 
      cout << "|" << endl; 
      cout << "_______________" << endl; 

     } 
     else if (bodyPart == 7) 
     { 
      cout << "_______" << endl; 
      cout << "|  }" << endl; 
      cout << "|  O" << endl; 
      cout << "|  /|\." << endl; 
      cout << "| /| \." << endl; 
      cout << "|  /" << endl; 
      cout << "|" << endl; 
      cout << "_______________" << endl; 

     } 
     else if (bodyPart == 8) 
     { 
      cout << "_______" << endl; 
      cout << "|  }" << endl; 
      cout << "|  O" << endl; 
      cout << "|  /|\." << endl; 
      cout << "| /| \." << endl; 
      cout << "| /\." << endl; 
      cout << "|" << endl; 
      cout << "_______________" << endl; 

     } 
     else if (bodyPart == 9){ 
      cout << "_______" << endl; 
      cout << "|  }" << endl; 
      cout << "|  O" << endl; 
      cout << "|  /|\." << endl; 
      cout << "| /| \." << endl; 
      cout << "| /\." << endl; 
      cout << "| /" << endl; 
      cout << "_______________" << endl; 

     } 
     else if (bodyPart == 10) 
     { 
      cout << "_______" << endl; 
      cout << "|  }" << endl; 
      cout << "|  O" << endl; 
      cout << "|  /|\." << endl; 
      cout << "| /| \." << endl; 
      cout << "| /\." << endl; 
      cout << "| / \." << endl; 
      cout << "_______________" << endl; 
     } 
    } 
+0

三段代碼不足以確定您的程序不工作的原因。你需要發佈整個事情,最好是削減到最低限度,以便編譯和工作。 – 2014-11-07 03:07:59

+0

不知道你想要多少,但這是整個代碼,如果有幫助。 – theboys123 2014-11-07 03:14:20

+0

你的邏輯哪部分不起作用? – gsamaras 2014-11-07 03:17:23

回答

1

你嘗試調試?如果你通過你的代碼步驟,你會立即發現問題,這是在這裏:

  if (theWord == mystery) // the word is the same as mystery 
      { 
       cout << theWord << endl; 
       cout << "\n\n Awesome, you guessed it. \n\n"; 
       break; // problem here 
       if (attemptsGame < bestScore) 
       { 
        bestScore = attemptsGame; 

       } 
       // break should be here 
      } 

你在檢查邏輯之前打破你的循環。因此,將您的break語句移到檢查之後,因爲該代碼從未得到執行。

+0

+1這是我第一眼看不到的突破(我在檢查代碼時發現它,但你已經找到了它)。 – gsamaras 2014-11-07 03:28:51

+0

是的,修復它。非常感激。 – theboys123 2014-11-07 03:29:39

+0

然後請接受我的回答,謝謝。 :) – 2014-11-07 03:30:56

1

首先啓用您的編譯器警告,您可以通過快速查看代碼獲取更多信息。

更改此

guessWord[i] == guess;

這個

guessWord[i] = guess;


此時

if (wordFill(letter, theWord, mystery) == 0) //fuction call 
{  
    ... 
    cout << "You have entered a letter that isn't in the word, guess again. \n\n"; 
} 
else 
{ 
    ... 

    if (theWord == mystery) // the word is the same as mystery 
    { 
     cout << theWord << endl; 
     cout << "\n\n Awesome, you guessed it. \n\n"; 
     break; 
     ... 
    } 
} 

在這裏,當你輸入第一個if時,那麼你將輸入第二個,如果太多,這是沒有意義的。發生這種情況是因爲theWordmystery都是空字符串!

還要注意休息應該是這部分代碼後:

if (attemptsGame < bestScore) 
{ 
    bestScore = attemptsGame;  
} 

因爲它是,這部分代碼將永遠執行。


我建議採取在這個答案(不是你的邏輯錯誤相關的)

System(「pause」); - Why is it wrong?

+1

+1。還有幾個地方會給編譯器提供警告,包括OP代碼中的問題區域。 – 2014-11-07 03:22:51