1

Since This Post didn't exactly answer my question I'm going to start a new one.

I'm encoutering a problem when moving an element from one vector to another.

The originating vector is a simple deck of cards and the end vector is supposed to represent cards in the player's hand.

I'm using Hand.push_back(Deck.back()) to copy the "top" card and then Deck.pop_back() to remove it from the Deck vector.

Strangely though the next time I call the Draw Card function it "draws" the same card that should have been removed last turn.

I'm guessing it's a problem with resizing the vector? Should I try Deck.resize(Deck.size() - 1) after each pop_back()?

void Player::DrawStartingHand(int num)
{
    for(int i = 0; i < num; i++)
    {
        Hand.push_back(Deck.back());
        Deck.pop_back();
    }
}

void Player::DrawCard()
{
    std::cout<< "Drawing next card..." << std::endl;

    if(Deck.size() >= 1)
    {
        //copy card from top of deck
        //place at front of hand
        Hand.push_back(Deck.back());

        //remove care from top of deck
        Deck.pop_back();
    }
    else
    {
        std::cout<< "No cards left to draw\n";
    }
}

EDIT:: Each of the above functions are currently only called in one place a piece, and not under any extreme circumstances or anything weird. Just very basic, simple calls.

Community
  • 1
  • 1
Prototype958
  • 179
  • 8
  • 5
    Please create a http://sscce.org -- the code above does not demonstrate your problem. This is not a request to post all of your code: it is a request for you to take your code, and reduce it down to a situation where the problem you see is still occurring. Ideally the reduced code would be **less** code than what you posted above. – Yakk - Adam Nevraumont Mar 08 '14 at 21:03
  • 1
    And eventually the process of creating a SSCCE will already help you to find the problem – Sebastian Hoffmann Mar 08 '14 at 21:03
  • Please provide more context, the problem does not seem to be present in the code you mention. – Gassa Mar 08 '14 at 21:05
  • 3
    A speculation: perhaps at some point, you pass Deck to the presented functions by value (instead of passing it by reference), so a local copy is created, and you modify that local copy instead of the actual deck. – Gassa Mar 08 '14 at 21:06
  • @Yakk Thank you for the suggestion and I'll definitely try putting something like that together when I get home, but currently I don't have all my code in front of me, and no way of testing if it would compile from where I am. That's also the reason I didn't simply TRY resizing the vector and instead asked if it was a possible solution. – Prototype958 Mar 08 '14 at 21:16
  • @Gassa Do you think you could elaborate in an answer? I'm not sure I understand. Very green programmer here. Sorry =/ – Prototype958 Mar 08 '14 at 21:21
  • 1
    Here is a SO question on topic: http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value. If there's some new information for you, consider reviewing your code and searching for a place where you passed Deck by value, like "void fun(vector Deck)", but wanted instead to pass it by reference, like "void fun(vector & Deck)". – Gassa Mar 08 '14 at 22:00
  • @Gassa So I did just notice that in my `DisplayHand()` function I pass the Hand vector by value so now I'm thinking that the problem is actually with displaying the hand instead of drawing cards. Thank you very much. I'll try this when I get out of work tonight. – Prototype958 Mar 08 '14 at 22:07

0 Answers0