检查两个不同的2D数组以查找匹配的c++

checking two different 2D arrays to find a match c++

本文关键字:数组 2D c++ 查找 两个 检查      更新时间:2023-10-16

我目前正在写一个内存游戏,我有点卡住检查我的数组。程序如何工作:

当有人猜测时,它检查瓷砖并将瓷砖保存在机器人的2D数组中,这允许机器人记住东西在棋盘上的位置。

我正在尝试编写一个2D数组循环,检查包含答案的2D数组与机器人的2D数组之间的匹配。

当找到匹配时,我想保存值,但是我的程序目前没有这样工作,

我只是想了解一下我的for循环做错了什么。我环顾四周,通读了一些我的c++教科书,但没有找到任何东西。输出

 //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

目前,你的逻辑是错误的(添加注释)

// 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;
};
演示