#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;
string suits[4]={"H", "D", "S", "C"};
string faces[13]={"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
class Card
{
public:
int face,suit;
Card(int face,int suit)
{
this->face=face;
this->suit=suit;
}
string toString()
{
string nameSuit = suits[suit];
string nameFace = faces[face];
return nameSuit + nameFace;
}
};
class DeckOfCards
{
private:
vector<Card> deck;
int currentLocation, location;
public:
DeckOfCards()
{ currentLocation = 0;
for(int i=0;i<13;i++)
for(int k=0;k<4;k++)
{
Card card(i,k);
deck.push_back(card);
}
}
void reset()
{
deck.erase(deck.begin(), deck.end());
currentLocation = 0;
for(int i=0;i<13;i++)
for(int k=0;k<4;k++)
{
Card card(i,k);
deck.push_back(card);
}
}
void shuffle()
{
for(int i=52;i > 0;i--)
{
location=rand()%i;
swap(deck[location], deck[i]);
}
}
string draw()
{
if (currentLocation < deck.size())
{
string ret = deck[currentLocation].toString();
currentLocation++;
return ret;
}
} . // ERROR CONTROL MAY REACH NON VOID FUNCTION
void print()
{
for (int i = currentLocation; i < deck.size(); i++) {
cout << deck[i].toString() << " ";
}
cout << endl;
}
};
void tockenize(string str, vector<string> &a)
{
string delimiter = " ";
size_t pos = 0;
string token;
while ((pos = str.find(delimiter)) != string::npos) {
token = str.substr(0, pos);
a.push_back(token);
/
str.erase(0, pos + delimiter.size());
}
token = str.substr(0, pos); /
a.push_back(token);
/
}
void probability(DeckOfCards &deck)
{
cin.ignore();
char arr[1000];
cout << "Enter space separated combination of cards <-:";
cin.getline(arr, 1000); /
string input(arr);
vector<string> requiredCards;
tockenize(input, requiredCards); /
sort(requiredCards.begin(), requiredCards.end()); /
int matchcount = 0;
for (int i = 0; i < 1000; i++) { /
deck.reset();
deck.shuffle();
vector<string> selectedCards;
for (int j = 0; j < requiredCards.size(); j++) /
selectedCards.push_back(deck.draw());
sort(selectedCards.begin(), selectedCards.end()); /
int j = 0;
for (;j < requiredCards.size(); j++)
if ( selectedCards[j] != requiredCards[j]) break; /
if (j == requiredCards.size()) matchcount++; /
}
cout << "Total match found = " << matchcount << endl;
cout << "empirical probabilty =" << matchcount/ 1000.0 << endl; /
/
}
int main ()
{ srand (time(NULL)); /
int choice = 1;
DeckOfCards deck;
while (choice) {
cout << "1: Reset cards\n2: Shuffle cards\n3: Draw card without replacement\n4: Print current content of card\n5: Calculate empirical prob\n0: Quit\n";
cin >> choice;
switch (choice) {
case 1:
deck.reset();
cout << "Card Reset" << endl;
break;
case 2:
deck.shuffle();
cout << "Card Shuffled" << endl;
break;
case 3:
cout << "Drawn card is " << deck.draw() << endl;
break;
case 4:
deck.print();
break;
case 5:
probability(deck);
break;
case 6:
exit(0);
}
}
};