2017-09-15 3 views
-2

私はC++を初めて使用しており、割り当てに問題があります。私は、二重の白いペグを数えながら、私が抱えている問題を理解することはできません。誰かが助けてくれますか?私は "または"声明だけを使用しているので私は二重カウントの問題があることを理解することができますが、私は白いペグから黒いペグを差し引く必要があると信じています。C++ Mastermind Double White Peg Issue

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

using namespace std; 

int main() { 

    char colors[4]; 

    srand(time(0)); 
    int randomint = (rand() % 5) + 1; 

    for (int i = 0;i<4;i++) { 
     randomint = (rand() % 5) + 1; 

     switch (randomint) { 
     case 1: 
      colors[i] = 'R'; 
      break; 
     case 2: 
      colors[i] = 'B'; 
      break; 
     case 3: 
      colors[i] = 'Y'; 
      break; 
     case 4: 
      colors[i] = 'P'; 
      break; 
     case 5: 
      colors[i] = 'G'; 
      break; 
     case 6: 
      colors[i] = 'Bl'; 
      break; 
     case 7: 
      colors[i] = 'R'; 
      break; 
     case 8: 
      colors[i] = 'O'; 
      break; 
     case 9: 
      colors[i] = 'T'; 
      break; 
     } 
    } 


    char usercolors[4]; 

    cout << "We have our colors!" << endl; 
    cout << endl << endl; 
    int turncounter = 0; 
    while (turncounter != 12) { 
     turncounter++; 

     cout << "Current try: " << turncounter << endl; 

     for (int i = 0;i<4;i++) { 
      cout << "Color " << i << ": "; 
      cin >> usercolors[i]; 
      cout << endl; 
     } 

     for (int i = 0;i<4;i++) { 
      if (usercolors[i] == colors[i]) 
       cout << "Black Peg" << " "; 
     } 

     if (usercolors[0] == colors[1] || 
      usercolors[0] == colors[2] || 
      usercolors[0] == colors[3]) { 
      cout << "White Peg" << " "; 
     } 
     if (usercolors[1] == colors[0] || 
      usercolors[1] == colors[2] || 
      usercolors[1] == colors[3]) { 
      cout << "White Peg" << " "; 
     } 
     if (usercolors[2] == colors[0] || 
      usercolors[2] == colors[1] || 
      usercolors[2] == colors[3]) { 
      cout << "White Peg" << " "; 
     } 
     if (usercolors[3] == colors[0] || 
      usercolors[3] == colors[1] || 
      usercolors[3] == colors[2]) 
     { 
      cout << "White Peg" << " "; 
     } 

     cout << endl << endl; 

     if (usercolors[0] == colors[0] && 
      usercolors[1] == colors[1] && 
      usercolors[2] == colors[2] && 
      usercolors[3] == colors[3]) 
     { 
      cout << "You win! Number of tries: " << turncounter << endl; 
      turncounter = 12; 
     } 
     else { 
      cout << "Try Again!" << endl << endl; 
     } 

    } 
    if (turncounter == 12) { 
     cout << "Sorry, you are incorrect!" << endl; 
     cout << "Answer: "; 
     cout << "Color 1: " << colors[0] << "\t" << "Color 2: " << colors[1] << "\t" << "Color 3: " << colors[2] << "\t" << "Color 4: " << colors[3] << "\t" << endl; 
    } 

    cin.get(); 
    cin.get(); 
    return 0; 
} 
+0

'(RAND()5%)+ 1 '生じることはありません6、7、8または9 – rustyx

+0

これは色[I] = 'BLは' 動作してはなりません。色はcharで、二重引用符で囲んだ2文字です。代わりに大文字のBと小文字のbを使用しますか?黒と青のために。または、列挙型を使用します。 – Rob

+3

デバッガを使用してステップスルーし、最初に何が起こるかを観察します。 – John

答えて

0

白いペグを数えるときは、黒いペグが検出されたすべての位置を無視する必要があります。だから、黒のペグを検出した後、あなたがやって白色のペグを検出しようとしている:あなたはすでに気づいたよう

if (usercolors[0] == colors[1] || 
    usercolors[0] == colors[2] || 
    usercolors[0] == colors[3]) { 
    cout << "White Peg" << " "; 
} 

は、このアプローチは、ダブルカウントにつながります。 1つの例:秘密のパターンには、最初の位置に緑色のペグが含まれています。今度はあなたの推測を入力してください:最初のポジションはグリーン、4番目は緑です。あなたのコードは、最初の位置にある黒色のペグと、少なくとも2つの追加の白いペグを検出します。これは、動作するはずの方法ではありません。黒いペグの位置は、白いペグを数えながら無視しなければならず、すでに検出されたすべての白いペグをカウントしなければなりません。

コードを変更する必要があります。検出された黒色ペグの位置をマップ(match_map)に書き込み、最初の白いペグのマッチの後に内側のループを残すことで、二重白ペグのカウントを回避できます(リストにも書き込まなければなりません)。カラーリストに「R」が2回あることにご注意ください。 'Bt'を文字として使うことはできません。私はそれを 'b'に置き換えました。 switch()を使わずに色の配列を使用すれば、シークレットパターンを処理するには単一のループで十分です。

名前空間を使用してを避けます。 - 説明のためにhereを見てください - それは悪い説明とみなされ、さらなる問題につながる可能性があります。

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

enum { 
    PEG_NO_MATCH, 
    PEG_BLACK, 
    PEG_WHITE, 
}; 

int main() { 
    char colors[4], usercolors[4], match_map[4]; 
    const char rand_colors[8] = { 'R', 'B', 'Y', 'P', 'G', 'b', 'O', 'T' }; // B -> Black, b -> Blue 
    int turncounter = 0, black_cnt, white_cnt; 

    std::srand(time(0)); 

    for (int i = 0; i < 4; i++) colors[i] = rand_colors[std::rand() % 8]; 

    std::cout << "We have our colors!" << std::endl << std::endl << std::endl; 

    while (turncounter != 12) { 
     turncounter++; 

     std::cout << "Current try: " << turncounter << std::endl; 

     black_cnt = 0; 
     white_cnt = 0; 

     // Get input and count black pegs 
     for (int i = 0; i < 4; i++) { 
      std::cout << "Color " << i << ": "; 
      std::cin >> usercolors[i]; 
      std::cout << std::endl; 

      if (usercolors[i] == colors[i]){ 
       black_cnt++; 
       match_map[i] = PEG_BLACK; 
      }else{ 
       match_map[i] = PEG_NO_MATCH; 
      } 
     } 

     // Count white pegs 
     for (int i = 0; i < 4; i++) { 
      if (match_map[i] != PEG_BLACK){ 
       for (int k = 0; k < 4; k++) { 
        if ((i != k) && (match_map[k] == PEG_NO_MATCH) && (usercolors[i] == colors[k])){ 
         match_map[k] = PEG_WHITE; 
         white_cnt++; 
         break; 
        } 
       } 
      } 
     } 

     std::cout << std::endl << std::endl; 

     // Display result 
     std::cout << "Black Pegs : " << black_cnt << std::endl; 
     std::cout << "White Pegs : " << white_cnt << std::endl; 

     // Do all pegs match? 
     if (black_cnt == 4) 
     { 
      std::cout << "You win! Number of tries: " << turncounter << std::endl; 
      break; 
     } 
     else { 
      std::cout << "Try Again!" << std::endl << std::endl; 
     } 
    } 

    if (turncounter == 12) { 
     std::cout << "Sorry, you are incorrect!" << std::endl; 
     std::cout << "Answer: " << "Color 1: " << colors[0] << "\t" << "Color 2: " << colors[1] << "\t" << "Color 3: " << colors[2] << "\t" << "Color 4: " << colors[3] << "\t" << std::endl; 
    } 

    // Wait for input 
    std::cin.get(); 
    std::cin.get(); 

    return 0; 
} 
+0

ようこそスタックオーバーフロー!この質問は、単に働くコードではなく、*説明*を探しています。あなたの答えは質問者の洞察をほとんど提供せず、削除される可能性があります。観察された症状を引き起こす原因と、元のコードからどのようにコードが到達したかを説明するために[編集]してください。また、 'namespace std;を使うのを避ける理由についても言及したいかもしれません。 –

+0

@トビー:私はさらに説明を追加します。 – CheviN

+0

@ sp2danny:複数回一致する例を挙げてください。 – CheviN