C ++ Mastermind Double White Peg Issue

时间:2017-09-15 18:54:58

标签: c++

我是C ++的新手,并且遇到了分配问题。我无法弄清楚我在计算双白钉的问题。有人可以帮忙吗?我可以理解我有重复计算的问题,因为我只使用"或"声明,但除此之外,我认为我需要从白钉中减去黑钉,但我不知道该怎么做。

#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;
}

1 个答案:

答案 0 :(得分:0)

计算白钉时,必须忽略检测到黑钉的所有位置。因此,在检测到黑钉之后,您通过执行以下操作来尝试检测白钉:

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

正如您已经注意到的,这种方法将导致重复计算。一个例子:秘密模式在第一个位置包含绿色钉。现在输入您的猜测:第一个位置为绿色,第四个位置为绿色。你的代码将检测到第一个位置上的黑色钉子和(至少)两个额外的白色钉子,这不是它应该工作的方式。在计算白钉时,必须忽略黑钉位置,并且所有已经检测到的白钉。

所以你必须修改你的代码。通过将检测到的黑色挂钉位置写入地图(称为match_map),并在第一个白色挂钩匹配(也必须写入列表)之后离开内部循环,可以避免双白钉计数。请注意,你有一个&#39; R&#39;在你的颜色列表中两次。给予&#39; Bt&#39;由于char无法工作,我将其替换为&#39; b&#39;。你可以不使用switch()来使用颜色数组,然后一个循环足以处理秘密模式。

避免使用名称空间std; - 查看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;
}