跳到正文

休闲·娱乐

斗地主游戏

Zhangyuehang20150330约 97 分钟
原文
文章信息与版本

文章信息

发布
2026年8月19日
收录
2026年8月22日
更新
2026年11月15日
篇幅
约 97 分钟
获赞
1

版本

  1. v3当前
  2. v2
  3. v1

下面是一个斗地主代码,祝大家玩的开心!(包含电脑玩家)

C++
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <ctime>
#include <chrono>
#include <map>
#include <string>
#include <limits>
#include <sstream>
#include <set>

using namespace std;

// 扑克牌点数
const vector<string> RANKS = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
const vector<string> JOKERS = {"小王", "大王"};

// 牌型定义
enum CardType {
    SINGLE,
    PAIR,
    THREE,
    THREE_WITH_ONE,
    THREE_WITH_TWO,
    STRAIGHT,
    CONSECUTIVE_PAIRS,
    AIRPLANE,
    AIRPLANE_WITH_WINGS,
    FOUR_WITH_TWO,
    BOMB,
    KING_BOMB,
    INVALID
};

// 牌型名称映射
string getCardTypeName(CardType type) {
    switch(type) {
        case SINGLE: return "单张";
        case PAIR: return "对子";
        case THREE: return "三张";
        case THREE_WITH_ONE: return "三带一";
        case THREE_WITH_TWO: return "三带二";
        case STRAIGHT: return "顺子";
        case CONSECUTIVE_PAIRS: return "连对";
        case AIRPLANE: return "飞机";
        case AIRPLANE_WITH_WINGS: return "飞机带翅膀";
        case FOUR_WITH_TWO: return "四带二";
        case BOMB: return "炸弹";
        case KING_BOMB: return "王炸";
        default: return "无效牌型";
    }
}

// 卡牌结构体
struct Card {
    int value;
    string name;
    
    bool operator<(const Card& other) const {
        return value < other.value;
    }
    
    bool operator==(const Card& other) const {
        return value == other.value && name == other.name;
    }
};

// 玩家基类
class Player {
public:
    string name;
    vector<Card> hand;
    bool isLandlord = false;
    
    Player(const string& n) : name(n) {}
    virtual ~Player() {}
    
    void sortHand() {
        sort(hand.begin(), hand.end());
    }
    
    void showHand() const {
        cout << name << "的手牌(" << hand.size() << "张): ";
        for (size_t i = 0; i < hand.size(); i++) {
            cout << hand[i].name;
            if (i != hand.size() - 1) cout << " ";
        }
        cout << endl;
    }
    
    virtual vector<Card> play(const vector<Card>& lastPlay, const vector<Player*>& players) = 0;
    virtual int bidForLandlord(int currentMaxBid) = 0;
    
    bool hasCards() const {
        return !hand.empty();
    }
    
    void removeCards(const vector<Card>& cards) {
        for (const auto& card : cards) {
            auto it = find(hand.begin(), hand.end(), card);
            if (it != hand.end()) {
                hand.erase(it);
            }
        }
    }
};

// 人类玩家
class HumanPlayer : public Player {
public:
    HumanPlayer(const string& n) : Player(n) {}
    
    int bidForLandlord(int currentMaxBid) override {
        int bid;
        while (true) {
            cout << "请叫地主(0=不叫";
            if (currentMaxBid < 1) cout << ",1=1分";
            if (currentMaxBid < 2) cout << ",2=2分";
            if (currentMaxBid < 3) cout << ",3=3分";
            cout << "): ";
            cin >> bid;
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            
            if (bid == 0) return 0;
            if (bid > currentMaxBid && bid <= 3) {
                return bid;
            }
            cout << "无效的叫分!当前最高分为 " << currentMaxBid 
                 << ",你只能叫更高的分数或不叫。" << endl;
        }
    }
    
    vector<Card> play(const vector<Card>& lastPlay, const vector<Player*>& players) override;
};

// 机器人玩家
class AIPlayer : public Player {
public:
    AIPlayer(const string& n) : Player(n) {}
    
    int bidForLandlord(int currentMaxBid) override;
    vector<Card> play(const vector<Card>& lastPlay, const vector<Player*>& players) override;
    
private:
    vector<Card> firstPlayStrategy();
    vector<Card> followPlayStrategy(const vector<Card>& lastPlay);
    
    vector<vector<Card>> findSingles();
    vector<vector<Card>> findPairs();
    vector<vector<Card>> findThrees();
    vector<vector<Card>> findThreeWithOnes();
    vector<vector<Card>> findThreeWithTwos();
    vector<vector<Card>> findStraights();
    vector<vector<Card>> findConsecutivePairs();
    vector<vector<Card>> findAirplanes();
    vector<vector<Card>> findAirplanesWithWings();
    vector<vector<Card>> findFourWithTwos();
    vector<vector<Card>> findBombs();
    vector<Card> findKingBomb();
    
    vector<vector<Card>> findGreaterSingles(const Card& card);
    vector<vector<Card>> findGreaterPairs(const Card& card);
    vector<vector<Card>> findGreaterThrees(const Card& card);
    vector<vector<Card>> findGreaterThreeWithOnes(const vector<Card>& cards);
    vector<vector<Card>> findGreaterThreeWithTwos(const vector<Card>& cards);
    vector<vector<Card>> findGreaterStraights(const vector<Card>& cards);
    vector<vector<Card>> findGreaterConsecutivePairs(const vector<Card>& cards);
    vector<vector<Card>> findGreaterAirplanes(const vector<Card>& cards);
    vector<vector<Card>> findGreaterAirplanesWithWings(const vector<Card>& cards);
    vector<vector<Card>> findGreaterFourWithTwos(const vector<Card>& cards);
    vector<vector<Card>> findGreaterBombs(const Card& card);
    
    map<int, int> getCardCount() const;
    vector<int> getSortedValues() const;
};

// 游戏类
class Game {
private:
    vector<Player*> players;
    vector<Card> deck;
    vector<Card> landlordCards;
    int currentPlayer = 0;
    mt19937 rng;
    
public:
    Game();
    ~Game();
    
    void initializeDeck();
    void shuffleDeck();
    void dealCards();
    void selectLandlord();
    void playGame();
    void announceWinner(Player* winner);
    void start();
    
    static CardType getCardType(const vector<Card>& cards);
    static bool canBeat(const vector<Card>& lastPlay, CardType lastType, 
                       const vector<Card>& currentPlay, CardType currentType);
    static int getKeyValue(const vector<Card>& cards, CardType type);
    static bool isConsecutive(const vector<int>& values);
};

// HumanPlayer 实现
vector<Card> HumanPlayer::play(const vector<Card>& lastPlay, const vector<Player*>& players) {
    if (hand.empty()) return {};
    
    cout << "\n========================================" << endl;
    cout << "轮到你出牌了。" << endl;
    showHand();
    
    if (!lastPlay.empty()) {
        cout << "上家出的牌: ";
        for (const auto& card : lastPlay) {
            cout << card.name << " ";
        }
        CardType type = Game::getCardType(lastPlay);
        cout << " (" << getCardTypeName(type) << ")" << endl;
    } else {
        cout << "你是第一个出牌的玩家。" << endl;
    }
    
    vector<Card> selectedCards;
    string input;
    
    while (true) {
        cout << "请输入你要出的牌(用空格分隔,输入'pass'跳过): ";
        getline(cin, input);
        
        if (input == "pass" || input == "p") {
            return {};
        }
        
        selectedCards.clear();
        vector<string> cardNames;
        istringstream iss(input);
        string cardName;
        
        while (iss >> cardName) {
            cardNames.push_back(cardName);
        }
        
        if (cardNames.empty()) {
            cout << "输入无效,请重新输入。" << endl;
            continue;
        }
        
        bool allFound = true;
        vector<Card> tempHand = hand;
        
        for (const auto& name : cardNames) {
            bool found = false;
            for (auto it = tempHand.begin(); it != tempHand.end(); ++it) {
                if (it->name == name) {
                    selectedCards.push_back(*it);
                    tempHand.erase(it);
                    found = true;
                    break;
                }
            }
            if (!found) {
                cout << "没有找到牌: " << name << endl;
                allFound = false;
                break;
            }
        }
        
        if (!allFound) {
            continue;
        }
        
        sort(selectedCards.begin(), selectedCards.end());
        
        CardType type = Game::getCardType(selectedCards);
        if (type == INVALID) {
            cout << "无效的牌型,请重新选择。" << endl;
            continue;
        }
        
        if (!lastPlay.empty()) {
            CardType lastType = Game::getCardType(lastPlay);
            if (!Game::canBeat(lastPlay, lastType, selectedCards, type)) {
                cout << "你的牌不能大过上家的牌,请重新选择。" << endl;
                continue;
            }
        }
        
        break;
    }
    
    removeCards(selectedCards);
    return selectedCards;
}

// AIPlayer 实现
int AIPlayer::bidForLandlord(int currentMaxBid) {
    float score = 0;
    map<int, int> count = getCardCount();
    
    for (const auto& card : hand) {
        if (card.value >= 13) score += 1.0;
        else if (card.value >= 11) score += 0.5;
        else if (card.value >= 9) score += 0.3;
    }
    
    for (const auto& p : count) {
        if (p.second == 4) score += 2.0;
    }
    
    if (count[16] > 0 && count[17] > 0) score += 3.0;
    
    int pairCount = 0;
    for (const auto& p : count) {
        if (p.second >= 2) pairCount++;
    }
    if (pairCount >= 3) score += 1.0;
    
    int threeCount = 0;
    for (const auto& p : count) {
        if (p.second >= 3) threeCount++;
    }
    if (threeCount >= 2) score += 1.5;
    
    static mt19937 rng(static_cast<unsigned>(chrono::system_clock::now().time_since_epoch().count()));
    int randomFactor = uniform_int_distribution<int>(0, 100)(rng);
    
    if (score > 8 && randomFactor > 20 && currentMaxBid < 3) return 3;
    if (score > 5 && randomFactor > 30 && currentMaxBid < 2) return 2;
    if (score > 3 && randomFactor > 50 && currentMaxBid < 1) return 1;
    return 0;
}

vector<Card> AIPlayer::play(const vector<Card>& lastPlay, const vector<Player*>& players) {
    if (hand.empty()) return {};
    
    vector<Card> playCards;
    
    if (lastPlay.empty()) {
        playCards = firstPlayStrategy();
    } else {
        playCards = followPlayStrategy(lastPlay);
    }
    
    if (!playCards.empty()) {
        cout << name << "出牌: ";
        for (const auto& card : playCards) {
            cout << card.name << " ";
        }
        CardType type = Game::getCardType(playCards);
        cout << " (" << getCardTypeName(type) << ")" << endl;
        removeCards(playCards);
    } else {
        cout << name << "选择跳过。" << endl;
    }
    
    return playCards;
}

vector<Card> AIPlayer::firstPlayStrategy() {
    sortHand();
    
    vector<vector<Card>> straights = findStraights();
    if (!straights.empty()) {
        return straights[0];
    }
    
    vector<vector<Card>> consecutivePairs = findConsecutivePairs();
    if (!consecutivePairs.empty()) {
        return consecutivePairs[0];
    }
    
    vector<vector<Card>> airplanes = findAirplanes();
    if (!airplanes.empty()) {
        return airplanes[0];
    }
    
    vector<vector<Card>> threeWithTwos = findThreeWithTwos();
    if (!threeWithTwos.empty()) {
        return threeWithTwos[0];
    }
    
    vector<vector<Card>> threeWithOnes = findThreeWithOnes();
    if (!threeWithOnes.empty()) {
        return threeWithOnes[0];
    }
    
    vector<vector<Card>> threes = findThrees();
    if (!threes.empty()) {
        return threes[0];
    }
    
    vector<vector<Card>> pairs = findPairs();
    if (!pairs.empty()) {
        return pairs[0];
    }
    
    return vector<Card>{hand[0]};
}

vector<Card> AIPlayer::followPlayStrategy(const vector<Card>& lastPlay) {
    CardType lastType = Game::getCardType(lastPlay);
    vector<vector<Card>> possiblePlays;
    
    switch (lastType) {
        case SINGLE:
            possiblePlays = findGreaterSingles(lastPlay[0]);
            break;
        case PAIR:
            possiblePlays = findGreaterPairs(lastPlay[0]);
            break;
        case THREE:
            possiblePlays = findGreaterThrees(lastPlay[0]);
            break;
        case THREE_WITH_ONE:
            possiblePlays = findGreaterThreeWithOnes(lastPlay);
            break;
        case THREE_WITH_TWO:
            possiblePlays = findGreaterThreeWithTwos(lastPlay);
            break;
        case STRAIGHT:
            possiblePlays = findGreaterStraights(lastPlay);
            break;
        case CONSECUTIVE_PAIRS:
            possiblePlays = findGreaterConsecutivePairs(lastPlay);
            break;
        case AIRPLANE:
            possiblePlays = findGreaterAirplanes(lastPlay);
            break;
        case AIRPLANE_WITH_WINGS:
            possiblePlays = findGreaterAirplanesWithWings(lastPlay);
            break;
        case FOUR_WITH_TWO:
            possiblePlays = findGreaterFourWithTwos(lastPlay);
            break;
        case BOMB:
            possiblePlays = findGreaterBombs(lastPlay[0]);
            break;
        default:
            break;
    }
    
    if (possiblePlays.empty() && lastType != BOMB && lastType != KING_BOMB) {
        vector<vector<Card>> bombs = findBombs();
        if (!bombs.empty()) {
            static mt19937 rng(static_cast<unsigned>(chrono::system_clock::now().time_since_epoch().count()));
            uniform_int_distribution<int> dist(0, bombs.size() - 1);
            return bombs[dist(rng)];
        }
        
        vector<Card> kingBomb = findKingBomb();
        if (!kingBomb.empty()) {
            return kingBomb;
        }
    }
    
    if (possiblePlays.empty()) {
        return {};
    }
    
    return possiblePlays[0];
}

map<int, int> AIPlayer::getCardCount() const {
    map<int, int> count;
    for (const auto& card : hand) {
        count[card.value]++;
    }
    return count;
}

vector<int> AIPlayer::getSortedValues() const {
    vector<int> values;
    for (const auto& card : hand) {
        values.push_back(card.value);
    }
    sort(values.begin(), values.end());
    return values;
}

vector<vector<Card>> AIPlayer::findSingles() {
    vector<vector<Card>> result;
    for (const auto& card : hand) {
        result.push_back({card});
    }
    return result;
}

vector<vector<Card>> AIPlayer::findPairs() {
    vector<vector<Card>> result;
    map<int, int> count = getCardCount();
    
    for (const auto& p : count) {
        if (p.second >= 2) {
            vector<Card> pair;
            int found = 0;
            for (const auto& c : hand) {
                if (c.value == p.first && found < 2) {
                    pair.push_back(c);
                    found++;
                }
            }
            result.push_back(pair);
        }
    }
    return result;
}

vector<vector<Card>> AIPlayer::findThrees() {
    vector<vector<Card>> result;
    map<int, int> count = getCardCount();
    
    for (const auto& p : count) {
        if (p.second >= 3 && p.first < 13) {
            vector<Card> three;
            int found = 0;
            for (const auto& c : hand) {
                if (c.value == p.first && found < 3) {
                    three.push_back(c);
                    found++;
                }
            }
            result.push_back(three);
        }
    }
    return result;
}

vector<vector<Card>> AIPlayer::findThreeWithOnes() {
    vector<vector<Card>> result;
    vector<vector<Card>> threes = findThrees();
    
    for (const auto& three : threes) {
        for (const auto& c : hand) {
            if (c.value != three[0].value) {
                vector<Card> play = three;
                play.push_back(c);
                result.push_back(play);
                break;
            }
        }
    }
    return result;
}

vector<vector<Card>> AIPlayer::findThreeWithTwos() {
    vector<vector<Card>> result;
    vector<vector<Card>> threes = findThrees();
    vector<vector<Card>> pairs = findPairs();
    
    for (const auto& three : threes) {
        for (const auto& pair : pairs) {
            if (pair[0].value != three[0].value) {
                vector<Card> play = three;
                play.insert(play.end(), pair.begin(), pair.end());
                result.push_back(play);
                break;
            }
        }
    }
    return result;
}

vector<vector<Card>> AIPlayer::findStraights() {
    vector<vector<Card>> result;
    vector<int> values = getSortedValues();
    
    for (int len = 5; len <= 12; len++) {
        for (int start = 0; start < (int)values.size(); start++) {
            if (values[start] >= 13) break;
            if (start + len > (int)values.size()) break;
            
            bool consecutive = true;
            for (int i = 0; i < len - 1; i++) {
                if (values[start + i + 1] != values[start + i] + 1) {
                    consecutive = false;
                    break;
                }
            }
            
            if (consecutive) {
                vector<Card> straight;
                for (int i = 0; i < len; i++) {
                    for (const auto& c : hand) {
                        if (c.value == values[start + i]) {
                            straight.push_back(c);
                            break;
                        }
                    }
                }
                result.push_back(straight);
                break;
            }
        }
    }
    
    return result;
}

vector<vector<Card>> AIPlayer::findConsecutivePairs() {
    vector<vector<Card>> result;
    map<int, int> count = getCardCount();
    vector<int> pairValues;
    
    for (const auto& p : count) {
        if (p.second >= 2 && p.first < 13) {
            pairValues.push_back(p.first);
        }
    }
    
    if (pairValues.size() < 3) return result;
    sort(pairValues.begin(), pairValues.end());
    
    for (int len = 3; len <= (int)pairValues.size(); len++) {
        for (int i = 0; i <= (int)pairValues.size() - len; i++) {
            bool consecutive = true;
            for (int j = 0; j < len - 1; j++) {
                if (pairValues[i + j + 1] != pairValues[i + j] + 1) {
                    consecutive = false;
                    break;
                }
            }
            
            if (consecutive) {
                vector<Card> play;
                for (int j = 0; j < len; j++) {
                    int found = 0;
                    for (const auto& c : hand) {
                        if (c.value == pairValues[i + j] && found < 2) {
                            play.push_back(c);
                            found++;
                        }
                    }
                }
                result.push_back(play);
                break;
            }
        }
    }
    
    return result;
}

vector<vector<Card>> AIPlayer::findAirplanes() {
    vector<vector<Card>> result;
    map<int, int> count = getCardCount();
    vector<int> threeValues;
    
    for (const auto& p : count) {
        if (p.second >= 3 && p.first < 13) {
            threeValues.push_back(p.first);
        }
    }
    
    if (threeValues.size() < 2) return result;
    sort(threeValues.begin(), threeValues.end());
    
    for (int len = 2; len <= (int)threeValues.size(); len++) {
        for (int i = 0; i <= (int)threeValues.size() - len; i++) {
            bool consecutive = true;
            for (int j = 0; j < len - 1; j++) {
                if (threeValues[i + j + 1] != threeValues[i + j] + 1) {
                    consecutive = false;
                    break;
                }
            }
            
            if (consecutive) {
                vector<Card> play;
                for (int j = 0; j < len; j++) {
                    int found = 0;
                    for (const auto& c : hand) {
                        if (c.value == threeValues[i + j] && found < 3) {
                            play.push_back(c);
                            found++;
                        }
                    }
                }
                result.push_back(play);
                break;
            }
        }
    }
    
    return result;
}

vector<vector<Card>> AIPlayer::findAirplanesWithWings() {
    vector<vector<Card>> result;
    vector<vector<Card>> airplanes = findAirplanes();
    
    if (airplanes.empty()) return result;
    
    for (const auto& airplane : airplanes) {
        set<int> airplaneValues;
        for (const auto& c : airplane) {
            airplaneValues.insert(c.value);
        }
        
        int planeSize = airplane.size() / 3;
        
        vector<Card> wings;
        for (const auto& c : hand) {
            if (airplaneValues.find(c.value) == airplaneValues.end()) {
                wings.push_back(c);
                if (wings.size() == planeSize) {
                    break;
                }
            }
        }
        
        if (wings.size() == planeSize) {
            vector<Card> play = airplane;
            play.insert(play.end(), wings.begin(), wings.end());
            result.push_back(play);
        }
        
        wings.clear();
        vector<vector<Card>> pairs = findPairs();
        for (const auto& pair : pairs) {
            if (airplaneValues.find(pair[0].value) == airplaneValues.end()) {
                wings.insert(wings.end(), pair.begin(), pair.end());
                if (wings.size() == planeSize * 2) {
                    break;
                }
            }
        }
        
        if (wings.size() == planeSize * 2) {
            vector<Card> play = airplane;
            play.insert(play.end(), wings.begin(), wings.end());
            result.push_back(play);
        }
    }
    
    return result;
}

vector<vector<Card>> AIPlayer::findFourWithTwos() {
    vector<vector<Card>> result;
    map<int, int> count = getCardCount();
    vector<vector<Card>> pairs = findPairs();
    
    for (const auto& p : count) {
        if (p.second == 4) {
            vector<Card> four;
            for (const auto& c : hand) {
                if (c.value == p.first) {
                    four.push_back(c);
                }
            }
            
            vector<Card> twoPairs;
            for (const auto& pair : pairs) {
                if (pair[0].value != p.first) {
                    twoPairs.insert(twoPairs.end(), pair.begin(), pair.end());
                    if (twoPairs.size() >= 4) break;
                }
            }
            
            if (twoPairs.size() >= 4) {
                vector<Card> play = four;
                play.insert(play.end(), twoPairs.begin(), twoPairs.begin() + 4);
                result.push_back(play);
            }
            
            twoPairs.clear();
            for (const auto& c : hand) {
                if (c.value != p.first) {
                    twoPairs.push_back(c);
                    if (twoPairs.size() >= 2) break;
                }
            }
            
            if (twoPairs.size() >= 2) {
                vector<Card> play = four;
                play.insert(play.end(), twoPairs.begin(), twoPairs.begin() + 2);
                result.push_back(play);
            }
        }
    }
    
    return result;
}

vector<vector<Card>> AIPlayer::findBombs() {
    vector<vector<Card>> result;
    map<int, int> count = getCardCount();
    
    for (const auto& p : count) {
        if (p.second == 4) {
            vector<Card> bomb;
            for (const auto& c : hand) {
                if (c.value == p.first) {
                    bomb.push_back(c);
                }
            }
            result.push_back(bomb);
        }
    }
    return result;
}

vector<Card> AIPlayer::findKingBomb() {
    vector<Card> result;
    bool hasSmallJoker = false;
    bool hasBigJoker = false;
    
    for (const auto& c : hand) {
        if (c.value == 16) hasSmallJoker = true;
        if (c.value == 17) hasBigJoker = true;
    }
    
    if (hasSmallJoker && hasBigJoker) {
        for (const auto& c : hand) {
            if (c.value == 16 || c.value == 17) {
                result.push_back(c);
            }
        }
    }
    
    return result;
}

vector<vector<Card>> AIPlayer::findGreaterSingles(const Card& card) {
    vector<vector<Card>> result;
    for (const auto& c : hand) {
        if (c.value > card.value) {
            result.push_back({c});
        }
    }
    return result;
}

vector<vector<Card>> AIPlayer::findGreaterPairs(const Card& card) {
    vector<vector<Card>> result;
    map<int, int> count = getCardCount();
    
    for (const auto& p : count) {
        if (p.second >= 2 && p.first > card.value) {
            vector<Card> pair;
            int found = 0;
            for (const auto& c : hand) {
                if (c.value == p.first && found < 2) {
                    pair.push_back(c);
                    found++;
                }
            }
            result.push_back(pair);
        }
    }
    return result;
}

vector<vector<Card>> AIPlayer::findGreaterThrees(const Card& card) {
    vector<vector<Card>> result;
    map<int, int> count = getCardCount();
    
    for (const auto& p : count) {
        if (p.second >= 3 && p.first > card.value && p.first < 13) {
            vector<Card> three;
            int found = 0;
            for (const auto& c : hand) {
                if (c.value == p.first && found < 3) {
                    three.push_back(c);
                    found++;
                }
            }
            result.push_back(three);
        }
    }
    return result;
}

vector<vector<Card>> AIPlayer::findGreaterThreeWithOnes(const vector<Card>& cards) {
    vector<vector<Card>> result;
    int threeValue = Game::getKeyValue(cards, THREE_WITH_ONE);
    if (threeValue == -1) return result;
    
    vector<vector<Card>> threes = findGreaterThrees(Card{threeValue, ""});
    
    for (const auto& three : threes) {
        for (const auto& c : hand) {
            if (c.value != three[0].value) {
                vector<Card> play = three;
                play.push_back(c);
                result.push_back(play);
                break;
            }
        }
    }
    
    return result;
}

vector<vector<Card>> AIPlayer::findGreaterThreeWithTwos(const vector<Card>& cards) {
    vector<vector<Card>> result;
    int threeValue = Game::getKeyValue(cards, THREE_WITH_TWO);
    if (threeValue == -1) return result;
    
    vector<vector<Card>> threes = findGreaterThrees(Card{threeValue, ""});
    vector<vector<Card>> pairs = findPairs();
    
    for (const auto& three : threes) {
        for (const auto& pair : pairs) {
            if (pair[0].value != three[0].value) {
                vector<Card> play = three;
                play.insert(play.end(), pair.begin(), pair.end());
                result.push_back(play);
                break;
            }
        }
    }
    
    return result;
}

vector<vector<Card>> AIPlayer::findGreaterStraights(const vector<Card>& cards) {
    vector<vector<Card>> result;
    int size = cards.size();
    int minValue = cards[0].value;
    
    vector<int> values = getSortedValues();
    
    for (int start = 0; start <= (int)values.size() - size; start++) {
        if (values[start] <= minValue) continue;
        if (values[start] >= 13) break;
        
        bool consecutive = true;
        for (int i = 0; i < size - 1; i++) {
            if (values[start + i + 1] != values[start + i] + 1) {
                consecutive = false;
                break;
            }
        }
        
        if (consecutive) {
            vector<Card> straight;
            for (int i = 0; i < size; i++) {
                for (const auto& c : hand) {
                    if (c.value == values[start + i]) {
                        straight.push_back(c);
                        break;
                    }
                }
            }
            result.push_back(straight);
            break;
        }
    }
    
    return result;
}

vector<vector<Card>> AIPlayer::findGreaterConsecutivePairs(const vector<Card>& cards) {
    vector<vector<Card>> result;
    int size = cards.size() / 2;
    int minValue = cards[0].value;
    
    map<int, int> count = getCardCount();
    vector<int> pairValues;
    
    for (const auto& p : count) {
        if (p.second >= 2 && p.first < 13) {
            pairValues.push_back(p.first);
        }
    }
    
    if ((int)pairValues.size() < size) return result;
    sort(pairValues.begin(), pairValues.end());
    
    for (int i = 0; i <= (int)pairValues.size() - size; i++) {
        if (pairValues[i] <= minValue) continue;
        
        bool consecutive = true;
        for (int j = 0; j < size - 1; j++) {
            if (pairValues[i + j + 1] != pairValues[i + j] + 1) {
                consecutive = false;
                break;
            }
        }
        
        if (consecutive) {
            vector<Card> play;
            for (int j = 0; j < size; j++) {
                int found = 0;
                for (const auto& c : hand) {
                    if (c.value == pairValues[i + j] && found < 2) {
                        play.push_back(c);
                        found++;
                    }
                }
            }
            result.push_back(play);
            break;
        }
    }
    
    return result;
}

vector<vector<Card>> AIPlayer::findGreaterAirplanes(const vector<Card>& cards) {
    vector<vector<Card>> result;
    int planeSize = cards.size() / 3;
    int minValue = cards[0].value;
    
    map<int, int> count = getCardCount();
    vector<int> threeValues;
    
    for (const auto& p : count) {
        if (p.second >= 3 && p.first < 13) {
            threeValues.push_back(p.first);
        }
    }
    
    if ((int)threeValues.size() < planeSize) return result;
    sort(threeValues.begin(), threeValues.end());
    
    for (int i = 0; i <= (int)threeValues.size() - planeSize; i++) {
        if (threeValues[i] <= minValue) continue;
        
        bool consecutive = true;
        for (int j = 0; j < planeSize - 1; j++) {
            if (threeValues[i + j + 1] != threeValues[i + j] + 1) {
                consecutive = false;
                break;
            }
        }
        
        if (consecutive) {
            vector<Card> play;
            for (int j = 0; j < planeSize; j++) {
                int found = 0;
                for (const auto& c : hand) {
                    if (c.value == threeValues[i + j] && found < 3) {
                        play.push_back(c);
                        found++;
                    }
                }
            }
            result.push_back(play);
            break;
        }
    }
    
    return result;
}

vector<vector<Card>> AIPlayer::findGreaterAirplanesWithWings(const vector<Card>& cards) {
    vector<vector<Card>> result;
    int planeSize = 0;
    int minValue = 0;
    
    map<int, int> count;
    for (const auto& c : cards) {
        count[c.value]++;
    }
    
    for (const auto& p : count) {
        if (p.second >= 3) {
            planeSize++;
            if (minValue == 0 || p.first < minValue) {
                minValue = p.first;
            }
        }
    }
    
    vector<vector<Card>> airplanes = findGreaterAirplanes(cards);
    
    for (const auto& airplane : airplanes) {
        set<int> airplaneValues;
        for (const auto& c : airplane) {
            airplaneValues.insert(c.value);
        }
        
        vector<Card> wings;
        for (const auto& c : hand) {
            if (airplaneValues.find(c.value) == airplaneValues.end()) {
                wings.push_back(c);
                if (wings.size() == planeSize) {
                    break;
                }
            }
        }
        
        if (wings.size() == planeSize) {
            vector<Card> play = airplane;
            play.insert(play.end(), wings.begin(), wings.end());
            result.push_back(play);
            break;
        }
        
        wings.clear();
        vector<vector<Card>> pairs = findPairs();
        for (const auto& pair : pairs) {
            if (airplaneValues.find(pair[0].value) == airplaneValues.end()) {
                wings.insert(wings.end(), pair.begin(), pair.end());
                if (wings.size() == planeSize * 2) {
                    break;
                }
            }
        }
        
        if (wings.size() == planeSize * 2) {
            vector<Card> play = airplane;
            play.insert(play.end(), wings.begin(), wings.end());
            result.push_back(play);
            break;
        }
    }
    
    return result;
}

vector<vector<Card>> AIPlayer::findGreaterFourWithTwos(const vector<Card>& cards) {
    vector<vector<Card>> result;
    int fourValue = Game::getKeyValue(cards, FOUR_WITH_TWO);
    if (fourValue == -1) return result;
    
    map<int, int> count = getCardCount();
    vector<vector<Card>> pairs = findPairs();
    
    bool withTwoPairs = false;
    map<int, int> cardCount;
    for (const auto& c : cards) {
        cardCount[c.value]++;
    }
    int pairCount = 0;
    for (const auto& p : cardCount) {
        if (p.second == 2) pairCount++;
    }
    if (pairCount == 2) withTwoPairs = true;
    
    for (const auto& p : count) {
        if (p.second == 4 && p.first > fourValue) {
            vector<Card> four;
            for (const auto& c : hand) {
                if (c.value == p.first) {
                    four.push_back(c);
                }
            }
            
            if (withTwoPairs) {
                vector<Card> twoPairs;
                for (const auto& pair : pairs) {
                    if (pair[0].value != p.first) {
                        twoPairs.insert(twoPairs.end(), pair.begin(), pair.end());
                        if (twoPairs.size() >= 4) break;
                    }
                }
                
                if (twoPairs.size() >= 4) {
                    vector<Card> play = four;
                    play.insert(play.end(), twoPairs.begin(), twoPairs.begin() + 4);
                    result.push_back(play);
                    break;
                }
            } else {
                vector<Card> singles;
                for (const auto& c : hand) {
                    if (c.value != p.first) {
                        singles.push_back(c);
                        if (singles.size() >= 2) break;
                    }
                }
                
                if (singles.size() >= 2) {
                    vector<Card> play = four;
                    play.insert(play.end(), singles.begin(), singles.begin() + 2);
                    result.push_back(play);
                    break;
                }
            }
        }
    }
    
    return result;
}

vector<vector<Card>> AIPlayer::findGreaterBombs(const Card& card) {
    vector<vector<Card>> result;
    vector<vector<Card>> bombs = findBombs();
    
    for (const auto& bomb : bombs) {
        if (bomb[0].value > card.value) {
            result.push_back(bomb);
        }
    }
    
    return result;
}

// Game 实现
Game::Game() : rng(static_cast<unsigned>(chrono::system_clock::now().time_since_epoch().count())) {
    players.push_back(new HumanPlayer("你"));
    players.push_back(new AIPlayer("电脑1"));
    players.push_back(new AIPlayer("电脑2"));
    initializeDeck();
}

Game::~Game() {
    for (auto player : players) {
        delete player;
    }
}

void Game::initializeDeck() {
    deck.clear();
    int value = 3;
    for (const auto& rank : RANKS) {
        for (int i = 0; i < 4; i++) {
            deck.push_back({value, rank});
        }
        value++;
    }
    deck.push_back({16, JOKERS[0]});
    deck.push_back({17, JOKERS[1]});
    shuffleDeck();
}

void Game::shuffleDeck() {
    shuffle(deck.begin(), deck.end(), rng);
}

void Game::dealCards() {
    shuffleDeck();
    
    for (auto player : players) {
        player->hand.clear();
        player->isLandlord = false;
    }
    landlordCards.clear();
    
    for (int i = 0; i < 51; i++) {
        players[i % 3]->hand.push_back(deck[i]);
    }
    
    for (int i = 51; i < 54; i++) {
        landlordCards.push_back(deck[i]);
    }
    
    for (auto player : players) {
        player->sortHand();
    }
}

void Game::selectLandlord() {
    cout << "\n========================================" << endl;
    cout << "开始叫地主..." << endl;
    
    int landlordIndex = -1;
    int current = 0;
    int maxBid = 0;
    int roundCount = 0;
    
    while (roundCount < 3 && maxBid < 3) {
        cout << "\n当前玩家: " << players[current]->name << endl;
        players[current]->showHand();
        
        int bid = players[current]->bidForLandlord(maxBid);
        
        if (bid > maxBid) {
            landlordIndex = current;
            maxBid = bid;
            cout << players[current]->name << "叫 " << bid << " 分!" << endl;
        } else {
            cout << players[current]->name << "不叫。" << endl;
        }
        
        if (maxBid == 3) {
            cout << players[current]->name << "叫了3分,直接成为地主!" << endl;
            break;
        }
        
        current = (current + 1) % 3;
        roundCount++;
    }
    
    if (landlordIndex == -1) {
        cout << "\n没有人叫地主,重新发牌!" << endl;
        dealCards();
        selectLandlord();
        return;
    }
    
    cout << "\n" << players[landlordIndex]->name << " 以 " << maxBid << " 分成为地主!" << endl;
    
    players[landlordIndex]->isLandlord = true;
    
    for (const auto& card : landlordCards) {
        players[landlordIndex]->hand.push_back(card);
    }
    players[landlordIndex]->sortHand();
    
    cout << "地主牌是: ";
    for (const auto& card : landlordCards) {
        cout << card.name << " ";
    }
    cout << endl;
    
    currentPlayer = landlordIndex;
    cout << "地主先出牌!" << endl;
}

void Game::playGame() {
    vector<Card> lastPlay;
    int passCount = 0;
    int roundCount = 0;
    
    while (true) {
        roundCount++;
        Player* current = players[currentPlayer];
        cout << "\n========================================" << endl;
        cout << "第 " << roundCount << " 轮" << endl;
        cout << "当前玩家: " << current->name << endl;
        
        vector<Card> playedCards = current->play(lastPlay, players);
        
        if (playedCards.empty()) {
            passCount++;
            if (passCount >= 2) {
                lastPlay.clear();
                passCount = 0;
                cout << "两家都PASS,重新开始新的一轮。" << endl;
            }
        } else {
            lastPlay = playedCards;
            passCount = 0;
            
            if (!current->hasCards()) {
                announceWinner(current);
                return;
            }
        }
        
        currentPlayer = (currentPlayer + 1) % 3;
    }
}

void Game::announceWinner(Player* winner) {
    cout << "\n========================================" << endl;
    cout << "游戏结束!" << endl;
    cout << "胜利者: " << winner->name << endl;
    if (winner->isLandlord) {
        cout << "地主获胜!" << endl;
    } else {
        cout << "农民获胜!" << endl;
    }
    
    cout << "\n最终手牌:" << endl;
    for (const auto player : players) {
        player->showHand();
    }
}

void Game::start() {
    cout << "========================================" << endl;
    cout << "    欢迎来到斗地主游戏!" << endl;
    cout << "========================================" << endl;
    cout << "牌型说明: 单张、对子、三张、三带一、三带二、" << endl;
    cout << "          顺子(5张及以上,不含2和王)、" << endl;
    cout << "          连对(3对及以上,不含2和王)、" << endl;
    cout << "          飞机(2组及以上连续三张,不含2)、" << endl;
    cout << "          飞机带翅膀(翅膀可以带2和王)、" << endl;
    cout << "          四带二(可以带2和王)、炸弹、王炸" << endl;
    cout << "========================================" << endl;
    cout << "注意:2和王不能出现在顺子、连对、飞机主体中!" << endl;
    cout << "========================================" << endl;
    
    while (true) {
        dealCards();
        selectLandlord();
        playGame();
        
        char choice;
        cout << "\n再来一局?(y/n): ";
        cin >> choice;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        if (choice != 'y' && choice != 'Y') {
            break;
        }
    }
    
    cout << "游戏结束,谢谢参与!" << endl;
}

// ==================== 核心牌型判断(最终修复版) ====================
CardType Game::getCardType(const vector<Card>& cards) {
    size_t size = cards.size();
    
    if (size == 0) return INVALID;
    if (size == 1) return SINGLE;
    
    vector<Card> sorted = cards;
    sort(sorted.begin(), sorted.end());
    
    // ===== 2张 =====
    if (size == 2) {
        if (sorted[0].value == sorted[1].value) return PAIR;
        if ((sorted[0].value == 16 && sorted[1].value == 17) ||
            (sorted[0].value == 17 && sorted[1].value == 16)) return KING_BOMB;
        return INVALID;
    }
    
    // ===== 3张 =====
    if (size == 3) {
        if (sorted[0].value == sorted[1].value && 
            sorted[1].value == sorted[2].value) return THREE;
        return INVALID;
    }
    
    // ===== 4张 =====
    if (size == 4) {
        if (sorted[0].value == sorted[1].value && 
            sorted[1].value == sorted[2].value && 
            sorted[2].value == sorted[3].value) return BOMB;
        
        map<int, int> count;
        for (const auto& card : sorted) {
            count[card.value]++;
        }
        
        bool hasThree = false;
        bool hasOne = false;
        for (const auto& p : count) {
            if (p.second == 3) hasThree = true;
            if (p.second == 1) hasOne = true;
        }
        
        if (hasThree && hasOne) return THREE_WITH_ONE;
        return INVALID;
    }
    
    // ===== 统计牌值出现次数 =====
    map<int, int> count;
    for (const auto& card : sorted) {
        count[card.value]++;
    }
    
    // ===== 三带二 (5张) =====
    if (size == 5) {
        bool hasThree = false, hasTwo = false;
        for (const auto& p : count) {
            if (p.second == 3) hasThree = true;
            if (p.second == 2) hasTwo = true;
        }
        if (hasThree && hasTwo) {
            return THREE_WITH_TWO;
        }
    }
    
    // ===== 顺子 (5张及以上,不含2和王) =====
    if (size >= 5) {
        bool isStraight = true;
        for (const auto& p : count) {
            if (p.second != 1) { 
                isStraight = false; 
                break; 
            }
            if (p.first == 15 || p.first == 16 || p.first == 17) {
                return INVALID;
            }
        }
        
        if (isStraight) {
            int minValue = sorted[0].value;
            for (size_t i = 0; i < size; i++) {
                if (sorted[i].value != minValue + (int)i) {
                    return INVALID;
                }
            }
            return STRAIGHT;
        }
    }
    
    // ===== 连对 (3对及以上,不含2和王) =====
    if (size >= 6 && size % 2 == 0) {
        bool isConsecutivePairs = true;
        vector<int> pairValues;
        
        for (const auto& p : count) {
            if (p.second != 2) { 
                isConsecutivePairs = false; 
                break; 
            }
            if (p.first == 15 || p.first == 16 || p.first == 17) {
                return INVALID;
            }
            pairValues.push_back(p.first);
        }
        
        if (isConsecutivePairs && (int)pairValues.size() >= 3) {
            sort(pairValues.begin(), pairValues.end());
            for (int i = 0; i < (int)pairValues.size() - 1; i++) {
                if (pairValues[i + 1] != pairValues[i] + 1) {
                    return INVALID;
                }
            }
            return CONSECUTIVE_PAIRS;
        }
    }
    
    // ===== 四带两对 (8张) - 可以带2和王 =====
    if (size == 8) {
        bool hasFour = false;
        int pairCount = 0;
        bool hasSingle = false;
        
        for (const auto& p : count) {
            if (p.second == 4) {
                hasFour = true;
            } else if (p.second == 2) {
                pairCount++;
            } else if (p.second == 1) {
                hasSingle = true;
            } else {
                return INVALID;
            }
        }
        
        if (hasFour && pairCount == 2 && !hasSingle) {
            return FOUR_WITH_TWO;
        }
        
        if (hasFour && hasSingle && pairCount == 0) {
            int singleCount = 0;
            for (const auto& p : count) {
                if (p.second == 1) singleCount++;
            }
            if (singleCount == 2) {
                return FOUR_WITH_TWO;
            }
        }
        
        return INVALID;
    }
    
    // ===== 飞机 (2组及以上连续三张,不含2) =====
    if (size >= 6 && size % 3 == 0) {
        bool isAirplane = true;
        vector<int> threeValues;
        for (const auto& p : count) {
            if (p.second != 3) { isAirplane = false; break; }
            if (p.first == 15) {
                return INVALID;
            }
            threeValues.push_back(p.first);
        }
        if (isAirplane && (int)threeValues.size() >= 2) {
            sort(threeValues.begin(), threeValues.end());
            for (int i = 0; i < (int)threeValues.size() - 1; i++) {
                if (threeValues[i + 1] != threeValues[i] + 1) {
                    return INVALID;
                }
            }
            return AIRPLANE;
        }
    }
    
    // ===== 飞机带翅膀 (翅膀可以带2和王) =====
    if (size >= 8) {
        int threeCount = 0;
        int wingTotal = 0;
        vector<int> threeValues;
        bool valid = true;
        
        for (const auto& p : count) {
            if (p.second == 3) {
                threeCount++;
                threeValues.push_back(p.first);
                // 飞机主体不能有2
                if (p.first == 15) {
                    return INVALID;
                }
            } else if (p.second == 1 || p.second == 2) {
                wingTotal += p.second;
                // 翅膀可以是任何牌,包括2和王
            } else {
                valid = false;
                break;
            }
        }
        
        if (valid && threeCount >= 2) {
            sort(threeValues.begin(), threeValues.end());
            bool consecutive = true;
            for (int i = 0; i < threeCount - 1; i++) {
                if (threeValues[i + 1] != threeValues[i] + 1) {
                    consecutive = false;
                    break;
                }
            }
            if (consecutive && (wingTotal == threeCount || wingTotal == threeCount * 2)) {
                return AIRPLANE_WITH_WINGS;
            }
        }
    }
    
    return INVALID;
}

int Game::getKeyValue(const vector<Card>& cards, CardType type) {
    map<int, int> count;
    for (const auto& card : cards) {
        count[card.value]++;
    }
    
    if (type == THREE_WITH_ONE || type == THREE_WITH_TWO) {
        for (const auto& p : count) {
            if (p.second == 3) return p.first;
        }
    } else if (type == FOUR_WITH_TWO) {
        for (const auto& p : count) {
            if (p.second == 4) return p.first;
        }
    } else if (type == AIRPLANE || type == AIRPLANE_WITH_WINGS) {
        int maxThree = -1;
        for (const auto& p : count) {
            if (p.second >= 3 && p.first > maxThree && p.first < 13) {
                maxThree = p.first;
            }
        }
        return maxThree;
    }
    
    return -1;
}

bool Game::isConsecutive(const vector<int>& values) {
    if (values.size() < 2) return false;
    for (size_t i = 0; i < values.size() - 1; i++) {
        if (values[i + 1] != values[i] + 1) {
            return false;
        }
    }
    return true;
}

bool Game::canBeat(const vector<Card>& lastPlay, CardType lastType, 
                   const vector<Card>& currentPlay, CardType currentType) {
    if (currentType == INVALID) return false;
    if (currentType == KING_BOMB) return true;
    
    if (lastType != BOMB && lastType != KING_BOMB && currentType == BOMB) return true;
    
    if (lastType != currentType || lastPlay.size() != currentPlay.size()) {
        return false;
    }
    
    int lastValue, currentValue;
    
    switch (lastType) {
        case SINGLE:
        case PAIR:
        case THREE:
        case BOMB:
            lastValue = lastPlay[0].value;
            currentValue = currentPlay[0].value;
            break;
            
        case THREE_WITH_ONE:
        case THREE_WITH_TWO:
        case FOUR_WITH_TWO:
            lastValue = getKeyValue(lastPlay, lastType);
            currentValue = getKeyValue(currentPlay, currentType);
            if (lastValue == -1 || currentValue == -1) return false;
            break;
            
        case STRAIGHT:
        case CONSECUTIVE_PAIRS:
        case AIRPLANE:
            lastValue = lastPlay[0].value;
            currentValue = currentPlay[0].value;
            break;
            
        case AIRPLANE_WITH_WINGS:
            lastValue = getKeyValue(lastPlay, lastType);
            currentValue = getKeyValue(currentPlay, currentType);
            if (lastValue == -1 || currentValue == -1) return false;
            break;
            
        default:
            return false;
    }
    
    return currentValue > lastValue;
}

int main() {
    Game game;
    game.start();
    return 0;
}
原文