Yah, for the moment you could use this basic setup of deck and card:
Code:
#ifndef CARD_H
#define CARD_H
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Card
{
public:
Card(pair<string, unsigned> crd) : aCard(crd) {}
friend ostream &operator<<(ostream &os, Card const &c);
public:
string getSuit() const { return aCard.first; }
unsigned getValue() const { return aCard.second; }
pair<string, unsigned> getCard() const { return aCard; }
private:
pair<string, int> aCard;
};
ostream &operator<<(ostream &os, Card const &c)
{
cout<<resetiosflags(ios::right);
cout<<setiosflags(ios::left);
string sName;
char suitName[6];
switch(c.aCard.second)
{
default:
itoa(c.aCard.second, suitName, 10); break;
case 11:
strcpy(suitName, "Jack"); break;
case 12:
strcpy(suitName, "Queen"); break;
case 13:
strcpy(suitName, "King"); break;
case 14:
strcpy(suitName, "Ace"); break;
}
sName = suitName;
int len = c.aCard.first.length()+sName.length()+1;
cout<<setw(40-len/2)<<""<<c.aCard.first<<" "<<suitName;
resetiosflags(ios::left);
return os;
}
#endif //CARD_H
Code:
#ifndef DECK_H
#define DECK_H
#include "card.h"
#include <ctime>
#include <vector>
#include <algorithm>
#include <string>
#include <iterator>
//#define _STLP_USE_DEBUG_LIB
using namespace std;
class Deck
{
public:
Card& operator [] (unsigned index) { return theDeck[index]; }
Deck() { CreateDeck(); }
void RemoveTopCard()
{ theDeck.erase(theDeck.begin()); }
void CreateDeck()
{
int cardtype;
string suit;
for (int i=0; i<52; i++)
{
// First 13 cards are Clubs, next 13 are Spades etc.
cardtype = i/13;
switch(cardtype)
{
case 0: suit = "Club"; break;
case 1: suit = "Spade"; break;
case 2: suit = "Diamond"; break;
case 3: suit = "Heart"; break;
}
theDeck.push_back(Card(pair<string,unsigned>(suit, 2+i%13) ));
}
}
void Shuffle()
{
static bool seeded;
if (!seeded)
{
srand(time(0));
seeded = true;
}
random_shuffle(theDeck.begin(), theDeck.end());
}
void Show(ostream& os)
{
// 1) "copy()" theDeck (from begin() to end() ) to ostream_iterator.
// 2) o_i fetches a "Card" from theDeck (start at "begin()" )...
// 3) ... and forwards it to os (BlackJack forwards a "cout" to here).
// 4) cout<<Card<<"\n"; is executed.
// 5) But there is no default "<<" for "Card" defined by C++...
// 6) ... so we defined our own in class Card.
copy(theDeck.begin(), theDeck.end(), ostream_iterator<Card>(os, "\n") );
}
vector<Card> getDeck()
{ return theDeck; }
private:
vector<Card> theDeck;
};
#endif //DECK_H
This should do the trick nicely. The next step is to creat a class Player (very simple) and a class "Solitaire" that basically
is the game. But to keep it short (I cant type no code right now) the card and deck setup is healthy so study it and use it.