Previous review of this project: Nested Deck class design and implementation
My program finally meets all of my expectations to the best of my ability, but I'd still like another review. Please give as many critiques as you can. I will then implement this with future games that I have in mind.
CardValuesMap.h
#ifndef CARDVALUESMAP_H
#define CARDVALUESMAP_H
std::map<char, unsigned> retrieveMap()
{
std::map<char, unsigned> values;
values['A'] = 1;
values['2'] = 2;
values['3'] = 3;
values['4'] = 4;
values['5'] = 5;
values['6'] = 6;
values['7'] = 7;
values['8'] = 8;
values['9'] = 9;
values['T'] = 10;
values['J'] = 11;
values['Q'] = 12;
values['K'] = 13;
return values;
}
#endif
Deck.h
#ifndef DECK_H
#define DECK_H
#include <iostream>
#include <array>
#include <map>
#include <string>
class Deck;
class Card
{
private:
friend class Deck;
unsigned value;
char rank;
char suit;
Card::Card(unsigned v, char r, char s) : value(v), rank(r), suit(s) {}
public:
Card::Card() : value(0), rank(' '), suit(' ') {}
bool operator<(const Card &rhs) const {return (value < rhs.value);}
bool operator>(const Card &rhs) const {return (value > rhs.value);}
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;
static const std::map<char, unsigned> values;
static const std::string SUITS;
std::array<Card, MAX_SIZE> cards;
int topCardPos;
public:
Deck();
void shuffle();
Card deal();
unsigned size() const {return topCardPos+1;}
unsigned empty() const {return topCardPos == -1;}
friend std::ostream& operator<<(std::ostream &out, const Deck &aDeck);
};
#endif
Deck.cpp
#include "Deck.h"
#include "CardValuesMap.h"
#include <algorithm>
const std::map<char, unsigned> Deck::values = retrieveMap();
const std::string Deck::SUITS = "HDCS";
Deck::Deck() : topCardPos(-1)
{
for (auto valueIter = values.cbegin(); valueIter != values.cend(); ++valueIter)
{
for (auto suitIter = SUITS.cbegin(); suitIter != SUITS.cend(); ++suitIter)
{
topCardPos++;
cards[topCardPos] = Card(valueIter->second, valueIter->first, *suitIter);
}
}
shuffle();
}
void Deck::shuffle()
{
topCardPos = MAX_SIZE-1;
std::random_shuffle(&cards.front(), &cards.back()+1);
}
Card Deck::deal()
{
if (empty())
{
std::cerr << "\nDECK IS EMPTY -- RESHUFFLING NOW\n";
shuffle();
}
topCardPos--;
return cards[topCardPos+1];
}
std::ostream& operator<<(std::ostream &out, const Deck &aDeck)
{
for (int i = aDeck.size()-1; i > -1; --i)
{
out << "\n" << aDeck.cards[i];
}
return out;
}