2016-08-26 7 views
-3

現在私はメモリゲームを書いています。どのようにプログラムが動作する:2つの異なる2D配列を照合して一致を見つけるC++

誰かが推測すると、それはタイルをチェックして、ボットの2D配列にタイルを保存します。これにより、ボットは物がボード上のどこにあるかを記憶することができます。

答えを含む2次元配列とロボットの2次元配列の一致をチェックする2次元配列のループを作成しようとしています。一致がしかし、私のプログラムは、現時点ではそのように動作していない、私は値を保存したい発見された

私はちょうど私が私のforループと間違ってやっているかについて、いくつかの洞察をしたいと思います。 私はC++テキストブックのいくつかを見て回って何かを見つけることができませんでした。 Output

//2d Arrays 
//bot array changes each time someone guesses 

string botArray[6][6] = { 
    { "-", "-", "-", "-", "-", "-" }, 
    { "-", "-", "-", "-", "-", "-" }, 
    { "-", "-", "-", "-", "-", "-" }, 
    { "-", "-", "-", "-", "-", "-" }, 
    { "-", "-", "-", "-", "-", "-" }, 
    { "-", "-", "-", "-", "-", "-" } 
}; 
string pairs[6][6] = { 
{ "a", "a", "b", "b", "c", "c" }, 
{ "d", "d", "e", "e", "f", "f" }, 
{ "g", "g", "h", "h", "i", "i" }, 
{ "j", "j", "k", "k", "l", "l" }, 
{ "m", "m", "n", "n", "o", "o" }, 
{ "p", "p", "q", "q", "r", "r" } 
}; 

        for (int r1 = 0; r1 < 6; r1++) { 
        for (int c1 = 0; c1 < 6; c1++) { 
         for (int r2 = 0; r2 < 6; r2++) { 
          for (int c2 = 0; c2 < 6; c2++) { 
           if (botArray[r1][c1] == pairs[r1][c1]){ 
            if(botArray[r2][c2] == pairs[r2][c2]) { 
            if (r1 != r2 && c1 != c2) { 
            row1 = r1; 
            col1 = c1; 
            row2 = r2; 
            col2 = c2; 
           } 
           } 
           else{ 
            row1 = rand() % 6 + 1; 
            row2 = rand() % 6 + 1; 
            col1 = rand() % 6 + 1; 
            col2 = rand() % 6 + 1; 
           } 
          } 
         } 
        } 
       } 
      } 
//values are printed outside of the for loop after 
//they are assigned in the if statement 
+3

ようこそスタックオーバーフロー!現在の出力は何ですか?デバッガを使用してコードをステップ実行する方法を学ぶ必要があるかもしれないようです。良いデバッガを使用すると、プログラムを1行ずつ実行し、どこからずれているかを確認することができます。これはプログラミングをする場合に不可欠なツールです。詳しい読書:** [小さなプログラムをデバッグする方法](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** –

+0

@FirstStepあなたが見ることができる出力写真ボットの2D配列に格納されているマッチがあるが、ボットはマッチしていることを認識していないこともある –

+1

質問には完全な情報が含まれている必要があります。外部サイトへのリンクは、いつでも機能しなくなり、アーカイブされた質問が無意味になり、受け入れられなくなります。 –

答えて

0

現在、あなたのロジックはと間違っている(コメントを追加)

// Loop 
if (botArray[r1][c1] == pairs[r1][c1]) { // Bot knows that case 
    if (botArray[r2][c2] == pairs[r2][c2]) { // Bot knows that case 
     if (r1 != r2 && c1 != c2) { // Different case 
      row1 = r1; 
      col1 = c1; 
      row2 = r2; 
      col2 = c2; 
     } 
    } else { // Hit each time that we found a second unknown case 
      // So overwrite previous found solution 
     row1 = rand() % 6 + 1; 
     row2 = rand() % 6 + 1; 
     col1 = rand() % 6 + 1; 
     col2 = rand() % 6 + 1; 
    } 
} 

それは

// Loop 
if ((r1 != r2 || c1 != c2) // Different case 
    && botArray[r1][c1] != "-" // Known card 
    && botArray[r1][c1] == botArray[r2][c2]) // Pair found 
    // Missing: Not already displayed 
{ 
    row1 = r1; 
    col1 = c1; 
    row2 = r2; 
    col2 = c2; 
    return; // No need more processing 
} 

// and after loop, to handle when no pair found 
do { 
    row1 = rand() % 6 + 1; // Random play 
    row2 = rand() % 6 + 1; 
    col1 = rand() % 6 + 1; 
    col2 = rand() % 6 + 1; 
} while ((row1 != row2 || col1 != col2) // Different case 
     // Potentially other condition, as at least one unknown case 
    ) 

のようなものである必要があり、私はそれをのようなものを実装します:

class BotPlayer 
{ 
public: 
    explicit BotPlayer(std::size_t size) { 
     for (std::size_t i = 0; i != size; ++i) { 
      data.emplace_back(i, ""); 
     } 
    } 

    std::pair<std::size_t, std::size_t> selectMove() { 
     // Try to find 2 cards with same value 
     std::sort(data.begin(), data.end(), [](const auto& lhs, const auto& rhs){ return lhs.second < rhs.second; }); 
     auto it = std::adjacent_find(data.begin(), data.end(), [](const auto& lhs, const auto& rhs){ return lhs.second == rhs.second && !lhs.second.empty(); }); 

     if (it == data.end()) { // No known pairs -> play randomly (but choose unknown cards) 
      auto isUnknown = [](const auto& e){ return e.second.empty(); }; 
      it = std::partition(data.begin(), data.end(), isUnknown); 
      std::shuffle(data.begin(), it, rnd); 
      it = data.begin(); 
     } 
     return {it->first, std::next(it)->first}; 
    } 

    void reveal(std::size_t pos, const std::string& value) 
    { 
     // Update our knowledge. 
     std::cout << "> " << pos << " is " << value << std::endl; 
     auto it = std::find_if(data.begin(), data.end(), [&](const auto& p) { return p.first == pos; }); 

     it->second = value; 
    } 

    void foundPair(std::size_t pos1, std::size_t pos2) 
    { 
     // So we don't have to worry about those cards anymore 
     data.erase(std::find_if(data.begin(), data.end(), [&](const auto& p) { return p.first == pos1; })); 
     data.erase(std::find_if(data.begin(), data.end(), [&](const auto& p) { return p.first == pos2; })); 
    } 

private: 
    std::mt19937 rnd; 

    // for each remaining position to play, 
    // associate the associated value, 
    // empty for unknown. 
    std::vector<std::pair<int, std::string>> data; 
}; 

Demo

関連する問題