c++输出52张扑克牌
时间: 2024-10-13 07:05:17 浏览: 44
C++课程设计21点扑克牌游戏样本.doc
在C++中,输出52张扑克牌需要创建一个包含所有花色和每个花色对应点数的结构体或类,并利用循环遍历来显示。这里我们简单使用枚举和数组的方式来表示:
```cpp
#include <iostream>
using namespace std;
enum Suit { HEARTS, DIAMONDS, CLUBS, SPADES };
enum Rank { ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING };
class Card {
public:
Suit suit;
Rank rank;
// 构造函数
Card(Suit s, Rank r) : suit(s), rank(r) {}
void print() {
switch (rank) {
case ACE: cout << "A"; break;
case JACK: cout << "J"; break;
case QUEEN: cout << "Q"; break;
case KING: cout << "K"; break;
default: cout << to_string(rank);
}
switch (suit) {
case HEARTS: cout << "♥"; break;
case DIAMONDS: cout << "♦"; break;
case CLUBS: cout << "♣"; break;
case SPADES: cout << "♠"; break;
}
}
};
int main() {
const int NUM_CARDS = 52;
Card deck[NUM_CARDS];
for (int i = 0; i < NUM_CARDS; ++i) {
if (i % 4 == 0) { // 每四张牌一组,切换花色
deck[i] = {HEARTS, static_cast<Rank>(i / 4 + 1)};
deck[i+1] = {DIAMONDS, static_cast<Rank>(i / 4 + 1)};
deck[i+2] = {CLUBS, static_cast<Rank>(i / 4 + 1)};
deck[i+3] = {SPADES, static_cast<Rank>(i / 4 + 1)};
} else {
deck[i] = {deck[i-1].suit, static_cast<Rank>((i - 1) % 13 + 1)}; // 保持当前花色不变,每种花色有四种牌
}
deck[i].print();
if ((i + 1) % 13 == 0 || i == NUM_CARDS - 1) { // 输出换行,每13张牌换一行
cout << endl;
}
}
return 0;
}
阅读全文