做一个c++扑克牌程序
时间: 2024-05-06 12:17:12 浏览: 137
扑克牌 c++
好的,下面是一个简单的 C++ 扑克牌程序:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;
// 定义扑克牌结构体
struct Card {
string suit; // 花色
string face; // 点数
};
// 初始化扑克牌
void initCards(vector<Card>& cards) {
string suits[] = {"Spades", "Hearts", "Diamonds", "Clubs"};
string faces[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
Card card;
card.suit = suits[i];
card.face = faces[j];
cards.push_back(card);
}
}
}
// 洗牌
void shuffleCards(vector<Card>& cards) {
srand(time(NULL));
random_shuffle(cards.begin(), cards.end());
}
// 发牌
void dealCards(vector<Card>& cards, vector<Card>& player1, vector<Card>& player2, vector<Card>& player3, vector<Card>& player4) {
for (int i = 0; i < 13; i++) {
player1.push_back(cards[i]);
player2.push_back(cards[i+13]);
player3.push_back(cards[i+26]);
player4.push_back(cards[i+39]);
}
}
// 打印扑克牌
void printCards(const vector<Card>& cards) {
for (const auto& card : cards) {
cout << card.suit << " " << card.face << endl;
}
}
// 打印玩家手牌
void printPlayerCards(const vector<Card>& playerCards) {
for (const auto& card : playerCards) {
cout << card.suit << " " << card.face << endl;
}
}
int main() {
vector<Card> cards; // 存放扑克牌
vector<Card> player1; // 玩家1手牌
vector<Card> player2; // 玩家2手牌
vector<Card> player3; // 玩家3手牌
vector<Card> player4; // 玩家4手牌
initCards(cards); // 初始化扑克牌
shuffleCards(cards); // 洗牌
dealCards(cards, player1, player2, player3, player4); // 发牌
// 打印每个玩家的手牌
cout << "Player 1 Cards:" << endl;
printPlayerCards(player1);
cout << endl;
cout << "Player 2 Cards:" << endl;
printPlayerCards(player2);
cout << endl;
cout << "Player 3 Cards:" << endl;
printPlayerCards(player3);
cout << endl;
cout << "Player 4 Cards:" << endl;
printPlayerCards(player4);
cout << endl;
return 0;
}
```
这个程序会初始化一副扑克牌,然后洗牌,最后发牌给四个玩家。你可以根据需要进行修改和扩展。
阅读全文