Card Game- returning Card points

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?

CardDeck.cpp
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "stdafx.h"
#include "Card.h"
#include "CardDeck.h"
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <string>
using namespace std;
CardDeck thisDeck;
CardDeck shuffleDeck;
int _tmain(int argc, _TCHAR* argv[])
{
	srand(time(NULL));
	bool done = false;

	while (!done)
	{
		int answer;
		cout << "1) Get a new card deck" << endl;
		cout << "2) Show all cards in the deck" << endl;
		cout << "3) Shuffle" << endl;
		cout << "4) Play WAR!" << endl;
		cout << "5) Exit" << endl;

		
		cin >> answer;
		if (answer == 1)
		{
			CardDeck *deck = new CardDeck();
			cout << "New Card Deck Has Been Generated"<< endl;
		}
		else if (answer == 2)
		{
			thisDeck.showAllCards();
		}
		else if (answer == 3)
		{
			shuffleDeck.shuffle();
			shuffleDeck.showAllCards();
			cout << "your cards have been shuffled!" << endl;
		}
		else if (answer == 4)
		{
			thisDeck.war();
		}
		else if (answer == 5)
		{
			done = true;
		}
	}
	int a;
	cin >> a;
	return 0;
}


CardDeck::CardDeck()
{
	int x = 0;
	topCard = 52;

	char suits[4] = { 'S', 'H', 'C', 'D' };

	for (int a = 0; a < 4; a++)
	{
		for (int b = 1; b <= 13; b++)
		{
			deck[x].suit = suits[a];
			deck[x].rank = b;

			x++;
		}
	}
	

}

Card CardDeck::deal()
{
	do
	{
		if (topCard >= 0)
		{
			Card result = deck[topCard];
			topCard--;
			cout << topCard;
			return result;
		}
		else
		{
			return Card();
		}
	} while (topCard > 0);
}

void CardDeck::displayCardAt(int a)
{
	
	deck[a].displayCard();

}
void CardDeck::showAllCards()
{
	for (int p = 0; p < 52; p++)
	{
		deck[p].displayCard();
	}

}
void CardDeck::shuffle()
{
	topCard = 52;
	
	srand(time(0)); // generate random seed

	for (int i = 0; i < topCard; i++) // loops through entire deck and randomly replaces positions to shuffle
	{
		int r = rand() % 52;
		Card temp(deck[r].suit, deck[r].rank);//create temp card
		deck[r].suit = deck[i].suit;
		deck[r].rank = deck[i].rank;
		deck[i].suit = temp.suit;
		deck[i].rank = temp.rank;

	}
}
void CardDeck::war()
{
	int player=0;
	int computer = 0;
	int a=0;
	int b=0;
	int result = 0;
	
		CardDeck card2;
		cout << "Get ready to play two-card WAR!!!" << endl;
		cout << "There are 52 cards in the deck." << endl;
		cout << "...dealing.... " << endl;
		cout << "One for me..." << endl;
		Card x;
		cout<< x.getValue();
		
		thisDeck.displayCardAt(a);
		result=thisDeck.getCardValue();
		thisDeck.shuffle();
		Card deck1;
		card2.deal();
		cout << "One for me..." << endl;
		thisDeck.displayCardAt(b);
		thisDeck.shuffle();
	cout << result << endl;
}
int CardDeck:: getCardValue()
{
	int value = 0;
	int cardPoint;

	for (int i = 0; i < 52; i++)
	{
		cardPoint = deck[i].getValue();

		switch (cardPoint)
		{
		case 1:
			value += 1;
			break;
		case 2:
			value += 2;
			break;
		case 3:
			value += 3;
			break;
		case 4:
			value += 4;
			break;
		case 5:
			value += 5;
			break;
		case 6:
			value += 6;
			break;
		case 7:
			value += 7;
			break;
		case 8:
			value += 8;
			break;
		case 9:
			value += 9;
			break;
		case 10:
			value += 10;
			break;
		case 11:
			value += 10;
			break;
		case 12:
			value += 10;
			break;
		case 13:
			value += 10;
			break;
		}
	}
	return value;
}





Card.cpp
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
#include "stdafx.h"
#include "Card.h"
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <string>
using namespace std;

Card::Card()
{
	char s;
	int r = 0;
}

Card::Card(char s, int r)
{
	suit=s;
	rank=r;
}

int Card:: getValue()
{
	return rank;
}

void Card::displayCard()
{
	/*char names[13][12] = {
		"Ace",
		"Two",
		"Three",
		"Four",
		"Five",
		"Six",
		"Seven",
		"Eight",
		"Nine",
		"Ten",
		"Jack",
		"Queen",
		"King"
	};

	cout << names[rank - 1] << " Of ";*/
	
	switch (rank)
	{
	case 1:
		cout<<("Ace");
		break;
	case 2:
		cout<<("Two");
		break;
	case 3:
		cout<<("Three");
		break;
	case 4:
		cout<<("Four");
		break;
	case 5:
		cout<<("Five");
		break;
	case 6:
		cout<<("Six");
		break;
	case 7:
		cout<<("Seven");
		break;
	case 8:
		cout<<("Eight");
		break;
	case 9:
		cout<<("Nine");
		break;
	case 10:	
		cout<<("Ten");
		break;
	case 11:
		cout<<("Jack");
		break;
	case 12:
		cout<<("Queen");
		break;
	case 13:
		cout<<("King");
		break;
	}

	cout << "	of	";
	switch (suit)
	{
	case'S':
		cout << "Spade" << endl;
		break;
	case'H':
		cout << "Heart" << endl;
		break;
	case'D':
		cout << "Diamond" << endl;
		break;
	case'C':
		cout << "Club" << endl;
		break;
	default:
		cout << "No Card" << endl;
		break;
	}
}

cardDeck.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include "Card.h"

using namespace std;

class CardDeck{

public:
	CardDeck();
	void displayCardAt(int);
	Card deal();
	void showAllCards();
//	int cardsLeft();
	void shuffle();
	int getCardValue();
	void war();
	//CardDeck shuffleDeck;
	int topCard;

	Card deck[52];
};



Card.h
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

#ifndef Card_H
#define Card_H
#include <iostream>
#include <string>

using namespace std;
class Card
{
public:
	Card();
	Card(char s, int r);
	int getValue();
	void displayCard();


//private:
	const int king = 13;
	const int jack = 11;
	const int queen = 12;
	const int ace = 1;
	char suit;
	int rank;

};

#endif 
Did you look at your debugger? What does that say is happening??
well when I run the program, it returns a value of 340. im testing the method at the war method
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 

Last edited on
Topic archived. No new replies allowed.