2017-10-21 127 views
1

當前我正在研究一個hang子手遊戲,我之前編碼它只能用於5個字母的單詞,但現在想要處理任何長度的單詞,我怎麼能改變這個代碼,使其工作如何我想要它?在Hang子手遊戲中檢查任意大小的字母

#include "stdafx.h" 
#include <iostream> 
#include <string> 
#include <stdlib.h> 
#include <cstdlib> 
using namespace std; 

int main() 
{ 
string word; 
int tries; 
string guess; 
string wordguess; 
string output; 

cout << "Enter a word for player two to guess: "; 
cin >> word; 
system("CLS"); 
cout.flush(); 
cout << "Guess the word!" << endl; 

for (int i = 0; i < word.length(); i++) 
{ 
cout << "_ "; 
} 

cout << "Enter a letter: "; 
cin >> guess; 

for (int tries = 5; tries > 0; tries--) 
{ 
if (guess[0] == word[0]) { 
    output[0] = word[0]; 
    cout << "You guessed the first letter! Good job!" << endl; 
} 
if (guess[0] == word[1]) { 
    output[2] = word[1]; 
    cout << "You guessed the second letter! Good job!" << endl; 
} 
if (guess[0] == word[2]) { 
    output[4] = word[2]; 
    cout << "You guessed the third letter! Good job!" << endl; 
} 
if (guess[0] == word[3]) { 
    output[6] = word[3]; 

    cout << "You guessed the fourth letter! Good job!" << endl; 
} 
if (guess[0] == word[4]) { 
    output[8] = word[4]; 
    cout << "You guessed the fifth letter! Good job!" << endl; 
} 

cout << output << endl; 
cout << "You have " << tries << " tries left. Take a guess at the word: " << endl; 
cin >> wordguess; 
if (wordguess == word) 
{ 
    cout << "Congratulations, you guessed the word correctly!" << endl; 
    break; 
} 
} 
    system("pause"); 
    return 0; 
} 

正如你可以告訴我檢查從0到4(第一到第五個字母)的每個位置。我知道有很多方法可以讓我更好地編碼,但正如你所猜測的,我是編碼新手,這是我想到的方式。請注意,這仍然是一項正在進行的工作,因此尚未完全完成。任何幫助將是偉大的!

+0

我認爲你正在尋找一個字母'的std :: set'那就是這個詞。 –

+0

聽起來像它會起作用,所以如果這個詞是「玩」,它會認爲它是一組p,l,a和y? – zhodges10

+0

'output [x]'是未定義的行爲,因爲'output'是空的(它不會自動增長字符串)。你可以在'word'中的字符上使用循環,但是你需要一些通用的方法來獲得英文單詞「first」,「second」等。 – aschepler

回答

2

在設計算法時,請想想如何在沒有計算機的情況下手動完成此操作。然後讓代碼執行相同的操作。

如果你檢查你的朋友的反對寫在沙灘上的字猜測,你可能會去像這樣:

  • 去通過字符寫入圖案的性格,在內存
  • 叨唸着你的字
  • 每個字母,檢查它是否等於猜測
  • 如果是

    • 更換PLACEHOLD呃與它
    • 記住你的朋友猜對了。
    • 還要注意是否有任何佔位符左
      • 如果沒有,你的朋友贏得
  • 最後,如果你的朋友沒有猜對,他們進球點球點並檢查它們是否丟失

現在,所有的結果都是將它放在C++中。該語言提供各種實體 - 讓我們檢查哪些適合我們需要最好的:

  • 字和當前的模式 - 一個固定大小的字符串
  • 位記憶:

    • 目前的猜測是否正確 - 布爾
    • 佔位符左 - 詮釋
    • 點球點(或等價地,左嘗試) - 詮釋
  • 部分的算法:

0
// Example program 
#include <iostream> 
#include <string> 
using namespace std; 

class my_game 
{ 
private: 
    string congrats_array[15] = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "nineth", "tenth", "eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth"}; 
    string word_to_guess; 
    int tries_left; 
    int word_length; 
    int letters_guessed_count; 
    string guessed_letters; 

    void check_letter(char letter); 
    void print_current_word_state(); 

public: 
    my_game(); 
    void begin_the_game(); 
    void play_the_game(); 
}; 

my_game::my_game() 
{ 

} 

void my_game::begin_the_game() 
{ 
    cout << "Enter a word for player to guess: " << endl; 
    cin >> word_to_guess; 
    system("CLS"); 
    cout.flush(); 

    cout << "Enter the tries amount!\n" << endl; 
    cin >> tries_left; 

    word_length = word_to_guess.size(); 

    guessed_letters = "_"; 
    letters_guessed_count = 0; 

    for(int i = 0; i < word_length - 1; i++){ 
     guessed_letters += "_"; 
    } 
} 

void my_game::play_the_game() 
{ 
    cout << "Guess the word!" << endl; 
    char letter; 

    for(int i = 0; i < tries_left; i++) 
    { 
     cout << guessed_letters << endl; 
     cout << "Enter a letter: " << endl; 
     cin >> letter; 
     check_letter(letter); 

     if(letters_guessed_count == word_length){ 
      cout << "Congrats! You won!" << endl; 
      return; 
     } 
    } 

    cout << "You lose" << endl; 
} 

void my_game::check_letter(char letter) 
{ 
    for(int i = 0; i < word_length; i++) 
    { 
     if(word_to_guess[i] == letter && guessed_letters[i] != letter) 
     { 
      guessed_letters[i] = letter; 
      letters_guessed_count++; 
      cout << "You guessed the" << congrats_array[i] <<"letter! Good job!" << endl; 
     } 
    } 
} 


int main() 
{ 
    my_game game; 
    game.begin_the_game(); 
    game.play_the_game(); 
} 
+0

你能否詳細說明爲什麼數組中有15個位置?這是否意味着這個詞只能達到15個字母,對嗎? – zhodges10

+0

正是。你可以將它添加到50,因爲最長的單詞是45個字母長度,並且在20個單詞後,你可以將單詞連接起來:20 + 1等等,所以你實際上需要25個單詞。我只是懶得xD。我認爲作者可以自己編碼。而且這個15個字母的單詞足夠長,可以第一次播放。 – Alex

+0

這是隻有「你猜對了第​​n個字母!好工作!」的限制短語,這是打印。如果您跳過它,或者像上面所說的那樣在數組中添加單詞,則可以跳過此限制。 – Alex

0

因此,簡而言之,你需要用任意長度的話要做到這一點的是用字符串的.substr()函數和字符串流庫的名爲.str()和< <和>>運營商。此版本的代碼使用一個函數,可以在正確的索引位置插入正確的猜測字符。這將在正確的地方用字母逐漸取代「_________」。在Java中這樣做更容易,但stringstream是一個很好的庫,我強烈建議熟悉它。我會留下怎樣處理推測的字符的多個實例取決於你的問題(即「我」在「書目」)

#include <string> 
using std::string; 

#include <sstream> 
using std::stringstream; 

#include <iostream> 
using std::cout; 
using std::cin; 
using std::endl; 

string newString(string, int, string); 

int main() 
{ 
    string word; 
    string guess; 
    int tries; 
    string output; 

    string input; 

    cout << "Enter word for player 2 to guess: "; 
    cin >> word; 

    stringstream ss; 

    //---------- fills the stream with "_"s matching the length of word 

    for(int i = 0; i < word.length(); i++) 
     ss << "_"; 

    //----------- assigns the initial value of "___..." to output 

    ss >> output; 

    //----------- sets up the loop 

    tries = 5; 
    bool found = false; 

    for(int i = 0; i < 5; i++) 
    { 
     cout << "\nTry " << i << " of 5: Enter a letter or guess the word: "; 
     cin >> input; 

     if(input == word) 
     { 
      cout << "Congratulations, you guessed the word correctly!" << endl; 
      break; 
     } 

     //------------------ else, proceed with replacing letters 

     if(word.find(input) != std::string::npos) 
     { 
      size_t position = word.find(input);           // finds index of first instance of the guessed letter 
      cout << "You guessed the " << position+1 << " letter! Good job!" << endl; // since strings start at index 0, position+1 

      //------- replaces appropriate "_" with the guessed letter 

      output = newString(input, position, output); 
      cout << "\n" << output; 

      // Around here you'll want to set up a way to deal with multiple instances 
      // of the same letter 

     } 
     else 
      cout << "Incorrect guess" << endl; 
    } 

    return 0; 
} 

//--------------------------------------------------- 

string newString(string guess, int index, string word) 
{ 
    string NewString; 
    stringstream temp; 

    //---------- hack up the string into sections before and after the index 

    string before = word.substr(0, index); 
    string after = word.substr(index+1, word.length() - index+1); 

    //---------------- populates the new stringstream and assigns it to the result 

    temp << before << guess << after; 

    NewString = temp.str(); 

    return NewString; 
}