【问题标题】:Simple way to shuffle deck of cards洗牌的简单方法
【发布时间】:2020-04-27 18:39:05
【问题描述】:

我正在寻找一种简单的方法来洗牌。我已经完成了套牌,有什么简单的方法来创建一个洗牌 52 张牌的函数?我不是在寻找复杂的洗牌方式,我更喜欢我可以轻松阅读的代码。我尝试将 rand() 放在 cout 部分,但它只是随机化了花色和面孔,我得到了错误的牌。

struct Card 
    {
        int value;
        char suit;
    };
Card cards[52];   //global variables

int main()
{
    int play;

    shuffleDeck();
    cout << endl<<endl;
    cout << "---------------------------" << endl;
    cout << "|Welcome to The Game Of 31| " << endl;
    cout << "---------------------------" << endl;
    cout << "Would you like to play? (1 for 'YES', 0 for 'NO'): ";
    cin >> play;
    if (play == 1) { shuffleDeck(); }
    else { cout << "See yaa!"; exit(0); }



}


void shuffleDeck() {
    for (int i = 0; i < 52; i++)
    {
        cards[i].value = i % 13; // 13 values
        if (cards[i].value == 0) {
            cards[i].value = 1;
        }
        else if (cards[i].value == 1) {
            cards[i].value = 2;
        }
        else if (cards[i].value == 2) {
            cards[i].value = 3;
        }
        else if (cards[i].value == 3) {
            cards[i].value = 4;
        }
        else if (cards[i].value == 4) {
            cards[i].value = 5;
        }
        else if (cards[i].value == 5) {
            cards[i].value = 6;
        }
        else if (cards[i].value == 6) {
            cards[i].value = 7;
        }
        else if (cards[i].value == 7) {
            cards[i].value = 8;
        }
        else if (cards[i].value == 8) {
            cards[i].value = 9;
        }
        else if (cards[i].value == 9) {
            cards[i].value = 10;
        }
        else if (cards[i].value == 10) {
            cards[i].value = 10;

        }
        else if (cards[i].value == 11) {
            cards[i].value = 10;
        }
        else if (cards[i].value == 12) {
            cards[i].value = 10;
        }


        cards[i].suit = i / 13;// 4 suits
        if (cards[i].suit == 0) {
            cards[i].suit = 'D';
        }
        else if (cards[i].suit == 1) {
            cards[i].suit = 'H';
        }
        else if (cards[i].suit == 2) {
            cards[i].suit = 'C';
        }
        else if (cards[i].suit == 3) {
            cards[i].suit = 'S';
        }
    }

    for (int count = 0; count < 52; count++) {
        int count2 = 0;
        cout << " | " << cards[count].value;
        cout << cards[count].suit << " | ";

        if (count == 13) { cout << endl; }
        else if (count == 26) { cout << endl; }
        else if (count == 39) { cout << endl; }
        else if (count == 52) { cout << endl; }
    }
}

编辑:

void shuffleDeck() {
    theDeck();
    std::random_device rd;
    std::mt19937 g(rd());

    std::shuffle(cards, cards + 52, g);

    for (int count = 0; count < 52; count++) {
        int count2 = 0;
        cout << " | " << cards[count].value;
        cout << cards[count].suit << " | ";

        if (count == 13) { cout << endl; }
        else if (count == 26) { cout << endl; }
        else if (count == 39) { cout << endl; }
        else if (count == 52) { cout << endl; }
    }

【问题讨论】:

  • 使用std::shuffle
  • 那是什么???
  • 这是一个打乱一组数据的函数。
  • 它随机打乱容器中的项目。正是你想要的,除非这是一项禁止你使用 c++ 标准库的学术任务。
  • cards[i].value = std::min(10, (i % 13) + 1); cards[i].suit = "DHCS"[i / 13]; 替换您的长时间初始化。

标签: c++ arrays random


【解决方案1】:

你可以shuffle这样的卡片:

std::random_device rd;
std::mt19937 g(rd());

std::shuffle(cards, cards + 52, g);

不要担心太多第 2 行。您可以将其视为一种确保洗牌的方法很好

【讨论】:

  • 这个库是什么?
  • 我已在答案中添加了链接。它在&lt;algorithm&gt;
  • 查看该链接中的示例代码。这直接来自c++ 标准库。它应该对支持 2011 标准或更高版本的编译器有效。
猜你喜欢
  • 1970-01-01
  • 2016-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-08
相关资源
最近更新 更多