Hello everybody, Yesterday I started to program Black Jack game in console and I have problems with vectpr.push_back(), I use it to add cards from deck to players hand, for what I use CPlayer::PDraw(CDeck d) function, here it is:
void PDraw( CDeck d)
{
cards.push_back(d.getDeck()[cardCounter]);//error, deck deletes somehow after doing one command
cardCounter++;
}
The problem is, when I use puch back(), my deck what I pass into function somehow is changed and becomes empty, I think it has to do something with my Copy and Assignment constructors of CDeck class, here is my CDeck class:
//Class representing Deck of Cards
class CDeck
{
public:
CDeck(){
pDeck= new SCard[CARD_NUMBER];
for(int i = 0; i < CARD_NUMBER; i ++)
{
//Assign ID to card and its suit and value
pDeck.id = i;
identifyCard(&pDeck);
}
}
~CDeck(){
delete[] pDeck;
}
CDeck(const CDeck &d) { operator=(d); }//error here?
const CDeck &operator=(const CDeck &d) {//still mistake
for(int i = 0; i < CARD_NUMBER; i++)
{
pDeck.id = d.pDeck.id;
pDeck.type = d.pDeck.type;
pDeck.num = d.pDeck.num;
pDeck.val = d.pDeck.val;
}
return d;
}
void shuffleDeck()
{
//Shuffles Deck, but maybe more suitable with vector and rand()
std::random_shuffle(pDeck, pDeck + 52);
}
void identifyCard(SCard* c)
{
c->type = suits[c->id/13];
c->val = value[c->id%13];
c->num = nums[c->id%13];
}
SCard* getDeck(){return pDeck;}
private:
SCard* pDeck;
};
Here is SCard structure:
//Structure representing one card
typedef struct
{
//Card ID convertible to its value and suit
int id;
//Card Value from Sce to King
string val;
//Card suit, Hearts etc.
string type;
//Cars numerical value with ace being 11
int num;
}SCard;
ANd lastly, my CPlayer class:
//Class representing main Player
class CPlayer
{
public:
CPlayer()
{
bet = 0;
balance = START_MONEY;
counter = 0;
//party begins
isPlaying = true;
//first move is banks
isActive = false;
}
//Player actions funcs
void PStay()
{
isActive = false;
}
void PDraw( CDeck d)
{
cards.push_back(d.getDeck()[cardCounter]);//error, deck deletes somehow after doing one command
cardCounter++;
}
void PDouble(CDeck d)
{
PDraw(d);
bet*=2;
}
void PMove(CDeck d)
{
int choise;
cout << "Choose move what you like: \n1.Stay.\n2.Draw\n3.Double.\n";
cin >> choise;
if(choise == 1) PStay();
if(choise == 2) PDraw(d);
if(choise == 3) PDouble(d);
}
void PInfo()
{
cout << "Balance: " << balance << endl;
cout << "Bet: " << bet << endl;
cout << "Cards in your hand:" << endl;
vector<SCard>::iterator iter;
for(iter = cards.begin(); iter != cards.end(); ++iter)
{
cout << (*iter).val << " of " << (*iter).type << endl;
}
cout << "Value: " << PSum() << endl;;
}
void PBet()
{
bool isBetMade = false;
while(!isBetMade)
{
cout << "Enter the bet between 10 to "<< balance << ": ";
cin >> bet;
if(bet < 10 || bet > balance)
cout << "\nYou can't bet this sum. Type Again\n";
else
isBetMade = true;
}
balance -= bet;
}
int PSum()
{
int numOfAces = 0; //nuimber of aces in hand
vector<SCard>::iterator iter;
for(iter = cards.begin(); iter != cards.end(); ++iter)
{
if((*iter).val == "Ace") numOfAces++;
counter += (*iter).num;
}
if(counter > BLACK_JACK)
{
for(iter = cards.begin(); iter != cards.end(); ++iter)
{
if((*iter).val == "Ace")
counter -= 10;// +1 and -11
if(counter <= BLACK_JACK)
break;
}
}
return counter;
}
bool isPlaying;//player currently playing
bool isActive; // is players move
private:
int balance;
int bet;
//stores current points that player has
int counter;
//stores current cards in hand
vector<SCard> cards;
};
I regard every possible answer to this problem. When I run program and step to line where I push card into my vector representing cards in player hands it throws unhandled exception (Unhandled exception at 0x779615de in Black Jack.exe: 0xC0000005: Access violation writing location 0x76d833aa.). To be more precise it comes to constructor of CDeck and then breaks.