#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <vector>
usingnamespace std;
class card
{
public:
int value;
string suit;
card(int a, string b)
{
value = a;
suit = b;
}
void gen();
};
void card::gen()
{
srand(time(NULL));
value = rand() % 13 + 1;
int tmp = rand() % 4 + 1;
if(tmp == 1)
{
suit = "Spades";
}
if(tmp == 2)
{
suit = "Diamonds";
}
if(tmp == 3)
{
suit = "Hearts";
}
if(tmp == 4)
{
suit = "Clubs";
}
}
class deck
{
public:
vector<card> cards;
deck()
{
vector<card>::iterator it;
it = cards.begin();
for (int i = 1; i < 5; i++)
{
for (int j = 1; j< 14; j++)
{
it = cards.begin();
if(i == 1)
{
cards.insert(it, card(j, "Spades"));
}
if(i == 2)
{
cards.insert(it, card(j, "Diamonds"));
}
if(i == 3)
{
cards.insert(it, card(j, "Hearts"));
}
if(i == 4)
{
cards.insert(it, card(j, "Clubs"));
}
}
}
};
void shuffle();
int size();
card draw();
};
card deck::draw()
{
card tmp = cards[0];
cards.erase(cards.begin()); //it seems this line isn't running
return (tmp);
}
int deck::size()
{
return cards.size();
}
void deck::shuffle()
{
srand(time(NULL));
for (int i = 0; i < 200; i++)
{
swap(cards[rand() % 52 + 0], cards[rand() % 52 + 0]);
}
}
class player
{
public:
vector<card> hand;
string name;
int points;
player(string namee, deck deckk)
{
name = namee;
for (int i = 0; i < 5; i++)
{
hand.push_back(deckk.draw());
}
}
};
int main()
{
deck deck1;
player player1("Player one", deck1);
for (int i = 0; i < 5; i++)
{
cout << player1.hand[i].value << " of " << player1.hand[i].suit << endl;
}
for (int i = 0; i < 52; i++)
{
cout << deck1.cards[i].value << " of " << deck1.cards[i].suit << endl;
}
cout << deck1.size() << endl;
return 0;
}
When I run it, I get the hand, (first 5 cards) the FULL deck, nothing deleted, and it says there is 52 cards left in the deck, I want there to be 47 cards once you draw 5 out from the deck, but I can't figure out why it's not working. I think the problem is at line 84, although I'm not sure. Please help!
Consider using std::deque<> as the sequence container when adding or removing elements at the front of the sequence is a common operation. http://cplusplus.com/reference/stl/deque/