// Get and print 5 unique cards
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int Unassigned = -1;
// Function prototypes
int getUniqueCard(int c1 = Unassigned, int c2 = Unassigned, int c3 = Unassigned, int c4 = Unassigned);
void printUpTo5Cards(int c1, int c2 = Unassigned, int c3 = Unassigned, int c4 = Unassigned, int c5 = Unassigned);
void printCard(int card);
bool cardsMatch(int card1, int card2);
int main()
{
srand(time(0));
int card1, card2, card3, card4, card5;
card1 = getUniqueCard(Unassigned,Unassigned,Unassigned,Unassigned);
printUpTo5Cards(card1);
card2 = getUniqueCard(card1,Unassigned,Unassigned,Unassigned);
card3 = getUniqueCard(card1, card2,Unassigned,Unassigned);
printUpTo5Cards(card1,card2,card3);
card4 = getUniqueCard(card1, card2, card3,Unassigned);
card5 = getUniqueCard(card1, card2, card3, card4);
printUpTo5Cards(card1,card2,card3,card4,card5);
}
int getUniqueCard(int c1, int c2, int c3, int c4)
{
int newCard = Unassigned;
do
{
newCard = rand() % 52;
}
while (cardsMatch(newCard,c1) || cardsMatch(newCard,c2) || cardsMatch(newCard,c3) || cardsMatch(newCard,c4));
return newCard;
}
void printUpTo5Cards(int c1, int c2, int c3, int c4, int c5)
{
printCard(c1);
if (c2 != Unassigned)
printCard(c2);
if (c3 != Unassigned)
printCard(c3);
if (c4 != Unassigned)
printCard(c4);
if (c5 != Unassigned)
printCard(c5);
cout << endl;
}
void printCard(int card)
{
switch (card%13)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
cout << static_cast<char>('0' + card%13 + 2);
break;
case 8:
cout << 'T';
break;
case 9:
cout << 'J';
break;
case 10:
cout << 'Q';
break;
case 11:
cout << 'K';
break;
case 12:
cout << 'A';
break;
default:
cout << '?';
}
char suit;
switch (card % 4)
{
case 0:
suit = 'C';
break;
case 1:
suit = 'D';
break;
case 2:
suit = 'H';
break;
case 3:
suit = 'S';
break;
default:
suit = '?';
}
cout << suit << ' ';
}
bool cardsMatch(int card1, int card2)
{
return card1 == card2;
}