Previous review of this project:
Card Deck class for a Poker game - version 2
This time, I considered combining both classes into one header file so that I can reduce my use of #include
and to keep them together. I also did this so that I can define my const std::string
variables only once for both classes. However, I decided to try using a namespace
for these variables for easier access. It makes more sense (to me) to keep them with Card
and let Deck
refer to them during the latter's construction, but I'm not sure how to do that.
Is this the proper thing to do, or could something be done differently? Is there a need for extern
somewhere? The code below doesn't include class implementations, but I've kept them intact from the linked code.
Deck.h
#ifndef DECK_H
#define DECK_H
#include <iostream>
#include <string>
#include <array>
namespace rs
{
const std::string RANKS = "A23456789TJQK";
const std::string SUITS = "HDCS";
}
class Card
{
private:
unsigned rankValue;
char rank;
char suit;
public:
Card();
Card(char r, char s);
bool operator<(const Card &rhs) const {return (rankValue < rhs.rankValue);}
bool operator>(const Card &rhs) const {return (rankValue > rhs.rankValue);}
bool operator==(const Card &rhs) const {return (suit == rhs.suit);}
bool operator!=(const Card &rhs) const {return (suit != rhs.suit);}
friend std::ostream& operator<<(std::ostream &out, const Card &aCard)
{return out << '[' << aCard.rank << aCard.suit << ']';}
};
class Deck
{
private:
static const unsigned MAX_SIZE = 52;
std::array<Card, MAX_SIZE> cards;
int topCardPos;
public:
Deck();
void shuffle();
Card deal();
unsigned size() const {return topCardPos+1;}
bool empty() const {return topCardPos == -1;}
friend std::ostream& operator<<(std::ostream &out, const Deck &aDeck);
};
#endif