1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
//use these to be able to compare cards
enum class CardRank
{
Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King
};
enum class Suit
{
Clubs, Spades, Diamonds, Hearts
};
class Card
{
private:
CardRank face_value;
Suit card_suit;
public:
//array of strings that shows the suits in ASCII
std::string ASCII_suits[4] { /*spades*/ u8"\u2663", /*clubs*/u8"\u2660",
/*hearts*/ u8"\u2666",/*diamonds*/ u8"\u2665"};
//array of strings that shows jack Queen King 10 etc
std::string denomination[13]{"A", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "J", "Q", "K"};
//constructor
Card(CardRank _val, Suit _suit)
{
//std::cout << "constructor using enum" << std::endl;
face_value = _val;
card_suit = _suit;
}
//constructor
Card(int value, int suit)
{
//std::cout << "constructor using int" << std::endl;
face_value = static_cast<CardRank>(value);
card_suit = static_cast<Suit>(suit);
}
//copy constructor
Card(const Card &card)
{
face_value = card.face_value;
card_suit = card.card_suit;
//std::cout << "copy constructor" << std::endl;
}
//get ASCII pictorial suit
std::string getSuitPic(int pos) const
{
return ASCII_suits[pos];
}
//get Face of card
std::string getCardFace(int pos) const
{
return denomination[pos];
}
//get suit
Suit getSuit() const { return card_suit; }
//get value
CardRank getValue() const {return face_value;}
//print out card value/suit using member function
void showCard()
{
std::cout << getCardFace(static_cast<int>(getValue()))
<< getSuitPic(static_cast<int>(getSuit())) << std::endl;
}
//overload < than operator to enable Card comparisons
bool operator<(const Card &other) const
{
return this->face_value < other.face_value;
}
//overload > than operator to enable Card comparisons
bool operator>(const Card &other)
{
return this->face_value > other.face_value;
}
//overlaod equality operator to enable Card comparisons
bool operator==(const Card &other)
{
return this->face_value == other.face_value && this->card_suit == other.card_suit;
}
//assignment operator overload
Card& operator=(const Card &other)
{
std::cout << "assignment op" << std::endl;
if(this == &other)
{
return *this;
}
face_value = other.face_value;
card_suit = other.card_suit;
return *this;
}
/* friend std::ostream& operator<<(std::ostream& out, const Card &rhs)
{
return out << rhs.getCardFace(static_cast<int>(rhs.getValue()))
<< rhs.getSuitPic(static_cast<int>(rhs.getSuit()));
}*/
};
//print out card value/suit using operator overload
inline std::ostream& operator<<(std::ostream& out, const Card &rhs)
{
return out << rhs.getCardFace(static_cast<int>(rhs.getValue()))
<< rhs.getSuitPic(static_cast<int>(rhs.getSuit()));
}
#ifndef Deck_hpp
#define Deck_hpp
#include <vector>
#include <array>
#include <random>
#include <iterator>
#include <algorithm>
#include "Card.hpp"
class Deck
{
private:
std::array<Card*, 52> deck;
public:
//constructor
Deck() { fillDeck(); }
~Deck();
void fillDeck();
void showDeck();
void shuffleDeck();
void sort();
const Card& getCard(int pos) const;
const Card& operator[](int i)const
{
return *deck[i];
}
};
#endif /* Deck_hpp */
#include "Deck.hpp"
#include "Card.hpp"
#include <iostream>
Deck::~Deck()
{
for(int i{}; i < 52; i ++)
{
delete deck[i];
deck[i] = nullptr;
}
//std::cout << "deck dest" << std::endl;
}
void Deck::fillDeck()
{
std::cout << "deck cons" << std::endl;
for(int j{};j < 52; j++)
{
deck[j] = new Card(j % 13, j / 13);
}
}
void Deck::showDeck()
{
for(int i{};i < 52;i++)
{
std:: cout << *deck[i] << " ";
if(i == 12 || i == 25 || i == 38)
{
std::cout << std::endl;
}
}
}
void Deck::shuffleDeck()
{
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(deck.begin(), deck.end(), g);
}
void Deck::sort()
{
std::sort(deck.begin(), deck.end(), []( const Card *lhs, const Card *rhs){
return lhs < rhs; } );
}
const Card& Deck::getCard(int pos) const
{
return *deck[pos];
}
|