Jump to content
  • Advertisement
Sign in to follow this  

STL vector help

This topic is 4397 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing a blackjack game for a class and am having a problem splitting the hand. I have a player class with a vector of Hand classes. The Hand class holds a vector of Card class pointers. Here are my stpes: 1) create a new Hand 2) pop last card off current hand if there are 2 cards 3) add popped card to new hand (I verify that each hand has 1 card) 4) push new hand onto vector of hands (after which old hand has 0 size and new hand has size of 1) 5) house keeping I have tried pushing the new hand before adding the card and that gives a segfault. For some reason I can't add cards to the new hand after pushing it onto the vector of hands. With the original steps, if I move on and add a "second" card to the first hand and check the size I get sizes like 595 and it segfaults when accessing all those cards. I tried making a copy constructor for Hand, to copy the vector I simply said new_vector = old_vector, hoping to call the vector copy constructor. This didn't help. I don't need a copy constructor for Card because I am using pointers to the cards and not passing the Cards themselves. Any help would be greatly appreciated. Thanks

Share this post


Link to post
Share on other sites
Advertisement
without seeing any code i'm going to have to make assumptions.
Quote:
Original post by StubornAH
The Hand class holds a vector of Card class pointers.

this is probably where your problem is. do you really need to use pointers? are you deleting allocated memory in a class's destructor somewhere? if so, then you may be destroying your data whenever a vector is resized. posting relevant code might help.

Share this post


Link to post
Share on other sites
what are you talking about. the problem is that when I push the second hand it screws up the vector in the second. so for the sake of argument, if the second hand holds the same card addresses as the first and the first gets destroyed would the second vector know that? I would assume that there is nothing telling the second vector that it destroy the data it points to, that is people's problem with C++.

the cards are not getting destroyed, why is the size changing?

thanks for trying though

Share this post


Link to post
Share on other sites
sorry, I may not have been clear. the vector holds addresses. as far as I know, the vector does not care if the address is valid or not. the problem is when you try to access an invalid address. since the vector does not care what it holds, why is it resizing?

Share this post


Link to post
Share on other sites
I'd try to find figure out your problem, but without source code I might as well be trying to hit a gru without a flashlight.

Share this post


Link to post
Share on other sites
(apparently I don't know how to get the code into seperate and scrollable areas)

Here is most of the Hand class implementation:


Hand::Hand(const Hand& old_hand) {
b_debug = old_hand.b_debug;
value = old_hand.value;
soft_hand = old_hand.soft_hand;
hand_cards = old_hand.hand_cards;
hand_size = hand_cards.size();
return;
}

int Hand::addCard (Card* new_card) {
hand_cards.push_back(new_card);
hand_size++;
if (strcmp("ace",(new_card->cardFace()).c_str()) == 0) {
if (!soft_hand) {
value += 10;
soft_hand = 1;
if (b_debug) {
cout << "Ace recieved, value added by 10." << endl;
}
}
}

value += new_card->cardValue();
if (b_debug) {
cout << "Value of card is " << new_card->cardValue();
cout << " and the value of the hand is " << value << endl;
cout << "Gamer now has " << hand_cards.size() << " cards." << endl;
}
if (soft_hand && (value > 21)) {
value -= 10;
soft_hand = 0;
}

return 0;
}

int Hand::numCards () {
if (b_debug) {
cout << "How many cards I got?" << endl;
cout << "Hand really has " << hand_cards.size() << " cards." << endl;
}
return hand_size;
}

int Hand::split () {
std::vector<Card*>::iterator temp = hand_cards.begin();
int top_card = (*temp)->cardValue();
temp++;

if ((hand_size == 2) && ((*temp)->cardValue() == top_card)) {
cout << "Okay to split." << endl;
return 1;
}

cout << "Not okay to split." << endl;

return 0;
}

Card* Hand::splitHand () {
vector<Card*>::iterator temp = hand_cards.end();
temp--;

if (split()) {
Card* split_card = *temp;
Card* old_card;
temp = hand_cards.begin();
old_card = *temp;
//hand_cards.pop_back();
hand_cards.erase(hand_cards.begin() + 1, hand_cards.end());
if (b_debug)
cout << "Hand is split and has " << hand_cards.size() << " cards." << endl;
hand_size--;
value -= split_card->cardValue();
if (b_debug) {
cout << "Hand split in splitHand() of Hand class." << endl;
}
return split_card;
}
return NULL;
}

int Hand::printHand () {
vector<Card*>::iterator temp;
vector<Card*>::iterator last = hand_cards.end();

cout << hand_cards.size() << " cards: " << endl;

if (hand_cards.size() > 1) {
for (temp = hand_cards.begin(); temp < last; temp++) {
cout << (*temp)->cardFace() << " of " << (*temp)->cardSuit();
cout << ", ";
}
cout << endl;
if (soft_hand)
cout << "soft";
else
cout << "hard";
cout << " " << value << endl;
return 0;
}
return -1;
}



and here is the player's split function:


int Gamer::split () {
Hand* temp;
vector<Hand>::iterator new_hand;

if (current_hand->split()) {
temp = new Hand(b_debug);
//player_hands.push_back(*temp); //causes segfault adding card in next line
temp->addCard(current_hand->splitHand());
//vector<Hand>::iterator new_hand;
//new_hand->addCard(current_hand->splitHand()); //works same as with temp
if (b_debug) {
cout << "Hand split, now time to make a second hand." << endl;
cout << "First hand has this many cards." << endl;
current_hand->numCards();
cout << "Second hand has this many cards." << endl;
temp->numCards();
//new_hand->numCards();
}
player_hands.push_back(*temp); //first hand now has 0 size
if (b_debug) {
cout << "First hand has this many cards." << endl;
current_hand->numCards();
cout << "Second hand has this many cards." << endl;
temp->numCards();
//new_hand->numCards();
cout << "Hand added, now move last_hand iterator." << endl;
cout << "Player now has " << player_hands.size() << " hands." << endl;
}
free(temp);
last_hand = player_hands.end();
if (b_debug) {
cout << "First hand has this many cards." << endl;
current_hand->numCards();
cout << "Last hand marked." << endl;
}
return 0;
}

return -1;
}



[Edited by - StubornAH on May 7, 2006 12:58:19 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by StubornAH
(apparently I don't know how to get the code into seperate and scrollable areas)
The tags that give you the scrollable source boxes are [ source ][ /source ].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!