Im working on a simple war card game, however Im having a problem returning the points for each card. I believe the problem is on the getCardValue function. Can anybody take a look at it?
I'm not really familiar with the card game, so just making some general observations here:
carddeck.cpp
----------------
lines 28-31: You dynamically allocate a new deck. That new deck goes out of scope at line 31. This is a memory leak and you never do anything with this deck. You can not access the deck once it's gone out of scope.
Lines 9-10: You have two instances of CardDeck. From my understanding of the card game, you shuffle one deck, distribute the cards to the two players, then "battle" by each player displaying their top card.
Line 114: You have an additional call to srand(). You should only ever call srand() at the beginning of your program.
Line 118-123: There is no guarantee that rand() won't generate the same value twice.
Lines 153-206: You're loop through all the cards in the deck adding up the values. Is that really what you intend to do here? Don't you really mean to get the value of a specific card? I think this is the jist of your problem.
Line 140: You create a Card using Card's default constructor (S=uninitialized, r=0). getValue() is going to return 0. What you want to do here is remove a Card from an instance of CardDeck or from a players stack.
Line 135: What is the point of CardDeck card2?
Line 147: card2.deal() returns a Card, but you don't use the return value.
The purpose of the game is to retrieve two cards, compare each of the card's value and the one wit the highest value wins.
I have fixed everything else I believe. I was just testing random values and forgot to clean up the code.
"Lines 153-206: You're loop through all the cards in the deck adding up the values. Is that really what you intend to do here? Don't you really mean to get the value of a specific card? I think this is the jist of your problem."
This is exactly what my problem is but I truly cant see what Im doing wrong, I thought my loop was just iterating through all cards. This is my first program in c++ btw and have 3 weeks working on this everyday each day with a different error, at this point I really dont know what the problem is =/
I truly cant see what Im doing wrong, I thought my loop was just iterating through all cards.
Explain to me why you are iterating through the deck adding up the point values of all the cards. How does this contribute to the game? Since you're always iterating through all 52 cards, you're always going to get the same value.
The way I would do this is to have one instance of CardDeck. Shuffle it. Deal 26 cards to each player. Each player has a vector to hold his cards. Note this is not an instance of CardDeck.
vector<Card> player[2];
As you deal cards for each player, you push it onto the vector for that player. As players "battle", you remove one card from each player's "stack".
1 2 3 4 5 6 7
Card card[2];
card[0] = player[0].back(); // get card from first player's stack
player[0].pop_back(); // remove card from first player's stack
card[1] = player[1].back(); // get card from second player's stack
player[1].pop_back(); // Remove card from second player's stack
// Now compare the cards