Cards Program

I am interested in creating a few card programs and I have the first part complete (basis for all the card programs I plan to write). Before I proceed, I'd like to request more experienced programmers take a look at my code so far to ensure I have no logic errors and any tips on how I can improve the organization of my code...

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
/*
Simple Psuedo:
1. Prompt User; ask if they want to play
2. Create card deck
3. Shuffle card deck
4. Deal cards (DEAL_CARDS = number of cards to deal)
5. Display cards on screen
6. End program
*/

#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <ctime>
using namespace std;

vector<string> cardDeck;
vector<string> playerHand;

const int DEAL_CARDS = 7;

//Declare Functions
void ResetDeck();
void Shuffle();
void DealACard();
string CStr(int);

int main()
{
  //SCREEN SETUP
	cout << "-------------------------------------------------\n";
	cout << "H = Hearts, D = Diamonds, C = Clubs, S = Spades\n";
	cout << "-------------------------------------------------\n\n";
	cout << "Would you like to be dealt in (y/n)? ";

	char yn;
	cin >> yn;
	if (yn == 'n' || yn != 'y') return 0;

	//Build a fresh deck of cards
	ResetDeck();

	//Pick random seed based off time so 
	//our shuffle is different with each run
	srand(time(0));

	//Shuffle the cards
	Shuffle();

	//Deal the cards to the player and
	//display them on the screen
	cout << "\n\nHere's your cards: ";
	for (int i=0; i < DEAL_CARDS; ++i)
	{
		DealACard();
		cout << playerHand[i] << " ";
	}

	//
	cout << "\n\nIf we were playing a game, we could do something here!\n\n";
	system("pause");
	return 0;
}

void Shuffle() { random_shuffle(cardDeck.begin(),cardDeck.end()); }

void DealACard()
{
	//Add top card to playerHand; remove that card from the cardDeck
	playerHand.push_back(cardDeck[0]);
	cardDeck.erase(cardDeck.begin());
}

string CStr(int n)
{
	stringstream s;
	s << n;
	return s.str();
}

void ResetDeck()
{
	//Erase old deck and players hand
	cardDeck.clear();
	playerHand.clear();

	//Build 2's through 10's
	for (int i=2; i<11; ++i)
	{
		cardDeck.push_back(CStr(i) + "H");
		cardDeck.push_back(CStr(i) + "D");
		cardDeck.push_back(CStr(i) + "C");
		cardDeck.push_back(CStr(i) + "S");
	}
	//Build Jacks
	cardDeck.push_back("JH");
	cardDeck.push_back("JD");
	cardDeck.push_back("JC");
	cardDeck.push_back("JS");

	//Build Queens
	cardDeck.push_back("QH");
	cardDeck.push_back("QD");
	cardDeck.push_back("QC");
	cardDeck.push_back("QS");

	//Build Kings
	cardDeck.push_back("KH");
	cardDeck.push_back("KD");
	cardDeck.push_back("KC");
	cardDeck.push_back("KS");

	//Build Aces
	cardDeck.push_back("AH");
	cardDeck.push_back("AD");
	cardDeck.push_back("AC");
	cardDeck.push_back("AS");

}

This is a deck of playing cards...
Last edited on
I noticed if you run it fast 2x the cards are the same each time.
Other than that looks good
I tried changing cards to 2, 5, and 52 didn't notice any problems.

I did notice a problem if you change const int DEAL_CARDS = 53;.
not that is a problem but you might keep that in mind if you allow user input for # cards later.

This deck only has 52 cards (no jokers). But yeah, I do need to go back and add error catchers even though I'd adjust the constant value as I coded the game and that wouldn't be a variable the "player" had access to.

As for the same cards when run quickly back to back, I don't see this as a major problem since someone who was intent to actually PLAY the game, wouldn't do that. And if they did (perhaps to get a better hand) they deserve the same hand. :p

Thanks for your input, Samuel.
Topic archived. No new replies allowed.