2

此代码用于从 int(从 51 中的 0)中以字符串(Kh、6c、5h 等)列出卡片组,反之亦然。

我已经为它编写了代码,但它似乎很长。有没有更有效的方法来写这个?

我也想这样做,将一个字符串发送到一个函数并获取一个 int。

std::string Card::getString(int card) {
    std::string cardstring;

    switch (card) {

    case 0:
        return "2c";
    case 1:
        return "3c";
    case 2:
        return "4c";
    case 3:
        return "5c";
    case 4:
        return "6c";
    case 5:
        return "7c";
    case 6:
        return "8c";
    case 7:
        return "9c";
    case 8:
        return "Tc";
    case 9:
        return "Jc";
    case 10:
        return "Qc";
    case 11:
        return "Kc";
    case 12:
        return "Ac";
    case 13:
        return "2d";
    case 14:
        return "3d";
    case 15:
        return "4d";
    case 16:
        return "5d";
    case 17:
        return "6d";
    case 18:
        return "7d";
    case 19:
        return "8d";
    case 20:
        return "9d";
    case 21:
        return "Td";
    case 22:
        return "Jd";
    case 23:
        return "Qd";
    case 24:
        return "Kd";
    case 25:
        return "Ad";
    case 26:
        return "2h";
    case 27:
        return "3h";
    case 28:
        return "4h";
    case 29:
        return "5h";
    case 30:
        return "6h";
    case 31:
        return "7h";
    case 32:
        return "8h";
    case 33:
        return "9h";
    case 34:
        return "Th";
    case 35:
        return "Jh";
    case 36:
        return "Qh";
    case 37:
        return "Kh";
    case 38:
        return "Ah";
    case 39:
        return "2s";
    case 40:
        return "3s";
    case 41:
        return "4s";
    case 42:
        return "5s";
    case 43:
        return "6s";
    case 44:
        return "7s";
    case 45:
        return "8s";
    case 46:
        return "9s";
    case 47:
        return "Ts";
    case 48:
        return "Js";
    case 49:
        return "Qs";
    case 50:
        return "Ks";
    case 51:
        return "As";
    }
    return cardstring;}

谢谢

4

3 回答 3

16
std::string get_card_string(int card)
{
    if (card >= 0 && card < 52)
    {
        std::string s(2,' ');
        s[0] = "23456789TJQKA"[card % 13];
        s[1] = "cdhs"[card / 13];
        return s;
    }
    return "";
}

The reverse process is a little more complicated. If I thought about it for a while, I might come up with a more clever method, but the obvious choice would be something like this:

std::unordered_map<std::string, int> initialize_card_map()
{
    std::unordered_map<std::string, int> m;
    for (int i=0; i<52; ++i)
        m[get_card_string(i)] = i;
    return m;
}

int get_card_number(std::string const & card_string)
{
    static std::unordered_map<std::string, int> const m = initialize_card_map();
    auto it = m.find(card_string);
    if (it != m.end())
        return it->second;

    return ??? value not found
}
于 2013-10-21T08:06:54.783 回答
3

使用一个std::array或一个std::vector

std::vector<std::string> cards{
   "2c",  // index 0
   "3c",  // index 1 
   "4c"...
};

std::string Card::getString(int card) { return cards[card]; }

assert(getString(0) == "2c"); 
于 2013-10-21T08:04:53.253 回答
1

Benjamin Lindley 的上述答案很棒,但是如果您首先编写像原始帖子一样的代码,您需要问自己更多关于您的设计选择的问题:

为什么必须通过 int 访问卡值?

一个元组代表卡值怎么样?

Peter Norvig 在 Udacity.com 的第一课/讲座上的“计算机程序设计”课程似乎与您非常相关。我建议看看它。

于 2013-10-21T08:18:57.820 回答