Skip to content

Instantly share code, notes, and snippets.

@j100002ben
Last active December 17, 2015 06:59
Show Gist options
  • Save j100002ben/5569881 to your computer and use it in GitHub Desktop.
Save j100002ben/5569881 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <ctime>
#include <iomanip>
#define SUITS_SPADES 0
#define SUITS_HEARTS 1
#define SUITS_DIAMONDS 2
#define SUITS_CLUBS 3
using namespace std;
class Card {
public:
Card(int suit, int face)
{
_suit = suit;
_face = face;
}
string toString()
{
return faces[_face] + " of " + suits[_suit];
}
static const string suits[];
static const string faces[];
private:
int _suit,
_face;
};
const string Card::suits[] = {"spades", "hearts", "diamonds", "clubs"};
const string Card::faces[] = {"one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen"};
class DeckOfCards {
public:
DeckOfCards()
{
for(int i=0;i<13;i++)
{
_deck.push_back(new Card(SUITS_SPADES, i));
_deck.push_back(new Card(SUITS_HEARTS, i));
_deck.push_back(new Card(SUITS_DIAMONDS, i));
_deck.push_back(new Card(SUITS_CLUBS, i));
}
}
void shuffle()
{
srand ( unsigned ( time(NULL) ) );
random_shuffle ( _deck.begin(), _deck.end());
_currentCard = 0;
}
void dealOne()
{
if( _currentCard >= _deck.size() )
{
return ;
}
cout << _deck[_currentCard]->toString() << endl;
_currentCard ++;
}
void dealAll()
{
for(int i=0;i<_deck.size();i++)
{
cout << _deck[i]->toString() << endl;
}
_currentCard = _deck.size();
}
private:
vector<Card *> _deck;
int _currentCard;
};
int main(int argc, char *argv[])
{
DeckOfCards deck;
deck.shuffle();
deck.dealAll();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment