我需要一个功能来洗牌我创建的卡片和一个 环 在main中打印洗牌值。我不想改变 什么 否则。该功能必须采用52张牌和牌组大小 参数。对此有何帮助?我的问题是我不知道如何编写shuffle函数并将其打印出来。
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
class Card
{
private:
string suit;
string value;
public:
Card() = default;
Card(string a, string b)
{
value = a;
suit = b;
}
string getSuit()
{
return suit;
}
string getValue()
{
return value;
}
void print()
{
cout << getValue() << " of " << getSuit() << endl;
}
void shuffle(Card cardlist[52], int size)
{
}
};
int main()
{
Card cardlist[52];
cardlist[0] = Card("2","Spades");
cardlist[1] = Card("3","Spades");
cardlist[2] = Card("4","Spades");
cardlist[3] = Card("5","Spades");
cardlist[4] = Card("6","Spades");
cardlist[5] = Card("7","Spades");
cardlist[6] = Card("8","Spades");
cardlist[7] = Card("9","Spades");
cardlist[8] = Card("10","Spades");
cardlist[9] = Card("Jack","Spades");
cardlist[10] = Card("Queen","Spades");
cardlist[11] = Card("King","Spades");
cardlist[12] = Card("Ace","Spades");
cardlist[13] = Card("2","Hearts");
cardlist[14] = Card("3","Hearts");
cardlist[15] = Card("4","Hearts");
cardlist[16] = Card("5","Hearts");
cardlist[17] = Card("6","Hearts");
cardlist[18] = Card("7","Hearts");
cardlist[19] = Card("8","Hearts");
cardlist[20] = Card("9","Hearts");
cardlist[21] = Card("10","Hearts");
cardlist[22] = Card("Jack","Hearts");
cardlist[23] = Card("Queen","Hearts");
cardlist[24] = Card("King","Hearts");
cardlist[25] = Card("Ace","Hearts");
cardlist[26] = Card("2","Diamonds");
cardlist[27] = Card("3","Diamonds");
cardlist[28] = Card("4","Diamonds");
cardlist[29] = Card("5","Diamonds");
cardlist[30] = Card("6","Diamonds");
cardlist[31] = Card("7","Diamonds");
cardlist[32] = Card("8","Diamonds");
cardlist[33] = Card("9","Diamonds");
cardlist[34] = Card("10","Diamonds");
cardlist[35] = Card("Jack","Diamonds");
cardlist[36] = Card("Queen","Diamonds");
cardlist[37] = Card("King","Diamonds");
cardlist[38] = Card("Ace","Diamonds");
cardlist[39] = Card("2","Clubs");
cardlist[40] = Card("3","Clubs");
cardlist[41] = Card("4","Clubs");
cardlist[42] = Card("5","Clubs");
cardlist[43] = Card("6","Clubs");
cardlist[44] = Card("7","Clubs");
cardlist[45] = Card("8","Clubs");
cardlist[46] = Card("9","Clubs");
cardlist[47] = Card("10","Clubs");
cardlist[48] = Card("Jack","Clubs");
cardlist[49] = Card("Queen","Clubs");
cardlist[50] = Card("King","Clubs");
cardlist[51] = Card("Ace","Clubs");
for (int i = 0; i < 52; i++)
cardlist[i].print();
return 0;
}
答案 0 :(得分:3)
除非它是一个单行班,否则我通常不会帮助做作业。你好运,虽然它是真的! (但是你的教授不太可能在没有解释的情况下接受这个,所以你需要花一点时间来理解为什么这个有效。)
要重新排列我们将要使用的卡片shuffle
:
shuffle(begin(cardlist), end(cardlist), mt19937(random_device()()))
我还应该在这里指出你的shuffle
课程中有一个Card
方法。这是没有意义的。方法是对一个对象进行操作。每个Card
都是一张独特的扑克牌,其属性可以通过它的方法进行操作。你不会洗牌黑桃王牌。你洗牌一副牌。因此,如果您要创建一个包含52个Deck
对象的Card
类,那么制作Deck::shuffle
方法是有意义的,因为事实上您实际上是在洗牌
答案 1 :(得分:-1)
您可以使用rand函数随机生成两个数字,然后只交换具有这两个数字作为索引的卡片。
添加For循环可以让你随机抽样。
void shuffle(Card cardlist[52], int size)
{
int v2;
int v1;
int loop=rand() % 100;
for(int i=0;i<loop;i++)
{ v1 = rand() % size;
v2 = rand() % size;
Card temp=cardlist[v1];
cardlist[v1]=cardlist[v2];
cardlist[v2]=temp;
}
请注意,这是假设您需要将shuffle功能作为类Card
的成员最好将其作为班级卡的朋友功能。