它是一副纸牌。只需这样做:
- 初始化卡组。将所有 52 张卡片布置在一个固定的 52 张卡片阵列中。
- 洗牌。
nextCard
通过从零 (0) 开始将索引初始化到您的牌组来开始绘图循环。每次“抽牌”(在 处的牌deck[nextCard]
)前进nextCard
一。当nextCard
== 52 时,你就没有牌了。
以下是如何设置甲板的示例。我把nextCard
索引和绘图算法留给你。
#include <iostream>
#include <algorithm>
using namespace std;
// names of ranks.
static const char *ranks[] =
{
"Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King"
};
// name of suites
static const char *suits[] =
{
"Spades", "Clubs", "Diamonds", "Hearts"
};
void print_card(int n)
{
cout << ranks[n % 13] << " of " << suits[n / 13] << endl;
}
int main()
{
srand((unsigned int)time(NULL));
int deck[52];
// Prime, shuffle, dump
for (int i=0;i<52;deck[i++]=i);
random_shuffle(deck, deck+52);
for_each(deck, deck+52, print_card);
return 0;
}
甲板转储的示例如下:
Seven of Diamonds
Five of Hearts
Nine of Diamonds
Ten of Diamonds
Three of Diamonds
Seven of Clubs
King of Clubs
Five of Diamonds
Ace of Spades
Four of Spades
Two of Diamonds
Five of Clubs
Queen of Diamonds
Six of Spades
Three of Hearts
Ten of Spades
Two of Clubs
Ace of Hearts
Four of Hearts
Four of Diamonds
Ace of Diamonds
Six of Diamonds
Jack of Clubs
King of Spades
Jack of Diamonds
Four of Clubs
Eight of Diamonds
Queen of Hearts
King of Hearts
Ace of Clubs
Three of Spades
Two of Spades
Six of Clubs
Seven of Hearts
Nine of Clubs
Jack of Hearts
Nine of Hearts
Eight of Clubs
Ten of Clubs
Five of Spades
Three of Clubs
Queen of Clubs
Seven of Spades
Eight of Spades
Ten of Hearts
King of Diamonds
Jack of Spades
Six of Hearts
Queen of Spades
Nine of Spades
Two of Hearts
Eight of Hearts