简体   繁体   中英

C++//How to store values from user input?

I am a very beginner of learning C++...!

I am writing a mastermind game code and I want to display userGuess. But not only displaying the current guess, but also displaying all the userGuess that user made. For example... If userGuess was 0 0 1 1, then the board should display [0 0 1 1] and if the user made another guess like 2 2 3 3, then I want to display

[0 0 1 1]

[2 2 3 3]

and so forth up to 4x10 matrix.. I am working inside the class so I have no idea how should I approach this method..! I figured out that StackOverflow is a very good website to search for questions! thanks, everyone!


Since I am so bad at explaining, here's my code!

I use this function to get user input.

    void Game::getUserGuess()
{
    int guess_1, guess_2, guess_3, guess_4;
    std::cout << "Type 4 colors that you want to check: ";
    std::cin >> guess_1 >> guess_2 >> guess_3 >> guess_4;
    currGuess.setGuess(guess_1, guess_2, guess_3, guess_4);
}

void Game::displayUserGuess() 
{

    // displaying user guess code
    std::cout << "Your Guess: " << std::endl;

    std::cout << "|" << gameboard[0] << "|" << gameboard[1] << "|" << gameboard[2] << "|" << gameboard[3] << "|    White Pegs: " << whitePegs <<  std::endl;
    std::cout << "|" << gameboard[4] << "|" << gameboard[5] << "|" << gameboard[6] << "|" << gameboard[7] << "|    Black Pegs: " << blackPegs << std::endl;
    std::cout << "|" << gameboard[8] << "|" << gameboard[9] << "|" << gameboard[10] << "|" << gameboard[11] << "|" << std::endl;
    std::cout << "|" << gameboard[12] << "|" << gameboard[13] << "|" << gameboard[14] << "|" << gameboard[15] << "|" << std::endl;
    std::cout << "|" << gameboard[16] << "|" << gameboard[17] << "|" << gameboard[18] << "|" << gameboard[19] << "|" << std::endl;
    std::cout << "|" << gameboard[20] << "|" << gameboard[21] << "|" << gameboard[22] << "|" << gameboard[23] << "|" << std::endl;
    std::cout << "|" << gameboard[24] << "|" << gameboard[25] << "|" << gameboard[26] << "|" << gameboard[27] << "|" << std::endl;
    std::cout << "|" << gameboard[28] << "|" << gameboard[29] << "|" << gameboard[30] << "|" << gameboard[31] << "|" << std::endl;
    std::cout << "|" << gameboard[32] << "|" << gameboard[33] << "|" << gameboard[34] << "|" << gameboard[35] << "|" << std::endl;
    std::cout << "|" << gameboard[36] << "|" << gameboard[37] << "|" << gameboard[38] << "|" << gameboard[39] << "|" << std::endl;


}


void Game::playGame()
{   

    colorTable();
    do {

        secretCode;
        getUserGuess();
        generateFeedback();
        if (isWinner()) {
            std::cout << "You won!" << std::endl;
            break;
        }
        else {
            std::cout << "[[ Wrong ]]" << std::endl;
            std::cout << std::endl;
            numGuesses++;
        }
        blackPegs = 0;
        whitePegs = 0;
    } while (!outOfGuesses());

}

Basically.. I want to store user_input to my gameboard but I do not know how to use vector or array well.. it keeps giving me an error once I use vector..

I want user to see their results after feedback. So if the user made one move and check their result, then they could see the previous result..!

Just store your guesses directly into the game board:

void Game::getUserGuess()
{
    int guess_1, guess_2, guess_3, guess_4;
    std::cout << "Type 4 colors that you want to check: ";
    std::cin >> guess_1 >> guess_2 >> guess_3 >> guess_4;
    gameboard[numGuesses*4] = guess_1;
    gameboard[numGuesses*4 + 1] = guess_2;
    gameboard[numGuesses*4 + 2] = guess_3;
    gameboard[numGuesses*4 + 3] = guess_4;
    currGuess.setGuess(guess_1, guess_2, guess_3, guess_4);
}

Here is a some tips to help how to approach the problem. With these tips I'm sure you can write this and other codes.

First you need to divide you problem in part to get your answer. Try to see the big picture and after that go deep.

As you said, you need to store a several data and the question already gave the size of this storage. It is a maximum of 4x10 matrix, or a matrix that store another matrix of (4 elements).

You can use the std::array or std::vector to do that. To decide with one use, important to note you have 'if the user made another guess'. That means you aren't sure how many times the userGuess will guess, but you know every guess has 4. To represent it in code it will be:

std::vector<std::array<int,4>> gameboard;

Next step you want to ask the maximum of 10x to user his userGuess his guesses. (Let put aside for a while how to ask). Also you say you are working in a class then you will have a function member to add new guess. It could be something like that:

void YourClass::Add() {
     if (gameboard.size() < 10) {        //Here you won't let more than 10 guesses
        gameboard.emplace_back();          //New guess added
        //will ask the 4 guesses
     }
}

The next step is ask the 4 guesses. That require a loop like this:

for (int g = 0; g < 4; ++g) {
    //fulfill the matrix here
}

Now, you just need to fulfill the elements

cin >> gameboard.back()[g];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM