2012-03-07 13 views
1
void offer_help(); 
bool play_one_game(); 

int main() { 

    offer_help(); 

    play_one_game(); 

} 


void offer_help() { 

    int help_response; 

    cout << "Need help? (0/1) "; 
    cin >> help_response; 
    if (help_response == 1) 
    cout << "I will generate a pattern of 4 numbers, each in the range 0 through 9.\n Each guess that you enter will be a line containing 4 integers,\n separated by spaces, such as:\n\t 2 4 7 1\n FOr each guess, I will echo back a lost consisting of\n 0's and 1's, with a 1 in a given position meaning that\n you guessed the number, and a zero meaning that you didn't.\n For example, if the actual solution was 2 3 6 1, I'll respond\n\t 1 0 0 1\n See how many guesses it takes you to get the solution!\n\n If you want to give up, type a negative number for one of\n your guesses, and we'll tell you what the pattern was.\n\n"; 

} 

bool play_one_game() { 

    srand(time(0)); //needed to start randint 

    vector<int> solution; //vector of 4 randomly generated 
       //solutions 
    vector<int> guess; //vector containing user guesses. 
    vector<int> result; 

    int guess_input; 

    for(int i = 0; i < solution.size(); ++i) 
    solution[i] = randint(10); 

    int trial_number = 0; //int that shows what guess the user is on 

    while (play_one_game() == true) {  
    //ask user for inputs. 
     cout << "Guess #" << ++trial_number << "? "; 
     for (int i = 0; i < guess.size(); ++i){ 
      cin >> guess_input; 
      guess.push_back(guess_input); 
     } 

     //outputs error if user inputs a letter. 
    if (!cin) { 
     cerr << "Bad input data! Feed me numbers!\n"; 
     return 43; 
    } 
    if (cin < 0){ 
     cout << "Too bad! Solution was " << endl; 
     for(int i = 0; i < result.size(); i++) 
     cout << (result[i]); 
    } 

    //determines if user correctly guessed any of the 
    //numbers and tells the user which is correct. 
    for (int i = 0; i < result.size(); i++) { 
     if (guess[i]==solution[i]) 
     cout << 1 << " "; 
     else if (guess[i]!=solution[i]) 
     cout << 0 << " "; 
    } 
    cout << endl; 

    // playagain(); 
    cout << endl << "Play again (0/1)? "; 
    int replay; 
    cin >> replay; 
    if (replay == 0) { 
     play_one_game() == false; 
     return 5; 
    } 
    else if (replay == 1) 
     play_one_game() == true; 
    else { 
     cerr << "wat?\n"; 
     return 10;  
    } 
    } 
} 

これは、プレーヤが乱数のパターンを推測できるように設計されています。ベクトル関連のセグメント化エラー

なぜセグメント化エラーが発生するのかわかりません。プログラムは、offer_help関数を呼び出してから、main関数内でplay_one_game関数を呼び出すことになっています。それから再びプレイしたいかどうかをプレイヤーに尋ねるべきです。いいえの場合は、bool play_one_gameをfalseに設定して終了する必要があります。

これは、play_one_game bool関数に関連しています。

+0

境界を確認してください。あなたは 'result.size()'まで反復していますが、 'guess [i]'と 'solution [i]'にアクセスしています。 –

答えて

5

あなたは、セグメンテーションフォールトを取得している:

while (play_one_game() == true) { 

play_one_gameはこのラインでplay_one_gameを呼び出しますが、これは、再び同じ行にplay_one_gameを呼び出します。 。これにより、最後にスタックオーバーフローが発生します。

代わりにbool keepPlaying;while(keepPlaying)を代わりに使用してください。

EDIT:まあ、これは単純な答えよりももう少しですが、私はゲームが好きなので...次のコードを見てみます:

#include <iostream> 
#include <ctime> 
#include <cstdlib> 
#include <vector> 

bool play_one_game(); 

void offer_help() { 
    int help_response; 
    std::cout << "Need help? (0/1) "; 
    std::cin >> help_response; 
    if (help_response == 1) 
     std::cout << "I will generate a pattern of 4 numbers, each in the range 0 through 9.\n" 
      "Each guess that you enter will be a line containing 4 integers,\n" 
      "separated by spaces, such as:\n" 
      "\t 2 4 7 1\n" 
      "For each guess, I will echo back a lost consisting of\n" 
      "0's and 1's, with a 1 in a given position meaning that\n" 
      "you guessed the number, and a zero meaning that you didn't.\n" 
      "For example, if the actual solution was 2 3 6 1, I'll respond\n" 
      "\t 1 0 0 1\n" 
      "See how many guesses it takes you to get the solution!\n\n" 
      "If you want to give up, type a negative number for one of\n" 
      "your guesses, and we'll tell you what the pattern was.\n\n"; 
} 

int main() { 
    offer_help(); 
    srand(time(0)); // Initialize random numbers with current time as seed 
    while(play_one_game()); // if play_one_game returns true, play again 
} 

bool play_one_game() { 
    std::vector<int> solution(4); // Four solutions for our guessing game 
    std::vector<int> guess;   // User guesses 

    for(unsigned i = 0; i < solution.size(); ++i) 
     solution[i] = rand() % 10; 

    int trial_number = 0; //int that shows what guess the user is on 
    bool keepPlaying = true; 
    while(keepPlaying){ 
     std::cout << "Guess #" << ++trial_number << "? "; 

     guess.clear(); // Clear old guesses 
     for(unsigned i = 0; i < solution.size(); ++i){ 
      int guess_input; 
      //outputs error if user inputs a letter. 
      if (!(std::cin >> guess_input)) { 
       std::cerr << "Bad input data! Feed me numbers!\n"; 
       std::cerr << "Try again!" << std::endl; 
       std::cin.clear(); // Clear flags 
       continue; 
      } 
      if (guess_input < 0){ 
       std::cout << "Too bad! Solution was " << std::endl; 
       for(unsigned i = 0; i < solution.size(); i++) 
        std::cout << (solution[i]); 
       keepPlaying = false; 
       break; 
      }else 
       guess.push_back(guess_input); 
     } 
     if(!keepPlaying) 
      break; 
     if(solution.size() != guess.size()){ 
      std::cerr << "Wrong number of guesses, try again!" << std::endl; 
      continue; 
     } 
     //determines if user correctly guessed any of the 
     //numbers and tells the user which is correct. 
     bool correct = true; 
     for (unsigned i = 0; i < solution.size(); i++) { 
      if (guess[i] == solution[i]) 
       std::cout << 1 << " "; 
      else{ 
       correct = false; 
       std::cout << 0 << " "; 
      } 
     } 
     if(correct){ 
      std::cout << "Congratulations - you won!" << std::endl; 
      break; 
     } 
     std::cout << std::endl; 
    } 
    int replay = -1; 
    do{ 
     // Ask user for input until input is 0 or 1 
     std::cout << std::endl << "Play again (0/1)? "; 
     std::cin >> replay; 
    } 
    while(replay != 0 && replay != 1); 
    return static_cast<bool>(replay); // return user replay answer (false/true) 
} 

Try to keep your code as simple as possibleを。ようこそ。 今後の回答がそれほど大きくなるとは思わないでください。

+0

スタックオーバーフロー。 'play_one_game()== false'のような呼び出しもあります。 – Johnsyweb

3

ソリューションベクターに何も挿入することは決してありません。ベクトルを宣言してから、

for(int i = 0; i < solution.size(); ++i) 
    solution[i] = randint(10); 

...この時点では何もしないので、これはsolution.size() == 0です。後で、結果ベクトルを反復処理すると、空の解ベクトルの無効な要素にアクセスすることになります。結果ベクトルと解ベクトルが同じサイズであると仮定することもできません。あなたは次の行で無限再帰に終わるので

関連する問題