编写一段c++写的扑克牌游戏
时间: 2023-07-01 10:13:31 浏览: 67
好的,这里给你一个简单的C++扑克牌游戏示例代码,供你参考。
```c++
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <ctime>
using namespace std;
// 扑克牌类
class Card {
public:
enum Suit {SPADES, HEARTS, CLUBS, DIAMONDS};
enum Rank {ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING};
Card(Suit s = SPADES, Rank r = ACE) : suit(s), rank(r) {}
int getValue() const {
if (rank >= TEN) {
return 10;
} else {
return rank + 1;
}
}
void display() const {
string suitStr, rankStr;
switch (suit) {
case SPADES:
suitStr = "spades";
break;
case HEARTS:
suitStr = "hearts";
break;
case CLUBS:
suitStr = "clubs";
break;
case DIAMONDS:
suitStr = "diamonds";
break;
}
switch (rank) {
case ACE:
rankStr = "Ace";
break;
case TWO:
rankStr = "2";
break;
case THREE:
rankStr = "3";
break;
case FOUR:
rankStr = "4";
break;
case FIVE:
rankStr = "5";
break;
case SIX:
rankStr = "6";
break;
case SEVEN:
rankStr = "7";
break;
case EIGHT:
rankStr = "8";
break;
case NINE:
rankStr = "9";
break;
case TEN:
rankStr = "10";
break;
case JACK:
rankStr = "Jack";
break;
case QUEEN:
rankStr = "Queen";
break;
case KING:
rankStr = "King";
break;
}
cout << rankStr << " of " << suitStr;
}
private:
Suit suit;
Rank rank;
};
// 扑克牌组类
class Deck {
public:
Deck() {
for (int s = Card::SPADES; s <= Card::DIAMONDS; ++s) {
for (int r = Card::ACE; r <= Card::KING; ++r) {
cards.push_back(Card(static_cast<Card::Suit>(s), static_cast<Card::Rank>(r)));
}
}
}
void shuffle() {
random_shuffle(cards.begin(), cards.end());
}
Card dealCard() {
Card temp = cards.back();
cards.pop_back();
return temp;
}
void display() const {
for (int i = 0; i < cards.size(); ++i) {
cards[i].display();
cout << endl;
}
}
private:
vector<Card> cards;
};
// 玩家类
class Player {
public:
Player(const string& n = "player") : name(n) {}
void addCard(Card c) {
hand.push_back(c);
}
void clear() {
hand.clear();
}
int getTotal() const {
int total = 0;
for (int i = 0; i < hand.size(); ++i) {
total += hand[i].getValue();
}
return total;
}
void display() const {
cout << name << ":\n";
for (int i = 0; i < hand.size(); ++i) {
hand[i].display();
cout << endl;
}
cout << "Total: " << getTotal() << endl;
}
protected:
vector<Card> hand;
string name;
};
// 游戏类
class Game {
public:
Game(const vector<string>& names) {
for (int i = 0; i < names.size(); ++i) {
players.push_back(Player(names[i]));
}
deck = Deck();
deck.shuffle();
}
void play() {
deal();
displayPlayers();
int winner = findWinner();
cout << "The winner is " << players[winner].name << ".\n";
for (int i = 0; i < players.size(); ++i) {
players[i].clear();
}
}
protected:
vector<Player> players;
Deck deck;
virtual int findWinner() = 0;
void deal() {
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < players.size(); ++j) {
players[j].addCard(deck.dealCard());
}
}
}
void displayPlayers() const {
for (int i = 0; i < players.size(); ++i) {
players[i].display();
}
}
};
// 21点游戏类
class BlackjackGame : public Game {
public:
BlackjackGame(const vector<string>& names) : Game(names) {}
protected:
virtual int findWinner() {
int maxScore = 0;
int winner = 0;
for (int i = 0; i < players.size(); ++i) {
int score = players[i].getTotal();
if (score > maxScore && score <= 21) {
maxScore = score;
winner = i;
}
}
return winner;
}
};
int main() {
srand(static_cast<unsigned int>(time(0)));
cout << "Enter player names (enter Q to quit):\n";
vector<string> names;
string name;
while (cin >> name && name != "Q") {
names.push_back(name);
}
BlackjackGame game(names);
game.play();
return 0;
}
```
这段代码实现了一个简单的21点游戏,使用了扑克牌类、扑克牌组类、玩家类和游戏类,其中游戏类是一个抽象类,由21点游戏类派生实现了游戏逻辑。在游戏开始时,玩家输入名字,游戏开始后,每个玩家发两张牌,计算总分,最后比较各玩家的总分,输出获胜者。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)