1

我是 C++ 的初学者程序员。我在一本旧的 c++ 书中发现了一个非常有趣的练习,它没有该练习的解决方案,所以我希望你们能帮助我:

它要我创建一副 52 张牌,然后洗牌并将牌发给 4 位玩家:

这是代码示例:

cout << "Player 1: ";
for (int j=0; j<13; j++)
{
     card[j].display();
     cout << ", ";
}

玩家 1:8、7、3、2、A、K、5、4、Q、9、3、A、2

现在它要我从大到小排列这些数字:A, A, K, Q, 9 ,8, 7,5 ,4, 3, 3,2, 2

在互联网上进行了研究之后,我学会了如何找到数组的最大数字,但我仍然不知道如何将这些数字从最大到最小排列。我正在使用 GNU 编译器。

谢谢。

对于那些想要所有代码的人,这里是:

#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>
using namespace std;

enum Suit {clubs, diamonds, hearts, spades};

class deck
{
private:
    int number;
    Suit suit;
public:
    void setcards(int n, Suit s)
        { number = n; suit = s; }

    void display();
};

void deck::display()
{
     if (number >= 2 && number <= 10)
    cout << number;
    else
        switch(number)
    {
        case 11: cout << "J"; break;
        case 12: cout << "Q"; break;
        case 13: cout << "K"; break;
        case 14: cout << "A"; break;
    }
    switch(suit)
    {
        case clubs:    cout << static_cast <char> (5); break;
        case diamonds: cout << static_cast <char> (4); break;
        case hearts:   cout << static_cast <char> (3); break;
        case spades:   cout << static_cast <char> (6); break;
    }
}

class game
{
private:
    int j;
    enum {cardmax = 52};
    deck card[cardmax];
public:
    game()
    {
        for (int j=0; j<cardmax; j++)
        {
            int num = (j % 13) + 2;
            Suit su = Suit(j / 13);
            card[j].setcards(num, su);
        }
    }

    void shuffle()
    {
        char ans;

        cout << "Would you like to shuffle the deck? (y/n): ";
        cin >> ans;
        if (ans == 'y')
        {
            srand(time(NULL));
            for (j=0; j<cardmax; j++)
            {
                int k = rand() % 52;
                deck temp = card[j];
                card[j] = card[k];
                card[k] = temp;
            }
        }
        else if (ans =='n')
            deal();
    }

    void deal();
};

void game::deal()
{
    const char esc = 27;
    const char enter = '\r';
    char ans;

    cout << "Press ESCAPE to exit the game or ENTER to deal: ";
    ans = getche();
    if (ans == esc)
        exit(0);
    else if (ans == enter)
    {
        cout << "\n\nPlayer 1: ";
        for (j=0; j<13; j++)
        {
            card[j].display();
            cout << ", ";
        }
        cout << "\nPlayer 2: ";
        for (j=13; j<26; j++)
        {
            card[j].display();
            cout << ", ";
        }
        cout << "\nPlayer 3: ";
        for (j=26; j<39; j++)
        {
            card[j].display();
            cout << ", ";
        }
        cout << "\nPlayer 4: ";
        for (j=39; j<52; j++)
        {
            card[j].display();
            cout << ", ";
        }
        cout << endl;

        cout << "\nWould you like to deal again? (y/n): ";
        cin >> ans;
        if (ans == 'y')
        {
            shuffle();
            deal();
        }
        else if (ans == 'n')
            exit(0);
    }
}

int main()
{
    game cards;
    cards.shuffle();
    cards.deal();
}
4

4 回答 4

3

首先,您需要在您的甲板类中实现小于运算符,如下所示:

class deck {
    //...
    bool operator < (const deck& other) const {return number < other.number;}
    //...
}

然后我建议把那组卡片放在 std::vector 中

std::vector<deck> card;
card.resize(cardmax);
//instead of
deck card[cardmax];

然后你可以像这样对 std::vector 进行排序

std::sort(card.begin(), card.end());

请注意,为了使用 std::vector 类型,您必须#include <vector>在使用它的文件中添加某个位置,并且为了使用 std::sort,您必须添加一个#include <algorithm>

于 2013-09-02T19:30:07.260 回答
1

The easiest sorting algorithm to understand in my opinion is the bubblesort.

The algorithm goes like this:(for biggest to smallest)

First it compares elements 0 and 1. If (element 0 less than element 1) THEN swap the elements Then it does the same thing, but with elements 1 and 2.

When you get to the end of the array, if any swaps have been made, you must start back at elements 0 and 1, and work your way across the array again.

I know that may be difficult to understand just reading it, but HERE is a youtube beginner c++ video that should make it very easy to understand.

于 2013-09-02T19:33:57.657 回答
1

抬头看std::sort

您应该能够将其应用于您的阵列:

   std::sort(&card[0], &card[cardmax]);

如果您想以不同的方式订购卡片,则必须编写一个更改顺序的函数。

来自http://www.cplusplus.com/reference/algorithm/sort/?kw=sort

comp
    Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines.
    The function shall not modify any of its arguments.
    This can either be a function pointer or a function object.

例子:

class deck
{
  [...]  // All your existing code.
  public:
    static bool descending_order(const deck& first_card, const deck& second_card)
    {
      return first_card.number > second_card.number;
    }
};
于 2013-09-02T19:32:24.130 回答
0

这是我在没有太多重构的情况下如何做到的。为了整个过程,我会拆分提示并在交易之外“再次交易”循环,这样它就不是递归的,并且制定了逻辑,所以你不需要exit(0)

#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <functional>
#include <iomanip>

//If no c++ 11 you may need to remove the next line
#include <tuple>

using namespace std;

enum Suit {clubs, diamonds, hearts, spades};
const char* CardLetters[] = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
const char SuitLetters[] = { 5, 4, 3, 6 };

class deck
{
private:
    int number;
    Suit suit;
public:
    void setcards(int n, Suit s)
    { 
        number = n; suit = s; 
    }

    void display()
    {
        cout << std::right<< std::setw(2) << CardLetters[number - 2] << SuitLetters[suit];
    }

    bool operator >(const deck& rhs) const
    {
        //If you have c++ 11, this is cleaner
        //return std::tie(number, suit) > std::tie(rhs.number, rhs.suit);
        //otherwise
        if(number == rhs.number)
        {
            return suit > rhs.suit;
        }
        return number > rhs.number;
    }
};

class game
{
private:
    enum {cardmax = 52};
    deck card[cardmax];
public:
    game()
    {
        for (int j=0; j<cardmax; j++)
        {
            int num = (j % 13) + 2;
            Suit su = Suit(j / 13);
            card[j].setcards(num, su);
        }
    }

    void shuffle()
    {
        char ans;

        cout << "Would you like to shuffle the deck? (y/n): ";
        cin >> ans;
        if (ans == 'y')
        {
            std::random_shuffle(&card[0], &card[cardmax]);
        }
        else if (ans =='n')
        {
            deal();
        }
    }

    void deal();
};

void game::deal()
{
    const char esc = 27;
    const char enter = '\r';

    cout << "Press ESCAPE to exit the game or ENTER to deal: ";
    char ans = getche();
    if (ans == esc)
    {
        exit(0);
    }
    else if (ans == enter)
    {
        for(int playernum = 0; playernum < 4; ++playernum)
        {
            const int offset = 13 * playernum;
            std::sort(&card[offset], &card[offset + 13], std::greater<deck>());
            cout << "\n\nPlayer " << (playernum + 1) << ": ";
            for (int j=0; j<13; j++)
            {
                if(j > 0) 
                { 
                    cout << ", ";
                }
                card[offset + j].display();
            }
        }

        cout << "\n\nWould you like to deal again? (y/n): ";
        cin >> ans;
        if (ans == 'y')
        {
            shuffle();
            deal();
        }
        else if (ans == 'n')
        {
            exit(0);
        }
    }
}

int main()
{
    srand(time(NULL));
    game cards;
    cards.shuffle();
    cards.deal();
    return 0;
}
于 2013-09-02T20:17:22.730 回答