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
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <random>
#include <chrono>
enum Suit {Spades, Hearts, Clubs, Diamonds};//change as required;
std::ostream& operator << (std::ostream& os, const Suit& s)//for printing Suit objects
{
switch(s)
{
case Spades: return os << "Spades";
case Hearts: return os << "Hearts";
case Clubs: return os << "Clubs";
case Diamonds: return os << "Diamonds";
default : return os << "";
}
}
const std::string RANK[] = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
struct Card
{
Suit m_itsSuit;
std::string m_itsRank;
Card(const Suit& itsSuit, const std::string& itsRank) :
m_itsSuit(itsSuit), m_itsRank(itsRank){};
};
bool operator < (const Card& lhs, const Card& rhs)//for sorting Card objects
{
return lhs.m_itsSuit < rhs.m_itsSuit ||
(!(rhs.m_itsSuit < lhs.m_itsSuit) && lhs.m_itsRank < rhs.m_itsRank);
}
std::ostream& operator << (std::ostream& os, const Card& c)//for printing Card objects
{
os << c.m_itsSuit << " " << c.m_itsRank << "\n";
return os;
}
int main()
{
std::default_random_engine numGenerator(static_cast<unsigned int>(time(NULL)));
std::uniform_int_distribution<int> rangeSuit(0, 3);
std::uniform_int_distribution<int> rangeRank(0, 12);
std::vector<Card> playerA{};
std::vector<Card> playerB{};
for (size_t i = 0; i < 5; ++i)
{
//create 2 hands of random cards
playerA.emplace_back(Card(static_cast<Suit>(rangeSuit(numGenerator)), RANK[rangeRank(numGenerator)]));
playerB.emplace_back(Card(static_cast<Suit>(rangeSuit(numGenerator)), RANK[rangeRank(numGenerator)]));
}
//sort the hands
std::sort(playerA.begin(), playerA.end());
std::sort(playerB.begin(), playerB.end());
//print the hands
std::cout << "Player A\n";
for (const auto& elem : playerA)std::cout << elem ;
std::cout << "\nPlayer B\n";
for (const auto& elem : playerB)std::cout << elem;
//check which hand has the lowest card
if(playerA[0] < playerB[0])std::cout << "\nA\n"; else std::cout << "\nB\n";
}
|