我的要求如下:
int Deal(int,CardSet&,CardSet&)将两只手放在传递的两个CardSet参数中。

每张牌中要放置的牌数是第一个参数。应当一次从当前的卡组中取出卡,并将卡交替放置。

例如。如果当前组持有2S, 3S, 4S, 5S, 6S, 7S(整数0到5)并且我们必须发3张牌,那么两只手将分别获得2S, 4S, 6S(整数0、2、4)和3S, 5S, 7S(1、3、5)。

两只手中可能已经有卡,并且其他卡将需要新的内存。创建新内存的次数不要超过所需的时间。

请记住,当前集合的大小也必须减小。如果当前集合中的卡不足以执行交易,请打印错误
消息并终止。

我不知道Deal()函数有什么错误。程序运行时没有任何错误,但是我得到的输出是错误的:请帮助我纠正错误(如果有)。

我在main()中定义的类对象是:

CardSet CardSet1(104), CardSet2(12), CardSet3, CardSet4, CardSet5, CardSet6;
cout << "Dealing 3 Cards from CardSet1 into CardSet3 and CardSet4: " <<endl;
CardSet1.Deal(3,CardSet3,CardSet4);
cout << "Printout of CardSet1:" << endl;
CardSet1.Print();
cout << "Printout of CardSet3:" << endl;
CardSet3.Print();
cout << "Printout of CardSet4:" << endl;
CardSet4.Print();
cout << endl;

这是类(class):
class CardSet
{
public:
    CardSet();
    CardSet(int);
    ~CardSet();
    int Size() const;
    bool IsEmpty() const;
    void Shuffle();
    int Deal();
    void Deal(int,CardSet&,CardSet&);
    void Deal(int,CardSet&,CardSet&,CardSet&,CardSet&);
    void AddCard(int);
    void MergeShuffle(CardSet&);
    void Print() const;
private:
    int* Card;
    int nCards;
    void PrintCard(int c) const;
};

这是我必须使用的功能:
CardSet::CardSet()
{
    Card = NULL;
    nCards =0;
}

CardSet::CardSet(int crd)
{
    int nCard;
    Card = new int[crd];
    nCards=crd;

    for(int i=0; i<nCards; i++)
    {
        Card[i]=i%52;
    }
}

CardSet::~CardSet()
{
    delete [] Card;
}

int CardSet::Size() const
{
    return nCards ;
}

void CardSet::Shuffle()
{
    int shuffle, tmp;
    for (int i=0; i<nCards-1; i++)
    {
        shuffle = rand() % 52;
        tmp = Card[i];
        Card[i]=Card[shuffle];
        Card[shuffle]= tmp;
    }
}

int CardSet::Deal()
{
    int a;
    a = Card[0];
    return a; // return Card[0]
    int *newSet = NULL;
    newSet = new int[nCards];

    if(nCards==0)
    {
        cerr << "The set is empty ." << endl;
        exit(1);
    }
    else
    {
        for(int i =0; i<nCards; i--)
        {
            newSet[i] = Card[i+1];
        }
        delete [] Card;
    }
}
void CardSet::Deal(int ncard, CardSet& crdHand1, CardSet& crdHand2 )
{
    int a;
    a =nCards/3;
    for(int i=0; i<a; i++)
    {
        for(int j=0; j<ncard; j++)
        {
            if(nCards==0)
            {
                cerr << "No more Cards. " <<endl;
                exit(1);
            }
            else if(i%2==0)
            {
                crdHand1 = Card[i];
            }
            else if(i%2==1)
            {
                crdHand2 = Card[i];
            }
        }
    }
}

这是输出:
当我实现crdHand1或crdHand2时。输出应该像
 for CardSet1:
 2S 4S 6S 8S XS
 QS AS 3C 5C 7C
 .....
 for CardSet4:
 3S 5S 7S 9S JS
 KS 2C 4C 6C 8C
 ......
and so on until cards are finished

c&#43;&#43; - 如何用C&#43;&#43;处理一副纸牌-LMLPHP

最佳答案

您可以将std::shuffle()用于<vector>来洗牌

private void setCardDeck() {
    for (int i = 0; i < Card.Rank.values().length; i++) {
        for (int j = 0; j < Card.Suit.values().length; j++) {
            cardDeck.push_back(new Card(Card.Rank.values()[i], Card.Suit.values()[j]));
        }
    }

    std::shuffle(cardDeck.begin(), cardDeck.end());
}

来自我在Java上的 flutter 克实现。 RankSuitenum

感谢Bob__的改进。

关于c++ - 如何用C++处理一副纸牌,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49809896/

10-12 14:51