//Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and 
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
// same ids are connected.
// prefix 的格式要求是 acm-[一级标题编号].[二级标题编号][该代码片段的英文简称]
// description 的格式要求是 "插入 [该代码片段的中文名]"

{
    "扑克牌模板": {
        "prefix": "acm-1.5poker",
        "body": [
            "",
            "struct Card",
            "{",
            "    int suit, rank; // 花色 0..3, 点数 2..14",
            "",
            "    Card() : Card(0, 0) {}",
            "    Card(char suit_ch, char rank_ch) : Card(GetSuit(suit_ch), GetRank(rank_ch)) {}",
            "    Card(int suit, int rank) : suit(suit), rank(rank) {}",
            "",
            "    static int GetSuit(char ch)",
            "    {",
            "        if (ch == 'C') return 0;",
            "        if (ch == 'D') return 1;",
            "        if (ch == 'H') return 2;",
            "        return 3; // 'S'",
            "    }",
            "    static int GetRank(char ch)",
            "    {",
            "        if (ch == 'A') return 14;",
            "        if (ch == 'K') return 13;",
            "        if (ch == 'Q') return 12;",
            "        if (ch == 'J') return 11;",
            "        if (ch == 'T') return 10;",
            "        return ch - '0';",
            "    }",
            "",
            "    bool operator < (const Card &rhs) const { return rank < rhs.rank; }",
            "    bool operator == (const Card &rhs) const { return rank == rhs.rank && suit == rhs.suit; }",
            "};",
            "",
            "",
            "struct CombEval",
            "{",
            "    enum class Type : int",
            "    {",
            "        Unspecified = 0,",
            "        HighCard,      // 高牌",
            "        Pair,          // 一对",
            "        TwoPairs,      // 两对",
            "        Three,         // 三条",
            "        Straight,      // 顺子",
            "        Flush,         // 同花",
            "        FullHouse,     // 葫芦",
            "        Four,          // 四条",
            "        StraightFlush, // 同花顺",
            "        RoyalFlush,    // 皇家同花顺",
            "    };",
            "",
            "    CombEval() : CombEval(Type::Unspecified, {}) {}",
            "    CombEval(Type type, const vector<Card> &cards) : type(type), cards(cards) {}",
            "",
            "    ",
            "",
            "    // 高牌 — 任何牌都至少是高牌",
            "    bool TryHighCard() { return true; }",
            "",
            "    // 一对 — 找出唯一一对，将其余牌按点数降序排列到前面",
            "    bool TryPair()",
            "    {",
            "        int id = -1;",
            "        for (int i = 0; id == -1 && i <= 3; i++)",
            "            if (cards[i].rank == cards[i + 1].rank)",
            "                id = i;",
            "        if (id == -1) return false;",
            "",
            "        int main_rank = cards[id].rank;",
            "        // 将对子移到末尾，其余牌按降序前置",
            "        for (int i = 0, cnt = 0; i <= 4; i++)",
            "            if (cards[i].rank != main_rank)",
            "            {",
            "                for (int j = i; j > cnt; j--)",
            "                    swap(cards[j], cards[j - 1]);",
            "                cnt++;",
            "            }",
            "        return true;",
            "    }",
            "",
            "    // 两对 — 找出两对，将非对子牌移到最前",
            "    bool TryTwoPairs()",
            "    {",
            "        int id1 = -1, id2 = -1;",
            "        for (int i = 0; i <= 3; i++)",
            "            if (cards[i].rank == cards[i + 1].rank)",
            "            {",
            "                if (id1 == -1) id1 = i;",
            "                else id2 = i;",
            "                i++;",
            "            }",
            "        if (id1 == -1 || id2 == -1) return false;",
            "",
            "        // 将单牌移到最前，便于比较",
            "        for (int j = 8 - id1 * 2 - id2 * 2; j; j--)",
            "            swap(cards[j], cards[j - 1]);",
            "        return true;",
            "    }",
            "",
            "    // 三条 — 找出唯一三条，将其余牌按降序前置",
            "    bool TryThree()",
            "    {",
            "        int id = -1;",
            "        for (int i = 0; id == -1 && i <= 2; i++)",
            "            if (cards[i].rank == cards[i + 1].rank && cards[i].rank == cards[i + 2].rank)",
            "                id = i;",
            "        if (id == -1) return false;",
            "",
            "        int main_rank = cards[id].rank;",
            "        for (int i = 0, cnt = 0; i <= 4; i++)",
            "            if (cards[i].rank != main_rank)",
            "            {",
            "                for (int j = i; j > cnt; j--)",
            "                    swap(cards[j], cards[j - 1]);",
            "                cnt++;",
            "            }",
            "        return true;",
            "    }",
            "",
            "    // 顺子 — 判断是否连续；处理 A-2-3-4-5 特殊顺",
            "    bool TryStraight()",
            "    {",
            "        for (int i = 1; i <= 3; i++)",
            "            if (cards[i].rank != cards[i - 1].rank + 1)",
            "                return false;",
            "        if (cards[4].rank == cards[3].rank + 1)",
            "            return true;",
            "        // A-2-3-4-5 特殊顺：cards = {2,3,4,5,14}",
            "        if (cards[4].rank == 14 && cards[0].rank == 2)",
            "        {",
            "            // 将 A 移到末尾 ⇒ {2,3,4,5,14} → {14,2,3,4,5}，便于比较",
            "            for (int i = 4; i; i--)",
            "                swap(cards[i], cards[i - 1]);",
            "            return true;",
            "        }",
            "        return false;",
            "    }",
            "",
            "    // 同花 — 所有牌花色相同",
            "    bool TryFlush()",
            "    {",
            "        for (const Card &card : cards)",
            "            if (card.suit != cards[0].suit)",
            "                return false;",
            "        return true;",
            "    }",
            "",
            "    // 葫芦 — 三条 + 一对",
            "    bool TryFullHouse()",
            "    {",
            "        if (!TryThree()) return false;",
            "        return cards[0].rank == cards[1].rank;",
            "    }",
            "",
            "    // 四条 — 四张相同点数",
            "    bool TryFour()",
            "    {",
            "        if (!TryThree()) return false;",
            "        return cards[1].rank == cards[2].rank;",
            "    }",
            "",
            "    // 同花顺",
            "    bool TryStraightFlush() { return TryFlush() && TryStraight(); }",
            "",
            "    // 皇家同花顺 — 同花顺且最小牌为 10（10,J,Q,K,A）",
            "    bool TryRoyalFlush()",
            "    {",
            "        if (!TryStraightFlush()) return false;",
            "        return cards[0].rank == 10;",
            "    }",
            "",
            "    ",
            "    void Sort()",
            "    {",
            "        sort(cards.begin(), cards.end());",
            "    }",
            "",
            "    ",
            "    bool operator < (const CombEval &rhs) const",
            "    {",
            "        if (type != rhs.type)",
            "            return int(type) < int(rhs.type);",
            "        // 从最大牌开始比较",
            "        for (int i = 4; i >= 0; i--)",
            "            if ((cards[i] < rhs.cards[i]) || (rhs.cards[i] < cards[i]))",
            "                return cards[i] < rhs.cards[i];",
            "        return false;",
            "    }",
            "    bool operator == (const CombEval &rhs) const",
            "    {",
            "        return !(*this < rhs) && !(rhs < *this);",
            "    }",
            "",
            "    Type type;",
            "    vector<Card> cards;",
            "};",
            "",
            "",
            "CombEval GetCombEval(const vector<Card> &cards)",
            "{",
            "    CombEval ret;",
            "    ret.cards = cards;",
            "    ret.Sort();",
            "",
            "    bool ok = false;",
            "    if (!ok && ret.TryRoyalFlush())    ret.type = CombEval::Type::RoyalFlush,    ok = true;",
            "    ret.Sort();",
            "    if (!ok && ret.TryStraightFlush()) ret.type = CombEval::Type::StraightFlush, ok = true;",
            "    if (!ok && ret.TryFour())          ret.type = CombEval::Type::Four,          ok = true;",
            "    if (!ok && ret.TryFullHouse())     ret.type = CombEval::Type::FullHouse,     ok = true;",
            "    if (!ok && ret.TryFlush())         ret.type = CombEval::Type::Flush,         ok = true;",
            "    if (!ok && ret.TryStraight())      ret.type = CombEval::Type::Straight,      ok = true;",
            "    if (!ok && ret.TryThree())         ret.type = CombEval::Type::Three,         ok = true;",
            "    if (!ok && ret.TryTwoPairs())      ret.type = CombEval::Type::TwoPairs,      ok = true;",
            "    if (!ok && ret.TryPair())          ret.type = CombEval::Type::Pair,          ok = true;",
            "    if (!ok && ret.TryHighCard())      ret.type = CombEval::Type::HighCard,      ok = true;",
            "    return ret;",
            "}"
        ],
        "description": "插入 扑克牌模板"
    }
}