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...
/*
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>
usingnamespace std;
vector<string> cardDeck;
vector<string> playerHand;
constint 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");
}
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 constint 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