Search code examples
c++push-back

Dealing 5 cards per player using vectors


I am trying to push_back 5 elements from vector deck_of_cards to vector player1_hand and pop_back those elements to reduce the deck_of_cards, then the next 5 elements from vector deck_of_cards for vector player2_hand, etc. I can push_back with the current code for player1_hand. Player2_hand pushes the same elements so I cannot pop off the elements as seen in my output:

shuffled deck 308 309 302 102 211 312 313 414 306 402 113 203 413 206 104 305 314 404 407 304 105 111 412 403 103 112 204 208 109 110 214 405 213 114 210 106 411 205 202 108 310 311 307 207 408 406 212 410 303 107 409 209

Player1 cards 308 309 302 102 211 312

Player2 cards 308 309 302 102 211 312

Here is my code:

class deck
{
private:
    int x;
    vector <int> deck_of_cards
    {
        102,103,104,105,106,107,108,109,110,111,112,113,114,
        202,203,204,205,206,207,208,209,210,211,212,213,214,
        302,303,304,305,306,307,308,309,310,311,312,313,314,
        402,403,404,405,406,407,408,409,410,411,412,413,414
    };

public:
    vector <int> player1_hand;
    vector <int> player2_hand;
    int shuffle_deck()
    {
        unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();

        shuffle(deck_of_cards.begin(), deck_of_cards.end(), default_random_engine(seed));

        cout << "shuffled deck\n";
        for (int x = 0; x < deck_of_cards.size(); x++)
        {
            cout << deck_of_cards.at(x) << " ";
        }
        return 0;
    };

    int deal()
    {
        cout << "\nPlayer1 cards\n";
        for (int d = 0; d < 6; d++)
        {
            int& element = deck_of_cards[d];
            player1_hand.push_back(deck_of_cards[d]);
            deck_of_cards.pop_back();
            cout << player1_hand.at(d) << " ";
        }
        cout << "\nPlayer2 cards\n";
        for (int e = 0; e < 6; e++)
        {
            int& element = deck_of_cards[e];
            player2_hand.push_back(deck_of_cards[e]);
            deck_of_cards.pop_back();
            cout << player2_hand.at(e) << " ";
        }
        return 0;
    }
};

int main()
{
    deck d;
    d.shuffle_deck();
    d.deal();

    return 0;
};

Solution

  • Consider these lines

    for (int d = 0; d < 6; d++)
    {
         int& element = deck_of_cards[d];   // <- Unused variable
         player1_hand.push_back(deck_of_cards[d]);
         //                                   ^   The FIRST 6 cards of the deck are added
         //                                       to the player hand
         deck_of_cards.pop_back();
         //                ^^^^                   The LAST cards of the deck are removed
         cout << player1_hand.at(d) << " ";
    }
    

    The same happens in the next loop, so that the other player's hand contains the same cards (the first 6 cards of the shuffled deck).

    There are at least a couple of ways to do it as expected:

    std::cout << "\nPlayer1 cards\n";
    for (int d = 0; d < 6; d++)
    {
        player1_hand.push_back(deck_of_cards.back());
        //                                    ^^^^^^    The last card of the deck is added
        deck_of_cards.pop_back();
        //                ^^^^                          The same card is removed
        std::cout << player1_hand.back() << " ";
    }
    
    // Or:
    std::cout << "\nPlayer2 cards\n";
    auto it = std::next(deck_of_cards.end(), -6);
    player2_hand.insert(player2_hand.end(), it, deck_of_cards.end());
    deck_of_cards.erase(it, deck_of_cards.end());
    for ( auto card : player2_hand )
    {
        std::cout << card << " ";
    }