2301_78364727 2023-10-26 21:10 采纳率: 90.9%
浏览 14
已结题

C++关于数据结构的问题

帮忙把下面使用的STL操作改为自己定义的(因为不能使用STL)


#include <iostream>#include <string>#include <vector>#include <random>enum class Suit {    SPADE,    HEART,    CLUB,    DIAMOND};enum class Rank {    ACE,    TWO,    THREE,    FOUR,    FIVE,    SIX,    SEVEN,    EIGHT,    NINE,    TEN,    JACK,    QUEEN,    KING};class Card {public:    Card(Suit suit, Rank rank) : suit(suit), rank(rank) {}    Suit getSuit() const {        return suit;    }    Rank getRank() const {        return rank;    }    friend std::ostream& operator<<(std::ostream& os, const Card& card) {        std::string suitStr;        std::string rankStr;        switch (static_cast<int>(card.suit)) {            case 0:                suitStr = "♠";                break;            case 1:                suitStr = "♥";                break;            case 2:                suitStr = "♣";                break;            case 3:                suitStr = "♦";                break;        }        switch (static_cast<int>(card.rank)) {            case 0:                rankStr = "A";                break;            case 1:                rankStr = "2";                break;            case 2:                rankStr = "3";                break;            case 3:                rankStr = "4";                break;            case 4:                rankStr = "5";                break;            case 5:                rankStr = "6";                break;            case 6:                rankStr = "7";                break;            case 7:                rankStr = "8";                break;            case 8:                rankStr = "9";                break;            case 9:                rankStr = "10";                break;            case 10:                rankStr = "J";                break;            case 11:                rankStr = "Q";                break;            case 12:                rankStr = "K";                break;        }        os << suitStr << rankStr;        return os;    }private:    Suit suit;    Rank rank;};template<typename T>class List {public:    void add(const T& item) {        items.push_back(item);    }    T get(int index) const {        return items[index];    }    int getSize() const {        return items.size();    }private:    std::vector<T> items;};template<typename T>class Player {public:    void addCard(const T& card) {        hand.add(card);    }    void showHand() const {        for (int i = 0; i < hand.getSize(); i++) {            std::cout << hand.get(i) << " ";        }        std::cout << std::endl;    }    void playCard(int index, List<T>& pile) {        T card = hand.get(index);        pile.add(card);        hand.remove(index);    }private:    List<T> hand;};void playCardGame() {    List<Card> deck;  // 扑克牌列表    // 生成扑克牌    for (int suit = static_cast<int>(Suit::SPADE); suit <= static_cast<int>(Suit::DIAMOND); suit++) {        for (int rank = static_cast<int>(Rank::ACE); rank <= static_cast<int>(Rank::KING); rank++) {            Card card(static_cast<Suit>(suit), static_cast<Rank>(rank));            deck.add(card);        }    }    // 打乱扑克牌顺序    std::random_device rd;    std::mt19937 gen(rd());    std::shuffle(deck.begin(), deck.end(), gen);    // 展示扑克牌    std::cout << "展示扑克牌:" << std::endl;    for (int i = 0; i < deck.getSize(); i++) {        std::cout << deck.get(i) << " ";    }    std::cout << std::endl;    // 发牌给三位玩家    Player<Card> player1, player2, player3;    for (int i = 0; i < deck.getSize(); i++) {        if (i % 3 == 0) {            player1.addCard(deck.get(i));        } else if (i % 3 == 1) {            player2.addCard(deck.get(i));        } else {            player3.addCard(deck.get(i));        }    }    // 展示玩家手中的扑克牌    std::cout << "玩家1手中的扑克牌:";    player1.showHand();    std::cout << "玩家2手中的扑克牌:";    player2.showHand();    std::cout << "玩家3手中的扑克牌:";    player3.showHand();    // 玩家1出牌    List<Card> pile;  // 牌堆    std::cout << "玩家1出牌:" << std::endl;    player1.playCard(0, pile);    // 展示玩家手中的扑克牌和牌堆中的扑克牌    std::cout << "玩家1手中的扑克牌:";    player1.showHand();    std::cout << "牌堆中的扑克牌:";    for (int i = 0; i < pile.getSize(); i++) {        std::cout << pile.get(i) << " ";    }    std::cout << std::endl;}int main() {    playCardGame();    return 0;}

img

  • 写回答

13条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2023-10-26 21:11
    关注
    #include <iostream>
    #include <string>
    #include <list>
    #include <algorithm>
    #include <random>
    
    using namespace std;
    
    // 牌的结构体
    struct Card {
        string suit;    // 花色
        string face;    // 牌面点数
    };
    
    // 随机数生成器
    random_device rd;
    mt19937 gen(rd());
    
    // 洗牌函数
    void shuffleDeck(list<Card>& deck) {
        vector<Card> v(deck.begin(), deck.end());
        shuffle(v.begin(), v.end(), gen);
        deck.assign(v.begin(), v.end());
    }
    
    int main() {
        list<Card> deck;          // 扑克牌列表
        list<Card> player1, player2, player3;  // 三位玩家手中的扑克牌
        list<Card> discardPile;   // 牌堆
    
        // 生成扑克牌
        const string suits[] = { "♠", "♥", "♣", "♦" };
        const string faces[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
        for (const string& suit : suits) {
            for (const string& face : faces) {
                Card card{ suit, face };
                deck.push_back(card);
            }
        }
        Card joker1{ "", "小王" };
        Card joker2{ "", "大王" };
        deck.push_back(joker1);
        deck.push_back(joker2);
    
        // 洗牌
        shuffleDeck(deck);
    
        // 用户选择展示扑克牌
        cout << "展示扑克牌: ";
        for (const Card& card : deck) {
            cout << card.suit << card.face << " ";
        }
        cout << endl;
    
        // 将扑克牌循环发放到三位玩家手中
        int currentPlayer = 1;
        int deckSize = deck.size();
        for (int i = 0; i < deckSize; i++) {
            if (currentPlayer == 1) {
                player1.push_back(deck.front());
                deck.pop_front();
                currentPlayer = 2;
            }
            else if (currentPlayer == 2) {
                player2.push_back(deck.front());
                deck.pop_front();
                currentPlayer = 3;
            }
            else {
                player3.push_back(deck.front());
                deck.pop_front();
                currentPlayer = 1;
            }
        }
    
        // 展示三位玩家手中的扑克牌
        cout << "玩家1的牌: ";
        for (const Card& card : player1) {
            cout << card.suit << card.face << " ";
        }
        cout << endl;
        cout << "玩家2的牌: ";
        for (const Card& card : player2) {
            cout << card.suit << card.face << " ";
        }
        cout << endl;
        cout << "玩家3的牌: ";
        for (const Card& card : player3) {
            cout << card.suit << card.face << " ";
        }
        cout << endl;
    
        // 用户选择出牌
        int currentPlayerIndex = 0;
        cout << "选择出牌的玩家 (1-3): ";
        cin >> currentPlayerIndex;
    
        if (currentPlayerIndex < 1 || currentPlayerIndex > 3) {
            cout << "无效的玩家编号!" << endl;
            return 0;
        }
    
        int cardIndex = 0;
        cout << "选择要出的牌 (1-" << player1.size() << "): ";
        cin >> cardIndex;
    
        if (cardIndex < 1 || cardIndex > player1.size()) {
            cout << "无效的牌索引!" << endl;
            return 0;
        }
    
        // 将出的牌添加到牌堆中,并从玩家手中删除
        auto playerIter = player1.begin();
        advance(playerIter, cardIndex - 1);
        Card playedCard = *playerIter;
        player1.erase(playerIter);
        discardPile.push_back(playedCard);
    
        // 输出出牌后的结果
        cout << "玩家1的牌: ";
        for (const Card& card : player1) {
            cout << card.suit << card.face << " ";
        }
        cout << endl;
        cout << "牌堆中的牌: ";
        for (const Card& card : discardPile) {
            cout << card.suit << card.face << " ";
        }
        cout << endl;
    
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(12条)

报告相同问题?

问题事件

  • 系统已结题 11月8日
  • 已采纳回答 10月31日
  • 修改了问题 10月26日
  • 创建了问题 10月26日

悬赏问题

  • ¥20 如何再GIS用海岸线建立缓冲区
  • ¥15 codeblock遇到问题了,求帮助😭
  • ¥15 Qt6.8.0加载网页MSVC2022
  • ¥15 360浏览器m2的这个值
  • ¥15 国内有哪些厂商做automlops的?
  • ¥15 skynet pb mysql
  • ¥15 笔记本外接显示器分辨率太低各种方法都用过了调不高
  • ¥15 Redstone R0697-F00 D2020 交换机 OS
  • ¥50 H5+js 动态数字画廊怎么做?
  • ¥20 外向内全景图像拼接相关项目和论文咨询