对一副卡片进行冒泡排序

发布于 2025-01-14 20:15:21 字数 1918 浏览 3 评论 0原文

我已经把自己带进了一个我不知道如何摆脱的兔子洞,我创建了一副随机卡片。但是,我现在需要首先按花色对它们进行排序,然后按枚举的顺序按排名对它们进行排序。首先,我不知道如何从向量内和枚举内获取元素,所以我可以比较它们,其次,我怀疑这个链接到第一个我不知道如何交换它们,最后会很好有一个用于冒泡排序的函数,我可以调用两次以按花色对它们进行排序,然后再次调用以按卡排序。

这是库:

#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
using std::vector;

这些是结构和枚举:

enum Rank { ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }; // aspects of the card
enum Suit { HEARTS, CLUBS, DIAMONDS, SPADES };                                                    // suits of the card

struct Card {                       //Card data numbers
    Rank rank;
    Suit suit;
};

struct Deck {                      // defines a card
    vector<Card> cards;            // All the cards
    int size = 5;                  // Max deck size
};

这是生成随机卡片组的内容:

void initialize(Deck& deck, string stdno) {
    for (int count=0; count < deck.size; count++) { //creates a deck of cards
        Card card;
        string rcardstr = stdno.substr(rand() % 8, 1) + stdno.substr(rand() % 8, 1);
        int rcard = stoi(rcardstr) % 52;
        card.suit = static_cast<Suit>(rcard / 13);     //cast between the enumerator and the value
        card.rank = static_cast<Rank>(1 + rcard % 13);
        deck.cards.push_back(card); //Adds the card to the deck of cards
    }
}

这是我在意识到前两个问题之前对冒泡排序的微弱尝试:

void bubble_sort(Deck& deck ){
    bool swapp = true;
    while (swapp) {
        swapp = false;
        for (int i=0; i < deck.size; i++) {
            if (deck.rank[0]
        }
    }
}

...最后是我的主要功能:

int main() {
    string stdno="14398132";
    Deck my_deck;
    initialize(my_deck, stdno);
}

如果有人愿意愿意给我几分钟的时间来解释我需要看哪里/我需要学习什么,我将非常感激,并确保在我在 c++ 方面变得更好后将同样的时间回馈给社区。

I have taken myself down a rabbit hole that I do not know how to get out of, I have created a deck of random cards. However, I now need to sort them firstly by suit then by rank in the order of my enum's. Firstly I can't figure out how to get elements from within the vector and within the enum, so I can compare them, secondly, I suspect this links to the first I'm not sure how to swap them, finally it would be nice to have a function for the bubble sort that I can call twice to first-order them by suit and then call again to order by card.

Here are the libraries:

#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
using std::vector;

These are the structs and enum's:

enum Rank { ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }; // aspects of the card
enum Suit { HEARTS, CLUBS, DIAMONDS, SPADES };                                                    // suits of the card

struct Card {                       //Card data numbers
    Rank rank;
    Suit suit;
};

struct Deck {                      // defines a card
    vector<Card> cards;            // All the cards
    int size = 5;                  // Max deck size
};

This is what generates a random card deck:

void initialize(Deck& deck, string stdno) {
    for (int count=0; count < deck.size; count++) { //creates a deck of cards
        Card card;
        string rcardstr = stdno.substr(rand() % 8, 1) + stdno.substr(rand() % 8, 1);
        int rcard = stoi(rcardstr) % 52;
        card.suit = static_cast<Suit>(rcard / 13);     //cast between the enumerator and the value
        card.rank = static_cast<Rank>(1 + rcard % 13);
        deck.cards.push_back(card); //Adds the card to the deck of cards
    }
}

This is my feeble attempts of a bubble sort before I realized my first two problems:

void bubble_sort(Deck& deck ){
    bool swapp = true;
    while (swapp) {
        swapp = false;
        for (int i=0; i < deck.size; i++) {
            if (deck.rank[0]
        }
    }
}

...And finally my main function:

int main() {
    string stdno="14398132";
    Deck my_deck;
    initialize(my_deck, stdno);
}

If someone would be willing to give me a few minutes of their time to explain where I need to look/what I need to learn I would be incredibly grateful and make sure to give the same time back to the community once I've become better at c++.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单 2025-01-21 20:15:21

访问牌组成员的方法是这样写:

deck.cards[i].rank

deck.cards[i].suit

您有多种排序选项。

  1. 只需编写两个函数:
void bubble_sort_by_rank(Deck& deck ){
    bool swapp = true;
    while (swapp) {
        swapp = false;
        for (int i=0; i < deck.cards.size()-1; i++) {
            if (deck.cards[i+1].rank > deck.cards[i].rank){
                //...
            }
        }
    }
}

void bubble_sort_by_suit(Deck& deck ){
    bool swapp = true;
    while (swapp) {
        swapp = false;
        for (int i=0; i < deck.cards.size()-1; i++) {
            if (deck.cards[i+1].suit > deck.cards[i].suit){
                //...
            }
        }
    }
}
  1. 编写一个冒泡排序函数,但将排序标准作为参数(可以是枚举)。

  2. 编写一个冒泡排序函数,但首先按花色比较每张牌,如果它们相等,则按排名比较它们,或者只是重载运算符

    按照评论中的建议,选择您的卡片类别。这可能是首选方法并且更有效。

如果您不坚持使用冒泡排序,则可以使用 std::sort 和 lambda 函数,该函数首先按花色比较您的牌,然后按排名进行比较。

对于交换,您应该能够使用 std::swap 或编写一个简单的自己的模板函数:

template <class T>
void swap(T& x,T& y)
{
     T temp;
     temp=x;
     x=y;
     y=temp;
}

The way to access the members of the deck is to write:

deck.cards[i].rank

or

deck.cards[i].suit

You have multiple options for sorting.

  1. Simply write two functions:
void bubble_sort_by_rank(Deck& deck ){
    bool swapp = true;
    while (swapp) {
        swapp = false;
        for (int i=0; i < deck.cards.size()-1; i++) {
            if (deck.cards[i+1].rank > deck.cards[i].rank){
                //...
            }
        }
    }
}

and

void bubble_sort_by_suit(Deck& deck ){
    bool swapp = true;
    while (swapp) {
        swapp = false;
        for (int i=0; i < deck.cards.size()-1; i++) {
            if (deck.cards[i+1].suit > deck.cards[i].suit){
                //...
            }
        }
    }
}
  1. Write one bubble sort function, but take the sort criteria as a parameter (which can be enum).

  2. Write a single bubble sort function, but compare each card first by suit and if they were equal, then compare them by rank or just overload the operator< for your Card class as suggested in the comments. This is probably the preferred method and more efficient.

If you don't insist on using bubble sort, you can just use std::sort with a lambda function that compares your cards first by suit then by rank.

For swapping you should be able to use std::swap or write a simple template function yourself:

template <class T>
void swap(T& x,T& y)
{
     T temp;
     temp=x;
     x=y;
     y=temp;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文