Int值不会重置

时间:2013-10-10 02:56:29

标签: c++ int reset

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

#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循环添加一个方法来完成,我会添加一个检查,看看你是否稍后赢了..之后我弄清楚为什么白钉不会重置

0 个答案:

没有答案