Deck of Cards Class

I worked this up today to use for creating card games. I thought I would share it so the greater audience could tear it apart and offer suggestions for improvement.

The code consists of a header file which contains a "card" structure and a "cardStack" class. These work together as demonstrated in the main() function to allow the creation of card games... on consoles.


Header file: playingCards.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
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
#ifndef PLAYINGCARDS_H
#define PLAYINGCARDS_H


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


/*
 * Basic structure for a playing card; versatile
 * so that it can be used for more than just
 * standard playing cards
 */
struct card {
public:
	card() : _valid(false) {};
	card(std::string face, std::string suit, int value) {
		create(face, suit, value);
	}

	//Until the create() method is called, the card
	//is not valid (essentially empty). Meant to be
	//used with cardStack class.
	void create(std::string face, std::string suite, int value) {
		_face = face;
		_suit = suite;
		_value = value;
		_valid = true;
	}

	std::string getFace() { return _face; }
	std::string getSuit() { return _suit; }

	int getValue() { return _value; }

	bool isValid() { return _valid; }
	void erase() {
		_valid = false;
	}


private:

	bool _valid;

	std::string _face;
	std::string _suit;
	int _value;
	

};


//Card stack - collection of cards, that's it!
//A deck of cards is a card stack, but a card
//stack is not necessarily a deck of cards.
//A players hand, discard pile, or cards in
//play could also be card stacks.
class cardStack {

	public:
		cardStack() {};

		//Add a card to this card stack and invalidate
		//the origin card.  Default adds to end of stack.
		void add(card &aCard, int index = -1) {
			if (aCard.isValid()) {
				if (index == -1) {
					_c.push_back(aCard);
				} else {
					_c.insert(_c.begin() + index, aCard);
				}
				aCard.erase();
			}
		}


		//returns this index card to the caller, however
		//does not remove the card from this card stack.
		card show(int index = 0) {
			if (!_c.empty()) {
				return _c[index];
			} else {
				//if there is no cards in the stack, return
				//an invalid card.
				return card();
			}
		}


		//return this index card to the caller;
		//erase the card from this card stack
		card remove(int index = 0) {
			if (!_c.empty()) {
				card tmp = _c[index];
				_c.erase(_c.begin() + index);
				return tmp;
			} else {
				return card();
			}
		}

		int size() { return _c.size(); }

		
		//Recommend adding srand(time(0)) at start
		//of program if using shuffle()
		void shuffle() {
			std::random_shuffle(_c.begin(), _c.end());
		}



	private:

		std::vector<card> _c;

};

#endif




Main program (cpp) file
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
#include "../../../../Libraries/playingCards.h"
#include <time.h>

void buildCardDeck(cardStack&);

int main() {

	//Randomize
	std::srand(time(0));

	//Create deck of cards and player hand
	cardStack deck;
	cardStack playerHand;

	//Build deck of cards
	buildCardDeck(deck);

	//Shuffle the deck
	deck.shuffle();

	//deal the player the top 5 cards from the deck
	for (int i=0; i<5; i++)
		playerHand.add(deck.remove());

	//Display stats to verify everything works as expected
	std::cout << "Player Card Count: " << playerHand.size();
	std::cout << "\nRemaining cards in deck: " << deck.size();
	std::cout << "\n\nPlayer Cards:";
	for (int i=0; i<5; i++) {
		std::string str = playerHand.show(i).getFace() + " of " + playerHand.show(i).getSuit();
		std::cout << "\n" << str;
	}
	std::cout << "\n\nCards remaining in the deck:";
	for (int i=0; i<deck.size(); i++) {
		std::string str = deck.show(i).getFace() + " of " + deck.show(i).getSuit();
		std::cout << "\n" << str;
	}

	std::cin.get();
	return 0;
}


//Build a default standard deck of playing cards (52 cards; no jokers)
void buildCardDeck(cardStack &cs) {
	cs.add(card("Ace", "Spades", 14));
	cs.add(card("King", "Spades", 13));
	cs.add(card("Queen", "Spades", 12));
	cs.add(card("Jack", "Spades", 11));
	cs.add(card("10", "Spades", 10));
	cs.add(card("9", "Spades", 9));
	cs.add(card("8", "Spades", 8));
	cs.add(card("7", "Spades", 7));
	//etc... enough for testing for now
}
In case anyone was wondering why I would make the 'card' structure using string variables, it's so I could do crazy stuff like this....

New structure... that inherits the card() structure.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//That's right - World of Warcraft Cards
//I was thinking Magic the Gathering but I've never played,
//so I don't know what the cards look like.
struct wowCard : public card {
public:	
	wowCard() : card() {};
	wowCard(std::string face, std::string suit, int value, int mana, int health) {
		wowCard::create(face, suit, value, mana, health);
	}

	void create(std::string face, std::string suit, int value, int mana, int health) {
		_mana = mana;
		_health = health;
		card::create(face, suit, value);
	}

	int getMana() { return _mana; }
	int getHealth() { return _health; }

private:
		//Added card properties
		int _mana;
		int _health;
};


Change to buildCardDeck() .... everything in main() still functions correctly.
1
2
3
4
5
6
7
8
void buildCardDeck(cardStack &cs) {
	cs.add(wowCard("Blood Elf", "Paladin", 75, 100, 100));
	cs.add(wowCard("Night Elf", "Hunter", 75, 100, 100));
	cs.add(wowCard("Human", "Priest", 70, 90, 85));
	cs.add(wowCard("Pandarean", "Druid", 65, 125, 90));
	cs.add(wowCard("Undead", "Warrior", 95, 95, 100));
	cs.add(wowCard("Orc", "Death Knight", 100, 100, 100));
}


Last edited on
Topic archived. No new replies allowed.