0

这是我到目前为止所拥有的

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cmath>

using namespace std;

string userGuess;
string code;

char colorPegs[6] = {'R', 'B', 'Y', 'G', 'O', 'P'};
int blackPeg = 0;
int whitePeg = 0;
int userGuessArray[6] = {0, 0, 0, 0, 0, 0};
int codeArray[6] = {0, 0, 0, 0, 0, 0};
bool attemptsDone = false;
int attemptCount = 0;

int main()
{
    srand(time(0));


    cout << "Please wait while the computer generates the code..." << endl;
    cout << "..." << endl;
    sleep(2);



    for(int i = 0; i < 4; i++)
    {

        sleep(1);
        int random = (rand() % 6) ;
        code[i] = colorPegs[random];
        cout << "Generating code number " << i << endl;
        cout << code[i] << endl;

    }

    cout << "Done generating the code!" << endl;
    sleep(1);
    cout << "..." << endl;
    cout << "Colors are: R, B, Y, G, O, P. Enter 4 colors in your guess. (e.g RBYG)" << endl;

    while (attemptsDone == false)
    {
        attemptCount++;
        cout << "Please enter your guess: ";
        getline(cin,userGuess);

// R B Y G O P
// 0 0 0 0 0 0
        for(int x = 0; x < 4; x++)
        {
            if (code[x] == 'R') {
            codeArray[0]++;

            } else if (code[x] == 'B') {
            codeArray[1]++;

            } else if (code[x] == 'Y') {
            codeArray[2]++;

            } else if (code[x] == 'G') {
            codeArray[3]++;

            } else if (code[x] == 'O') {
            codeArray[4]++;

            } else if (code[x] == 'P') {
            codeArray[5]++;

            }
        }

                for(int z = 0; z < 6; z++)
        {
            if (userGuess[z] == 'R') {
            userGuessArray[0]++;

            } else if (userGuess[z] == 'B') {
            userGuessArray[1]++;
            } else if (userGuess[z] == 'Y') {
            userGuessArray[2]++;

            } else if (userGuess[z] == 'G') {
            userGuessArray[3]++;

            } else if (userGuess[z] == 'O') {
            userGuessArray[4]++;

            } else if (userGuess[z] == 'P') {
            userGuessArray[5]++;

            }
        }

    for(int b = 0; b < 6; b++)
        {
        whitePeg += min(userGuessArray[b], codeArray[b]);
        }


        if (userGuess.length() > 4)
        {

            cout << "Invalid amount of colors in your guess!" << endl;

        }
        else if (userGuess.length() < 4)
        {

            cout << "Invalid amount of colors in your guess!" << endl;

        }
        else if (attemptCount > 9)
        {

            cout << "You are out of guesses! You lose!" << endl;
            return 0;

        }
        else if (userGuess.length() == 4)
        {

            for (int o = 0; o < 4; o++)
            {

                if(userGuess[o] == code[o])
                {

                    blackPeg++;

                }

            }

        }

        cout << "Your white pegs were " << whitePeg << ", your black pegs were " << blackPeg << endl;
        whitePeg = 0;
        blackPeg = 0;
    }
    cout << "You win! Congrats! " << endl;
}

当我运行它时,blackPeg 值重置为 0,但 whitePeg 值没有,它一直在增加,有人知道这是为什么吗?我试图在底部重置它们,并尝试在 while 循环的顶部重置它们,无论我做什么 blackPeg 回到 0 但 whitePeg 不断加起来?

顺便说一句,你可能已经注意到我没有添加一个方法来完成 while 循环,我会添加一个检查,看看你以后是否赢了..在我弄清楚为什么白色钉子不会重置之后

4

0 回答 0