Feb 17, 2012 at 3:03am Feb 17, 2012 at 3:03am UTC
Basically, here's what I have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <vector>
using namespace 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!
Last edited on Feb 17, 2012 at 3:04am Feb 17, 2012 at 3:04am UTC
Feb 17, 2012 at 3:25am Feb 17, 2012 at 3:25am UTC
You have multiple decks:
1) 'deck1' in main() (line 121)
2) 'deckk' in player's ctor (line 108)
Note that deckk is being passed by value, and therefore is a COPY of deck1.
You are properly removing cards from deckk, but that does not change deck1.
You probably want to pass the deck by reference:
player(string namee, deck& deckk) // <- note the '&', now deckk is a reference and not a copy
Feb 17, 2012 at 3:29am Feb 17, 2012 at 3:29am UTC
player(string namee, deck deckk)
Should be player(string namee, deck& deckk)
Because you want to modify the deck you're drawing from instead of working with a copy.
Feb 17, 2012 at 3:50am Feb 17, 2012 at 3:50am UTC
Thanks, all. That fixed it!