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
|
#include <iostream>
#include <string>
#include <cstdlib>
const int NumSuits = 4;
const int NumFaces = 13;
struct Card {
int suit; // 1, 2, 3, 4
int face; // 1, 2, 3, ... 13
};
static const char* const suits[] = {
"Clubs", "Hearts", "Spades", "Diamonds" };
static const char* const faces[] = { "Ace", "2", "3", "4", "5",
"6", "7", "8", "9", "10", "Jack", "Queen", "King" };
std::string to_string(const Card& card)
{
return std::string(faces[card.face - 1]) + " of " + std::string(suits[card.suit - 1]);
}
// expects 1-based suit number
std::string suit_to_string(int suit)
{
return suits[suit - 1];
}
// Just for demonstration.
// In a real situation, you'd want to pick out of a shuffled deck.
Card generate_random_card()
{
// just using C-style rand for brevity
return Card { rand() % NumSuits + 1,
rand() % NumFaces + 1 };
}
// returns 1-based suit that was found, otherwise 0
int any_suit_contains_full_run(int table[][NumFaces])
{
for (int i = 0; i < NumSuits; i++)
{
bool missing_card = false;
for (int j = 0; j < NumFaces; j++)
{
if (table[i][j] == 0)
{
missing_card = true;
break;
}
}
if (!missing_card)
{
return (i + 1);
}
}
return 0; // all suits contain at least one missing card
}
void print_table(int table[][NumFaces])
{
for (int i = 0; i < NumSuits; i++)
{
for (int j = 0; j < NumFaces; j++)
{
std::cout << table[i][j] << ' ';
}
std::cout << '\n';
}
}
int main()
{
int table[NumSuits][NumFaces] { };
while (true)
{
//print_table(table);
Card card = generate_random_card();
std::cout << "Adding a " << to_string(card) << '\n';
// add card to table
table[card.suit - 1][card.face - 1]++;
int suit = 0;
if ((suit = any_suit_contains_full_run(table)) != 0)
{
std::cout << "\nFull run of: " << suit_to_string(suit) << '\n';
print_table(table);
break;
}
}
}
| |