Decided to create a deck in C++ using a vector and a Card class. I chose this because shuffling is supposed to be easy using random_shuffle
. My first approach just to get aquatint with C++ I made a Card list, used vector to generate numbers and shuffle them just to insert the cards in a tmp list and return it. This was messy to say the least.
So on my second branch I tried to do it with a vector from the start, it looks like this:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
// Represent a card
class Card
{
private:
int suit;
int value;
public:
Card(){}
Card(int v, int s)
{
suit = s;
value = v;
}
void
setNewVals(int v, int s)
{
value = v;
suit = s;
}
int
getValue()
{
return value;
}
int
getSuit()
{
return suit;
}
};
// Shuffle deck
void
shuffleDeck(vector<Card> *c, int shuffles)
{
for(int s = 0; s < shuffles; s++)
{
random_shuffle(c->begin(), c->end());
}
}
int
main()
{
//TODO: Later input from main
int numDecks = 10;
int numShuffles = 10;
// Create a deck vector
vector<Card> c;
// Select card's value
for(int d = 0; d < numDecks; d++)
{
for(int v = 1; v < 14; v++)
{
// Select card's suit
for(int s = 0; s < 4; s++)
{
Card card(v, s);
c.push_back(card);
}
}
}
// Shuffle the deck
shuffleDeck(&c, numShuffles);
// Print out first 52 cards
for(int i = 0; i < 52; i++)
{
Card aCard = c.front();
cout << aCard.getValue() << "\t" << aCard.getSuit() << endl;
c.erase(c.begin());
}
return 0;
}
The problem is that now the random_shuffle
does not really work as expected. It shuffles alright, but all cards always end up in the same order. When I did almost the exact same thing with int, it worked fine.
I really have no idea why it is not shuffling random all the time. Have I misinterpreted how vectors work?