用c++写二十一点游戏的代码
时间: 2023-11-05 11:05:15 浏览: 53
以下是一个简单的 C++ 二十一点游戏代码,包括洗牌、发牌、计算点数和判断胜负等基本功能:
```cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;
// 牌面大小
enum Rank {
ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING
};
// 牌的花色
enum Suit {
CLUBS, DIAMONDS, HEARTS, SPADES
};
// 牌结构体
struct Card {
Rank rank;
Suit suit;
};
// 生成一副新牌并洗牌
void shuffle(vector<Card>& deck) {
deck.clear();
for (int s = CLUBS; s <= SPADES; ++s) {
for (int r = ACE; r <= KING; ++r) {
deck.push_back({ static_cast<Rank>(r), static_cast<Suit>(s) });
}
}
srand(time(NULL));
for (size_t i = 0; i < deck.size(); ++i) {
size_t j = rand() % deck.size();
swap(deck[i], deck[j]);
}
}
// 计算一张牌的点数
int getCardValue(const Card& card) {
switch (card.rank) {
case ACE:
return 1;
case TWO:
case THREE:
case FOUR:
case FIVE:
case SIX:
case SEVEN:
case EIGHT:
case NINE:
return card.rank;
case TEN:
case JACK:
case QUEEN:
case KING:
return 10;
default:
return 0;
}
}
// 计算一手牌的点数,如果牌中有 ACE,尽量使其作为 11 点计算,否则作为 1 点计算
int getHandValue(const vector<Card>& hand) {
int numAces = 0;
int value = 0;
for (const auto& card : hand) {
int cardValue = getCardValue(card);
if (cardValue == 1) {
++numAces;
}
value += cardValue;
}
while (numAces > 0 && value + 10 <= 21) {
value += 10;
--numAces;
}
return value;
}
int main() {
vector<Card> deck;
shuffle(deck);
vector<Card> playerHand, dealerHand;
playerHand.push_back(deck.back());
deck.pop_back();
dealerHand.push_back(deck.back());
deck.pop_back();
playerHand.push_back(deck.back());
deck.pop_back();
cout << "你的牌是:" << endl;
for (const auto& card : playerHand) {
cout << static_cast<int>(card.rank) << " ";
}
cout << endl;
cout << "庄家的牌是:" << endl;
cout << static_cast<int>(dealerHand[0].rank) << " ?" << endl;
while (true) {
int playerValue = getHandValue(playerHand);
if (playerValue == 21) {
cout << "你赢了!" << endl;
break;
} else if (playerValue > 21) {
cout << "你输了!" << endl;
break;
} else {
cout << "你的点数是 " << playerValue << ",是否继续要牌?(y/n)" << endl;
char choice;
cin >> choice;
if (choice == 'y') {
playerHand.push_back(deck.back());
deck.pop_back();
} else {
cout << "你停止要牌,庄家开始抽牌。" << endl;
break;
}
}
}
while (true) {
int dealerValue = getHandValue(dealerHand);
if (dealerValue >= 17) {
cout << "庄家的牌是:" << endl;
for (const auto& card : dealerHand) {
cout << static_cast<int>(card.rank) << " ";
}
cout << endl;
if (dealerValue > 21) {
cout << "庄家输了!" << endl;
} else {
int playerValue = getHandValue(playerHand);
if (dealerValue > playerValue) {
cout << "庄家赢了!" << endl;
} else if (dealerValue < playerValue) {
cout << "你赢了!" << endl;
} else {
cout << "平局!" << endl;
}
}
break;
} else {
dealerHand.push_back(deck.back());
cout << "庄家抽了一张牌。" << endl;
deck.pop_back();
}
}
return 0;
}
```
这个代码只是一个简单的演示,还有很多细节需要注意,比如牌的数量、胜负规则、Ace 的计算方式等等。
相关推荐
![](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)