Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplyfing long code lines

Tags:

c++

This code is to list a cards deck in string (Kh,6c,5h, etc..) from a int (0 from 51) or vice versa.

I have written code for it but it seems very long. Is there a more efficient way to write this ?

I want to do this both way too, send a string to a function and get an 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;}

thanks

like image 900
Daheh Avatar asked Feb 07 '26 03:02

Daheh


2 Answers

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
}
like image 113
Benjamin Lindley Avatar answered Feb 13 '26 11:02

Benjamin Lindley


Use an std::array or an 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"); 
like image 44
Vittorio Romeo Avatar answered Feb 13 '26 11:02

Vittorio Romeo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!