0

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;
};
Progman
  • 16,827
  • 6
  • 33
  • 48

3 Answers3

1

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 << " ";
}
Bob__
  • 12,361
  • 3
  • 28
  • 42
1

"Consider the following:"

enum Card_Suit { HEART, CLUB, DIAMOND, SPADE};

struct Card
{
  Card_Suite suit;
  int        value;
};

std::vector<Card> deck;
std::vector<Card> player1_hand;

In the above code, a struct is used to model a card. A Card has a suit and has a value. Easy to read.

A Deck of cards is represented by std::vector<Card>.
A Player's hand is represented by std::vector<Card>.

A nice idea here is that you can move a card from the deck to a players' hand.

You can augment the Card struct by adding methods for:
constructing, copying, destructing, input (from user), and output (to user),
as well as comparison (ordering and equality).

Other functions involving cards depend on the game you are playing.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

pop_back() erases the last element of the vector, not the first. So, everytime you draw a card to the player hand you remove the last card from the deck rather than the one you gave him.

To remove the first element you can use erase.

deck_of_cards.erase(deck_of_cards.begin() )

You alse need to change your push_back() lines to use 0 as index rather than d and e, because everytime you remove the first element the entire vector goes one index backward.

Space.cpp
  • 31
  • 3