# 斗地主游戏C++实现 以下是一个简化版的斗地主游戏C++实现,包含基本的游戏逻辑、牌型判断和简单AI出牌功能。 ```cpp #include <iostream> #include <vector> #include <algorithm> #include <random> #include <ctime> #include <map> #include <string> using namespace std; // 牌的类型 enum CardType { HEART, // 红桃 DIAMOND, // 方块 CLUB, // 梅花 SPADE, // 黑桃 JOKER // 王 }; // 牌的结构体 struct Card { CardType type; int value; // 3-13(3-10,J,Q,K,A,2), 小王:14, 大王:15 string toString() const { if (type == JOKER) { return (value == 14) ? "小王" : "大王"; } string typeStr; switch(type) { case HEART: typeStr = "♥"; break; case DIAMOND: typeStr = "♦"; break; case CLUB: typeStr = "♣"; break; case SPADE: typeStr = "♠"; break; default: typeStr = ""; } string valueStr; if (value >= 3 && value <= 10) { valueStr = to_string(value); } else { switch(value) { case 11: valueStr = "J"; break; case 12: valueStr = "Q"; break; case 13: valueStr = "K"; break; case 1: valueStr = "A"; break; case 2: valueStr = "2"; break; default: valueStr = ""; } } return typeStr + valueStr; } bool operator<(const Card& other) const { if (value != other.value) { return value < other.value; } return type < other.type; } }; // 玩家类 class Player { public: string name; vector<Card> handCards; bool isLandlord = false; Player(string n) : name(n) {} void addCard(const Card& card) { handCards.push_back(card); sort(handCards.begin(), handCards.end()); } void showCards() const { cout << name << "的手牌: "; for (const auto& card : handCards) { cout << card.toString() << " "; } cout << endl; } // 简单AI出牌逻辑 vector<Card> playCards(const vector<Card>& lastPlay) { if (handCards.empty()) return {}; // 如果是第一个出牌或者要不起 if (lastPlay.empty()) { // 简单策略: 出最小的单牌 vector<Card> play = {handCards.front()}; handCards.erase(handCards.begin()); return play; } // 简单策略: 尝试出比上家大的牌 for (size_t i = 0; i < handCards.size(); ++i) { if (handCards[i].value > lastPlay[0].value) { vector<Card> play = {handCards[i]}; handCards.erase(handCards.begin() + i); return play; } } // 要不起 return {}; } }; // 游戏类 class DouDiZhuGame { private: vector<Card> deck; vector<Player> players; vector<Card> currentPlay; int currentPlayer = 0; void initDeck() { deck.clear(); // 添加普通牌 for (int t = HEART; t <= SPADE; ++t) { for (int v = 3; v <= 15; ++v) { if (v == 11) v = 1; // A if (v == 12) v = 2; // 2 if (v == 13) v = 11; // J if (v == 14) v = 12; // Q if (v == 15) v = 13; // K deck.push_back({static_cast<CardType>(t), v}); } } // 添加大小王 deck.push_back({JOKER, 14}); // 小王 deck.push_back({JOKER, 15}); // 大王 // 洗牌 shuffleDeck(); } void shuffleDeck() { static mt19937 rng(time(nullptr)); shuffle(deck.begin(), deck.end(), rng); } void dealCards() { // 发牌 (留3张底牌) for (int i = 0; i < 51; ++i) { players[i % 3].addCard(deck[i]); } // 简单确定地主 (实际游戏应该通过叫分确定) int landlord = rand() % 3; players[landlord].isLandlord = true; // 地主拿底牌 for (int i = 51; i < 54; ++i) { players[landlord].addCard(deck[i]); } cout << players[landlord].name << "成为地主!" << endl; } public: DouDiZhuGame() { players.emplace_back("玩家1"); players.emplace_back("玩家2"); players.emplace_back("玩家3"); } void startGame() { initDeck(); dealCards(); // 显示初始手牌 for (auto& player : players) { player.showCards(); } // 游戏主循环 while (true) { Player& current = players[currentPlayer]; cout << "\n当前是" << current.name << "的回合" << endl; vector<Card> play; if (current.name == "玩家1") { // 玩家手动出牌 (简化版) cout << "请选择要出的牌(输入索引, 空格分隔, 0不出): "; string input; getline(cin, input); if (input == "0") { play = {}; } else { // 简单处理输入 for (char c : input) { if (isdigit(c)) { int idx = c - '0' - 1; if (idx >= 0 && idx < current.handCards.size()) { play.push_back(current.handCards[idx]); } } } // 从手牌中移除 for (const auto& card : play) { auto it = find(current.handCards.begin(), current.handCards.end(), card); if (it != current.handCards.end()) { current.handCards.erase(it); } } } } else { // AI出牌 play = current.playCards(currentPlay); } if (play.empty()) { cout << current.name << "选择不出" << endl; } else { cout << current.name << "出牌: "; for (const auto& card : play) { cout << card.toString() << " "; } cout << endl; currentPlay = play; // 检查是否出完牌 if (current.handCards.empty()) { cout << "\n游戏结束! " << current.name << "获胜!" << endl; if (current.isLandlord) { cout << "地主方胜利!" << endl; } else { cout << "农民方胜利!" << endl; } return; } } // 切换到下一个玩家 currentPlayer = (currentPlayer + 1) % 3; } } }; int main() { srand(time(nullptr)); DouDiZhuGame game; game.startGame(); return 0; } ``` ## 代码说明 1. 牌型表示:使用`Card`结构体表示每张牌,包含花色和数值 2. 玩家类:`Player`类管理玩家手牌和简单出牌逻辑 3. 游戏流程: - 初始化牌组并洗牌 - 发牌并随机确定地主 - 玩家轮流出牌 - 判断游戏结束条件 ## 三条额外需要注意的逻辑点 1. 当前实现只支持单牌出牌逻辑,需要扩展以支持对子、顺子、等复杂牌型判断 2. AI出牌策略非常基础,可以添加更智能的出牌算法 3. 游戏缺少叫地主环节和记分系统,完整实现需要添加这些功能 [2025-07-18 10:12:54 | AI写代码神器 | 1937点数解答]